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. It is not my first time to pay a quick visit this web page, i am visiting this web
    page dailly and obtain good information from here everyday.

    Comment by mobile — 3 April 2014 @ 10 h 24 min
  12. But, prior to taking the services for web hosting, users must
    ensure the fulfillment of some of the key criteria.
    With free hosts be prepared to see your website online for a very short period.
    With web hosting services, you can give a professional look to your blog.

    Comment by web hosting — 5 April 2014 @ 0 h 02 min
  13. Undeniably believe that that you stated. Your favourite reason seemed to be on the
    internet the simplest thing to be mindful of. I say tto you,
    I certainly get irked whilst folks think about issues
    that the plainly do not understand about. Yoou managed
    too hit the nail upon the highest as well as defined out thhe whole thing without having side-effects ,
    folks could take a signal. Will likely bee again to get
    more. Thank you

    Comment by successfully completed home — 19 June 2014 @ 5 h 30 min
  14. There is certainly a lot to learn about this topic.
    I love all the points you hasve made.

    Check out my website: concrete blonde joey 4shared mp3 4shared search

  15. Link excange is nothing elde except it is onl placing the
    other person’s web site link on yoiur page at suitable
    plce and oyher person will also do similar for you.

    Comment by Potters Bar Aggregates — 22 August 2014 @ 4 h 01 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

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