diablero13
1/10/2018 - 9:56 PM

JavaScript Compose Function

JavaScript Compose Function

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))

// Usage : compose functions right to left
// compose(minus8, add10, multiply10)(4) === 42
//
// The resulting function can accept as many arguments as the first function does
// compose(add2, multiply)(4, 10) === 42


// Alternative version 
// const compose = (...fns) => x => fns.reduce((v, fn) => fn(v), x)