One way to do function composition in Haskell is using the (.)
operator.
Something like
foo = bar . baz
-- which reads like
-- foo x = bar (baz x)
Which is commonly referred to as “backwards composition”. Meaning the flow of information goes backwards, when an x
fills the spot, the result can be used as the argument for bar.
Another way to compose functions is using the &
(or >>>
for arrows) operator, akin to |>
in some languages, which is forward composition.
foo' x = x & baz & bar
-- which reads like
-- foo' x = bar (baz x)
Which, notably, has the direction of information going forwards.
Given this, I wonder why we use backwards composition? Is it because it’s more akin to mathematics, which makes mathematical algorithms perhhaps easier to translate? Is it because it’s easier to make things point-free using backwards composition? Maybe it’s closer to how you’d compose functions without (.)
? Or is there some sort of inherent problem with forwards composition that I don’t know about?
IMO, forwards composition which feels like a pipeline, is much more natural for me to read and write. I really do prefer it, but most people using backwards composition in Haskell is making me wonder if there’s something I’m missing out on.