Function composition is the act of combining one or many functions into a single function.

For example, lets say our compose function signiture looks like this:

function compose(functions) { |

How can we build this function?

function compose(functions) { |

Let’s improve on this further by using forEach.

function compose(functions) { |

With that simple change we get something much cleaner. We can simplify even futher by using reduce, where we use the accumulator to store the output of the previous function, with it’s inital value being our input.

function compose(functions) { |

Update 7/21: Technically this should be `reduceRight`

and not `reduce`

.

Let’s take a look at a working example.

// the functional equivalent of Math.sqrt((x * 2) + 4) |