Composing
Composing is quite similar to pipelining, but has its roots in mathematical theory. The concept of composition is a sequence of function calls in which the output of one function is the input for the next one—but in the opposite order to when pipelining. So, if you have a series of functions, from left to right, when pipelining, the first function of the series to be applied is the leftmost one, but when you use composition, you start with the rightmost one.
Let’s investigate this a bit more. When you define the composition of, say, three functions as (f
∘g
∘h
) and apply this composition to x
, this is equivalent to writing f(g(h(x)))
.
It’s important to note that, as with pipelining, the arity of the first function to be applied (actually the last one in the list) can be anything, but all the other functions must be unary. Also, besides the difference in the sequence of function evaluation, composing is an important tool in FP: it abstracts...