Clojure refactoring: flattening reduces
This morning I wrote some code which looked like:
(reduce (fn [acc x] (reduce (fn [acc y] (reduce f acc y)) acc x)) init xs)
(it was slightly more complex with some filtering and destructuring thrown in for good measure).
I wasn’t happy with those nested reduces and it occured to me that I could refactor it to use a single one:
(reduce f init (for [x xs, y x, z y] z))
Now that reads better!
OK, please keep this sort of thing to yourself. You’re scaring people. :) (see: http://search.twitter.com/search?q=too+stupid+to+program+in+clojure )
I’m kidding, but one of the reasons I love Clojure is that it’s deeply practical for problems that don’t resolve to math functions. Some elaboration might benefit this example. I guess you have a sequence of sequences of sequences and want to reduce f over all values contained therein? (where values are always in the innermost sequences only)
Thanks for the blog!
Took me a sec to figure out what you’re doing but that is freakin’ awesome. Will have to remember it in the future.
@Jeremey: it was a seq of maps of sets (which I could have obtained from a database) and the computation used values of the sets and keys of the maps.
That’s just awesome, really neat code!
I used to write something like this: (clojure.contrib.seq-utils/flatten (apply concat (map seq foo))) instead of (for [x foo, y x, z y] z), I don’t use ‘for’ enough.
[...] all the examples, you’re seeing the power of the seq-abstraction. Lets say you need to work on a nested strucuture, only working on the innermost data — double bound for is your friend. Imagine you have a [...]
Very neat way to flatten sequence of sequences! I would usually do something like what Nicolas did, but this way is much better. Thank you for posting it!
Thanks for writing this idiom up. I had a need for just such an idiom today and instead of a lot of small nested reduces I used the a for form to handle it all.
Same for me: just needed this and luckily remembered this post. You should rename your blog to “Treasure Chest” or so. :)
Make sure you excuse my my English speak, i’m simply schooling. I substantially like your website greatly, I realize it quite fascinating and i saved a bookmark in my personal internet.
Hello! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Cheers!
ghs tool c’est un terriblement bon outil pour le positionnement
internet où l’on peut planifier des taches, qui gère le spintax, les positions sur Google etc…
Currently it appears like Movable Type is the preferred blogging platform
available right now. (from what I’ve read) Is that what you
are using on your blog?
iStlhe Muchos Gracias for your blog.Thanks Again. Cool.
Good post. I learn something totally new and challenging on sites I stumbleupon everyday. It will always be exciting to read through content from other writers and practice a little something from their web sites.
Good day very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds also…I am glad to seek out a lot of helpful information right here within the post, we’d like develop extra techniques on this regard, thanks for sharing. . . . . .
This website was how do I say it? Relevant!! Finally I ave found something that helped me. Thanks!
very good submit, i definitely love this web site, carry on it
himself and his country on the
global stage. He let the Russians spring
from American confinement a
weapons to terrorists and our adversaries
Prediction is difficult, especially about
the future.” That line is often misattributed
to professional baseball player
Yogi Berra, but it actually is a Danish
proverb that was repeated
by Niels Bohr, the scientist
who figured out the structure of the
The resurrected political dinosaur
from California proposes
policy of rolling back communism
scoffed Yuri Zhukov in the pages of
Pravda in April 1975. “It is
very straight
Bolje od dorucka u krevetu – vesti na dlanu . Pamti tvoje „volim te“ zauvek !