Skip to content

Instantly share code, notes, and snippets.

@swazza
Created May 10, 2017 10:53
Show Gist options
  • Save swazza/27eab38ec29d055c6338b0d1070ab79d to your computer and use it in GitHub Desktop.
Save swazza/27eab38ec29d055c6338b0d1070ab79d to your computer and use it in GitHub Desktop.
Pipeline Processor Using Reduce
function add(i) {
return i + 1;
}
function mul(i) {
return i * 2;
}
function sqr(i) {
return i * i;
}
const pipe = (val) => (stages) => stages.reduce((input, stage) => stage(input), val)
let stages = [add, mul, sqr]
let result = pipe(1)(stages)
console.log(result) // 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment