krismicinski
2018-5-26 22:48:17

@krismicinski has joined the channel


krismicinski
2018-5-26 22:51:32

I have a super-basic question: if I want to organize my tests into a separate directory, and have them import code from a source directory, is the right way to have a project structure like this:

project/
  - info.rkt
  - src/
      - file1.rkt
      - ...
      - info.rkt
  - test/
      - info.rkt
      - test1.rkt
      - ...

samth
2018-5-27 00:32:06

Usually there’s no src directory


krismicinski
2018-5-27 00:34:18

ah ok.


krismicinski
2018-5-27 04:07:01

I have what I feel must be a naive question about macros, but I’m not sure if I’m forming it correctly. I want to define a macro that instruments every #%app form to add a bit of information to that closure at runtime. A simple example might be to say “every time I call into a lambda, add the caller’s name to a list” but that list is some variable local to that closure.


krismicinski
2018-5-27 04:08:04

I thought the way to do this would be to have my #%app macro wrap the application with a parameterize, but I’m not sure where to get the identifier from. If I just pick one out of thin air, then things lower in the stack can’t access that identifier, and of course, I have to worry about hygiene as well.


krismicinski
2018-5-27 04:08:15

I’ll try to write a minimal example that illustrates my point


ghoetker
2018-5-27 05:18:06

I have a JSON file such as the below and with to return the object that has “name” of “lipsum”. I’ve figured out one way to do it:

#lang racket
(require json)

(define json-file "/Users/ghoetker/Desktop/tl.json")

(define v (with-input-from-file json-file
  (λ () (read-json))))

(for ([pkg v])
  (when (equal? (hash-ref pkg 'name) "lipsum")
    (pretty-print pkg)))

This seems inefficient, especially since the actual file has about 7000 much larger objects, and somewhat anti-functional in relying on a loop. Is there a more Rackety approach that anyone would suggest? Many thanks.

[
  {
    "executes": [],
    "name": "lipsum",
    "shortdesc": "Easy access to the Lorem Ipsum dummy text"
  },
  {
    "executes": [],
    "name": "animate",
    "shortdesc": "Create PDF animations from graphics files and inline graphics"
  }
]

lexi.lambda
2018-5-27 05:23:56

@ghoetker Are the "name" fields unique? That is, is there only one object in the array with the name "lipsum"?


ghoetker
2018-5-27 05:30:33

@lexi.lambda Yes.


lexi.lambda
2018-5-27 05:30:58

Do you control the JSON?


ghoetker
2018-5-27 05:32:31

@lexi.lambda Oh, how I wish I did. But, unfortunately, no. I did, just as you were typing, have the idea of

(define (lipsum? o)
  (equal? (hash-ref o 'name) "lipsum"))

(filter lipsum? v)

Avoids the loop, but I’m not sure it is more performant.


lexi.lambda
2018-5-27 05:33:28

filter is just iterating over the list, so it’s doing the same thing. The only difference is that you’re not running the loop for a side-effect, you’re getting back the value.


lexi.lambda
2018-5-27 05:34:32

If you care about efficient key lookups over a large dataset, a list is just never going to be performant. If you’re only looking up one key, then you’re pretty much forced to do what you’re doing, but if you’re doing multiple lookups, you could rearrange the data into a hash table and do actual key lookups.


ghoetker
2018-5-27 05:37:26

@lexi.lambda Ah, cool. I will be doing multiple lookups. Let me play with that and see if I can figure out how to do that rearrangement. Thank you so much for all of you help on my multiple questions over the last week or so. It’s made a huge difference.


lexi.lambda
2018-5-27 05:39:23

You could look into using for/hash, which would make it easy to build a hash table in a similar way to the for loop you were already using.


lexi.lambda
2018-5-27 05:40:11

As one other small point of advice, you should use in-list when iterating over a list if you care about performance; that is, write (for/hash ([pkg (in-list v)]) ....) to specialize the loop to lists.


ghoetker
2018-5-27 05:58:34

@lexi.lambda (Obviously, please stop when you have more useful/fun things to do than help me). With your hint, that ended up being pretty easy. I’d already done it with a loop and hash-set!.for-hashis nicer:(define pkg-ht (for/hash ([pkg (in-list v)]) (values (hash-ref pkg 'name) (hash-ref pkg 'shortdesc))))Two questions, if I may: a) Have I taken your nudge in the proper direction? b) If I wanted to easily be able to retrieve the "shortdesc" and the "executes" values for a package of a given "name", would I put a hash-table as the second item of myvalues` statement? If so, can you give me a nudge in how to construct that, as I’m struggling with it.