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?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
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.