Skip to content

Instantly share code, notes, and snippets.

@venomnert
Created June 30, 2017 19:06
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/c0e26f5b5150c92390c41c3120035f3a to your computer and use it in GitHub Desktop.
Save venomnert/c0e26f5b5150c92390c41c3120035f3a to your computer and use it in GitHub Desktop.
// First calculate the total cost of the pizza including tax
const calcTotalWithTax = pizzaCost => pizzaCost * 1.13 // Here in Toronto, tax is 13%
// Then calculate the cost of pizza for two people
const costForTwo = itemCost => Math.round(itemCost/2 * 100) / 100
// implement pipe function
// the pipe function accepts only two
// operations
const pipe = (op1, op2) => {
// return a function that bundles all
// operations into a single operation
return (arg) => {
// first we invoke op1 with the passed
// arg and save its output
const result1 = op1(arg);
// invoke op2 by calling it with
// saved output of the op1 and return the result of op2
return op2(result1);
}
}
//Let's try out the pipe function
const splitTotalCost = pipe(calcTotalWithTax, costForTwo)
console.log(`$5 pizza (plus tax), would cost ${splitTotalCost(5)} between two people`); // 2.83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment