Skip to content

Instantly share code, notes, and snippets.

@JakeTheCorn
Created January 22, 2018 15:45
Show Gist options
  • Save JakeTheCorn/3bac69a2eaff99c93ac36c09908dd22a to your computer and use it in GitHub Desktop.
Save JakeTheCorn/3bac69a2eaff99c93ac36c09908dd22a to your computer and use it in GitHub Desktop.
// https://gist.github.com/JamieMason/172460a36a0eaef24233e6edb2706f83#file-es6-compose-md
// By: Jamie Mason
// https://gist.github.com/JamieMason
const compose = (...fns) =>
fns.reverse().reduce((prevFn, nextFn) =>
value => nextFn(prevFn(value)),
value => value
);
const example = compose(
val => { console.log(1); return `1<${val}>`; },
val => { console.log(2); return `2<${val}>`; },
val => { console.log(3); return `3<${val}>`; }
);
example('hello');
/* OUTPUT TO CONSOLE =>
3
2
1
"1<2<3<hello>>>"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment