Re:List Comprehensions in Eight Lines of Clojure
by kenko on Wednesday January 02, 2013 @10:54AM
(#4997256)
"p.s. You're missing a square bracket after [1 2]. I was puzzling even more before I realized that :)"
Whoops!
"Both idioms are useful, but I use the parallel one quite a bit more than the nested one. Does FOR handle both?"
Not directly---to get a parallel product, you would use something like:
(map vector '(0 1 2 3 4 5) '(1 2))
For the same reasons (concision, idiom, and clarity), forgive me but I don't like the monads/with-monad versions you've given. "monads/with-monad monads/sequence-m" is much too verbose — I want an operation this simple to be lightweight. It doesn't feel idiomatic to me for looping and pairing things, though that may just be my ignorance. And it feels like some powerful and general infrastructure is being invoked to do something whose triviality I would rather see the code bring out. Looping is the canonical example, I think, of something that one does so often and has such repetitive-yet-assorted patterns that a little language is perfect for the job. But I'm biased, as I said.
By the way, I've been puzzling over that FOR form trying to make out how it ends up producing a Cartesian product out of '(0 1 2 3 4 5) and '(1 2). In CL, if you make the bindings parallel like that, as in (loop for x in foo for y in bar...), it means "take an x from foo and a y from bar and keep going as long as they're both producing new values". So to get a Cartesian product, I had to make an explicit nested loop in my translation above. Both idioms are useful, but I use the parallel one quite a bit more than the nested one. Does FOR handle both?
p.s. You're missing a square bracket after [1 2]. I was puzzling even more before I realized that :)