3

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?

5

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.

  • So reducers is just an abstraction over the foldable collections? Like haskell's Foldable type class? – Simon Bergot 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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