Created
May 26, 2019 16:39
-
-
Save umcconnell/702beb82b5b14208bbe9ed2903e02857 to your computer and use it in GitHub Desktop.
Curried JS pipe function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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