poigalnitish
2021-7-17 07:54:51

@poigalnitish has joined the channel


zenspider
2021-7-17 22:50:36

I’ve got this pattern across my code:

(for/list ([thing things]) (match-let ([(cons a d) thing]) ...)) I’m trying to move it to something more like this, but I’m still getting arity errors despite the map values part: (for/list ([(a d) (map values things)]) ...) What am I not getting? I thought map values was pretty normal?


soegaard2
2021-7-17 23:00:47

Well, > (map values (list 1 2 3)) '(1 2 3) So the clause [(a d) (map ...)] will only succeed if the list happens to be of length two.


zenspider
2021-7-17 23:01:45

whereas the cons match form is getting head and tail… gotcha…


zenspider
2021-7-17 23:03:40

hrm… is there a better/cleaner pattern to read in data (input via read so it’s all pairs and atoms) and then process the data into a final form? My match-fu isn’t strong enough yet to match and process the whole thing in one fell swoop.


soegaard2
2021-7-17 23:04:37

There is an in-match form that might do what you want (in adjutor/unstable). Looks useful, so a shame it is in “unstable”.


zenspider
2021-7-17 23:04:44

Initially I went with a bunch of processors like the above to create a list of structs of structs… but the struct side throws a lot of benefits of raw data out the window unless I add a bunch of #:methods protocols


soegaard2
2021-7-17 23:06:12

There is in-port and in-lines for the reading part.


zenspider
2021-7-17 23:09:27

the reading part is the easy part: (process (cadr (read (open-input-file “workout_data.scm")))) … but then I want to filter out data and transform values through the tree… ((date (name weight time target-time) ...) …) … eg if one of the entries isn’t that shape I want to reject it… and I want to parse the time and target time so I can say <seconds> or <minutes>:<seconds> into integer seconds … etc. nested match feels really hairy


soegaard2
2021-7-17 23:09:31

For bindings in for-clauses, something like in-match is needed - but I don’t think there are other binding sequences other than in-value in the standard library.


zenspider
2021-7-17 23:10:14

I might just take a whack at writing a for/list/match macro to clean up the above a bit


soegaard2
2021-7-17 23:10:18

As an alternative to nested match-let there is match-define.


zenspider
2021-7-17 23:10:42

but I’d love to see what idiomatic structured processing should look like


soegaard2
2021-7-17 23:13:02

Maybe something like: (for/list ([thing things] #:when (valid? thing)) (format-thing thing))


zenspider
2021-7-17 23:13:34

that’s roughly what I have now… it could use some refactoring into those predicates


soegaard2
2021-7-17 23:13:37

Where valid? is a helper function defined elsewhere.


zenspider
2021-7-17 23:14:52

I started to go whole hog into creating structs, but I lost really useful things like assq or dict-ref without jumping through some really ugly hoops, so now my struct definitinions look like:

(define exercise list) (define workout cons)


soegaard2
2021-7-17 23:16:10

An alternative is to do: (filter values (for/list ([thing things]) (match-define (cons a d) thing) (and <valid-test> <format-thing>))))


zenspider
2021-7-17 23:18:10

hrm… I should swap from match-let to match-define to flatten the nesting…


robert.mitchell36
2021-7-18 01:53:10

@robert.mitchell36 has joined the channel