Skip to content

Instantly share code, notes, and snippets.

@umcconnell
Created May 26, 2019 16:39
Show Gist options
  • Save umcconnell/702beb82b5b14208bbe9ed2903e02857 to your computer and use it in GitHub Desktop.
Save umcconnell/702beb82b5b14208bbe9ed2903e02857 to your computer and use it in GitHub Desktop.
Curried JS pipe function
/**
* JS Curried Pipe Function
*
* @param {array} arr input function chain
* @example
* let calculateCost = pipe([
* products => products.reduce(plus), // Total Price
* price => price * 1.2, // Tax
* price => price - 50 // Discount
* ]);
* calculateCost([3, 5, 2.5, 10, 40])
* // => 22.599999999999994
* @returns {(start:*) => *} a function accepting an initial argument
*/
let pipe = arr => start => arr.reduce((acc, curr, i) => curr(acc, i), start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment