Last active
June 30, 2017 23:44
-
-
Save venomnert/44270bcfb19bd968de7577999ee2f2d3 to your computer and use it in GitHub Desktop.
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
const _pipe = (a, b) => (arg) => b(a(arg)); | |
const pipe = (...ops) => ops.reduce(_pipe) | |
const calcTotalWithTax = pizzaCost => pizzaCost * 1.13 | |
const costForTwo = itemCost => Math.round(itemCost/2 * 100) / 100 | |
const cadToUSD = (cad) => Math.round(cad * 0.753653*100)/100; | |
const costPerPersonUsd = pipe(calcTotalWithTax, costForTwo, cadToUSD); | |
console.log(`You have to pay $ ${costPerPersonUsd(5)} US.`); // You have to pay $2.13 in usd | |
// Behind the scene: | |
const costPerPersonUsd = (calcTotalWithTax, costForTwo, cadToUSD) | |
1. _pipe(calcTotalWithTax, costForTwo) `would be` (args) => costForTwo(calcTotalWithTax(arg)) // We will call the bundled function pipe1 | |
2a. _pipe((args) => costForTwo(calcTotalWithTax(arg), cadToUSD)` would be` (arg) => cadToUsd( costForTwo(calcTotalWithTax(arg)) | |
// or _pipe(pipe1, cadToUSD) would become (arg) => cadToUsd( costForTwo(calcTotalWithTax(arg)) | |
3. costPerPersonUsd `would be` (args) => cadToUsd( costForTwo(calcTotalWithTax(arg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment