Skip to content

Instantly share code, notes, and snippets.

@toboid
Last active July 26, 2017 13:41
Show Gist options
  • Save toboid/ddebbb5fd819bafcb59ec60f2f3d5012 to your computer and use it in GitHub Desktop.
Save toboid/ddebbb5fd819bafcb59ec60f2f3d5012 to your computer and use it in GitHub Desktop.
Pipe in JS
function pipe (...funcs) {
if (!funcs.length) {
throw new Error('pipe requires at least 1 argument');
}
return (...args) => funcs.slice(1).reduce((accum, func) => func(accum), funcs[0](...args))
}
// Usage
pipe((a, b) => a + b)(1, 2); // => 3
pipe(
(x, y) => x + y,
(x) => x + 1
)(1, 2); // => 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment