Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created April 15, 2019 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/7ed5750901a3e5ed31a4c1e271ddc95e to your computer and use it in GitHub Desktop.
Save yairEO/7ed5750901a3e5ed31a4c1e271ddc95e to your computer and use it in GitHub Desktop.
Composition example
function last(n){
return next => next - n;
}
function second(n){
return next => next * n;
}
function first(n){
return next => next + n;
}
var compose = (...funcs) => x =>
funcs.reduceRight((accumulator, currentValue) => {
console.log(accumulator, currentValue)
return currentValue(accumulator)
}, x)
var chain = [last, second, first].map(f => f(2));
console.warn(
compose(...chain)(10)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment