Function Composition
Function composition is another concept that leaks over from mathematics.
Given two functions, a and b, compose returns a new function that applies a to the output of b, which is then applied to a given set of parameters.
Function composition is a way to create a complex function from a set of smaller ones.
This will mean you might end up with a bunch of simple functions that do one thing well. Functions with a single purpose are better at encapsulating their functionality and therefore help with separation of concerns.
Composing functions ties in with currying and the partial application of functions since currying/partial application is a technique that allows you to have specialized versions of generic functions, like so:
const sum = x => y => x + y; const multiply = x => y => x * y; const compose = (f, g) => x => f(g(x)); const add1 = sum(1); const add2 = sum(2); const double = multiply(2);
To explain the following code...