Skip to content

Instantly share code, notes, and snippets.

@supasympa
Created April 26, 2017 21:43
Show Gist options
  • Save supasympa/a42c67ea6bf53e21a2af4133b20dc1d4 to your computer and use it in GitHub Desktop.
Save supasympa/a42c67ea6bf53e21a2af4133b20dc1d4 to your computer and use it in GitHub Desktop.
Functional Javascript compose example
const add = (...args) => {
console.log('add ', args)
return args.reduce((acc, n) => acc += parseInt(n), 0)
}
const exec = (fn, ...args) => {
console.log('exec ', fn, args)
return fn.apply(fn, args)
}
const compose = (fns) => {
return function (init) {
console.log(init)
return fns.reduce((acc, n) => exec(n, acc), init)
}
}
const multiplyBy10 = (val) => val * 10
const add10 = (val) => val + 10
const minus23 = (val) => val - 23
const composed = compose([add10, multiplyBy10, minus23])
console.log(composed(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment