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?
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?
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.
Foldable
type class?
Commented
Jan 2, 2014 at 10:37
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.
Commented
Jan 3, 2014 at 8:24