@poigalnitish has joined the channel
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?
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.
whereas the cons
match form is getting head and tail… gotcha…
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.
There is an in-match
form that might do what you want (in adjutor/unstable
). Looks useful, so a shame it is in “unstable”.
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
There is in-port
and in-lines
for the reading part.
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
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.
I might just take a whack at writing a for/list/match
macro to clean up the above a bit
As an alternative to nested match-let
there is match-define
.
but I’d love to see what idiomatic structured processing should look like
Maybe something like: (for/list ([thing things] #:when (valid? thing)) (format-thing thing))
that’s roughly what I have now… it could use some refactoring into those predicates
Where valid?
is a helper function defined elsewhere.
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)
An alternative is to do: (filter values
(for/list ([thing things])
(match-define (cons a d) thing)
(and <valid-test>
<format-thing>))))
hrm… I should swap from match-let to match-define to flatten the nesting…
@robert.mitchell36 has joined the channel