JavaScript Currying 3 - Compose
September 15, 2020The compose
function in the code below takes any number of functions, returns a function that takes the initial value, and then uses the reduceRight
function to iterate right-to-left over each function f
in functions
argument and returns the accumulated value y
. In other words, the compose
function creates a pipeline of functions with the output of the function is connected to the input of the next function.
index.js
1const compose =2 (...functions) =>3 (x) =>4 functions.reduceRight((y, f) => f(y), x)56const g = (n) => n + 17const f = (n) => n * 28const h = compose(f, g)9h(20) // => 42