Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just stumbled upon Clojure reducers library.

It looks a lot like loop fusion. However the term is not mentioned anywhere on the page. Is there a difference between the two?

share|improve this question
up vote 4 down vote accepted

Loop fusion is an optimisation where the compiler notices you have two separate loops iterating over the same values, so just fuses the loop bodies.

You get reducers when you split the classic fold/reduce implementation into its separate pieces. In particular, you split out the reducing function itself (the action you take on the accumulator and current value) and the particular traversal scheme.

I imagine the reason you thought of loop fusion is that you can combine reducing functions together, so that when reduction actually takes place (i.e., when you actually iterate over the values of the collection), the reductions happen "together".

The bodies of the loops correspond to the reducing functions, and the loop itself corresponds to the traversal scheme.

One important difference is that the actions that take place inside the loops have no necessary relationship: they could perform completely different actions. With reducers, you're still mapping a collection to a result (in the case of reduce. Obviously for map you're mapping a collection to a collection). It's still a function returning a single result.

share|improve this answer
So reducers is just an abstraction over the foldable collections? Like haskell's Foldable type class? – Simon Jan 2 '14 at 10:37
I'm not an expert here, but I think that the reducers library take the separation of concern a bit further. Clojure reducible maps to Haskell's Foldable, but as far as I can tell, the reducing function part doesn't have a direct analogue in Haskell. – Frank Shearar Jan 3 '14 at 8:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.