Skip to content

Instantly share code, notes, and snippets.

@venomnert
Last active June 30, 2017 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venomnert/44270bcfb19bd968de7577999ee2f2d3 to your computer and use it in GitHub Desktop.
Save venomnert/44270bcfb19bd968de7577999ee2f2d3 to your computer and use it in GitHub Desktop.
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