Created
April 26, 2017 21:43
-
-
Save supasympa/a42c67ea6bf53e21a2af4133b20dc1d4 to your computer and use it in GitHub Desktop.
Functional Javascript compose example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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