Clojure refactoring: flattening reduces

mistakes — cgrand, 19 January 2010 @ 12 h 48 min

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!


  1. OK, please keep this sort of thing to yourself. You’re scaring people. :) (see: )

    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!

    Comment by Jeremey — 19 January 2010 @ 21 h 07 min
  2. Took me a sec to figure out what you’re doing but that is freakin’ awesome. Will have to remember it in the future.

    Comment by Patrick Sullivan — 19 January 2010 @ 21 h 17 min
  3. @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.

    Comment by cgrand — 19 January 2010 @ 21 h 23 min
  4. 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.

    Comment by Nicolas Buduroi — 20 January 2010 @ 1 h 50 min
  5. [...] 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 [...]

    Pingback by Simplicity on Steroids | BEST IN CLASS — 4 February 2010 @ 22 h 29 min
  6. 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!

    Comment by Ivan Koblik — 27 February 2010 @ 22 h 34 min
  7. 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.

    Comment by William Hidden — 7 April 2010 @ 16 h 24 min
  8. Same for me: just needed this and luckily remembered this post. You should rename your blog to “Treasure Chest” or so. :)

    Comment by Meikel — 30 April 2010 @ 11 h 55 min
  9. 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.

    Comment by Jon Judah — 5 May 2011 @ 18 h 43 min
  10. 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!

    Comment by Galaxys2 Forsale — 9 September 2011 @ 13 h 03 min
  11. 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…

    Comment by ghs tools — 29 October 2015 @ 3 h 10 min
  12. 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?

    Comment by Maryellen — 1 August 2018 @ 12 h 22 min
  13. iStlhe Muchos Gracias for your blog.Thanks Again. Cool.

    Comment by suba suba — 23 December 2019 @ 14 h 17 min
  14. 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.

    Comment by beni ourain rug — 8 May 2020 @ 17 h 33 min
  15. 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. . . . . .

    Comment by erotic massage — 9 May 2020 @ 2 h 14 min
  16. This website was how do I say it? Relevant!! Finally I ave found something that helped me. Thanks!

    Comment by Rolet online — 8 July 2020 @ 19 h 33 min
  17. very good submit, i definitely love this web site, carry on it

    Comment by Joe Biden 2020 T Shirt — 21 September 2020 @ 2 h 09 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2023 Clojure and me | powered by WordPress with Barecity