@charlestaylor95 has joined the channel
When iterating over nested data structures like: #(#(1 a)#(2 b)#(3 c))
using a for form, with the intention of binding nested elements at certain indexes to specific names, how do you find is the best/clearest way to go about it? I initially thought: (for ([(a b) v])
;...)
would work, but that only works out of the box for hash tables. Any idea if there’s a specific reason why? Other than, you obviously can’t include every possible feature (that’s why macros exist) in the language; I was curious if there’s some kind of ambiguity or clash of some kind that makes this behaviour as default a bad choice in general. The other two options I know of are: (for* ([inner v]
[a (in-value (vector-ref inner 0))]
[b (in-value (vector-ref inner 1))])
;...)
and (for* ([(a b) (in-parallel (vector-map (curryr vector-ref 0) v)
(vector-map (curryr vector-ref 1) v))])
;...
But I personally find those to be quite verbose and, more importantly, a bit less “obvious”. Any tips? I’d normally just use the for form itself purely for iterating over the outermost structure, and then a binding form like match-let/define inside the body of the for to bind the inner elements to the desired names. I’ve yet to really make use of the various for/… forms outside of the most basic functionality like for/list, for/fold, for/first, #:when, that sort of thing. Thank you :)
@sydney.lambda why not use a hash table to represent the nested data? do you have duplicate keys, or does order matter somehow?
@notjack I was just experimenting with for/ to try and get more familiar with it, and upon realising that the ([(a b) data-structure]) didn’t work, and seeing how verbose that alternatives were, wondered if there’s a common way of doing that which I’m missing, or whether it’s more common to just use an inner let/define. I don’t have a specific use case, I’m afraid.
and also I was just a bit curious what others think of this in regards to language design - whether it’s a “good” idea to have that sort of general ([(a b) data-structure]) form work, and whether they find it intuitive/useful. I’m still finding my feet with macros, and these are the types of decisions/considerations I’m trying to think about more.
I think making pattern matching work in for
forms is a good idea
Thanks @notjack :) After some searching, I found @soegaard2 wrote a function that does the job: https://stackoverflow.com/questions/37469667/multiple-bindings-in-a-for-loop so I’ll have a go at working it into the for syntax via a macro. Should be a good learning exercise.