Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Created July 10, 2019 19:47
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 gdyrrahitis/15f7830a3e2313423d40f4dec2ba3407 to your computer and use it in GitHub Desktop.
Save gdyrrahitis/15f7830a3e2313423d40f4dec2ba3407 to your computer and use it in GitHub Desktop.
const keyboard = { name: 'Keyboard', inStock: true, unitPrice: 125.5, quantity: 2 };
const laptop = { name: 'Laptop', inStock: true, unitPrice: 920.99, quantity: 1 };
const cables = { name: 'HDMI Cables', inStock: true, unitPrice: 12.99, quantity: 4 };
const headphones = { name: 'Headphones', inStock: false, unitPrice: 40, quantity: 1 };
const order = [keyboard, laptop, cables, headphones];
const coupon = 0.2;
const inStock = (product) => product.inStock;
const productPrice = (product) => product.unitPrice * product.quantity;
const applyCoupon = (customerCoupon) => (total) => total - (total * customerCoupon);
const sum = (total, price) => total + price;
const result = order
|> i => i.filter(inStock)
|> i => i.map(productPrice)
|> i => i.reduce(sum, 0)
|> applyCoupon(coupon) // partial function application
|> t => t.toFixed(2);
console.log(`Order total is $${result} by applying a 20% discount`);
// prints: Order total is $979.16 by applying a 20% discount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment