Skip to content

Instantly share code, notes, and snippets.

@nobitagit
Last active June 6, 2018 21:20
Show Gist options
  • Select an option

  • Save nobitagit/99c427e5ec161e0d48263e5be5849e65 to your computer and use it in GitHub Desktop.

Select an option

Save nobitagit/99c427e5ec161e0d48263e5be5849e65 to your computer and use it in GitHub Desktop.
Pipe implementation in js
// goal is to make this work
pipe(
function a(c) {
return c + 1;
},
a => a + 1,
)(3); // 5
// 1st stage, nope...
const pipe = (...args) => {
return args.map(fn => fn())
}
// 2nd stage, not quite..
const pipe = (...args) => {
return args.reduce((acc, fn) => {
const ret = fn(acc);
return ret;
}, null);
}
// 3rd stage, it works!
const pipe = (...args) => arg => {
return args.reduce((acc, fn) => {
const ret = fn(acc);
return ret;
}, arg);
}
// 4th stage, cleanup
const pipe = (...args) => arg => {
return args.reduce((acc, fn) => fn(acc), arg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment