Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Last active August 30, 2019 13:11
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 anthonybrown/41e17584007c3be7776bd411f88bb4df to your computer and use it in GitHub Desktop.
Save anthonybrown/41e17584007c3be7776bd411f88bb4df to your computer and use it in GitHub Desktop.
example of using curried functions
/** Store Data */
const prices = {
game: 59.95,
nintendo: 399.99,
controller: 49.99,
xbox: 299.99,
ps1: 349.99
}
const basket = [
{ name: 'nintendo', qty: 1 },
{ name: 'controller', qty: 3 },
{ name: 'game', qty: 4 },
]
const TAX_RATE = 0.075
/** Utils */
function curry(fn) {
return (...args) => {
if (args.length >= fn.length) {
return fn.apply(null, args)
}
return curry(fn.bind(null, ...args))
}
}
const trace = curry((label, val) => {
console.log(label, val)
return val
})
const sum = (a, b) => a + b;
const multi = curry((m, n) => m * n);
const isObj = (mixed) => !!mixed && typeof mixed === 'object';
const get = curry((obj, name) => isObj(obj) ? obj[name] : undefined)
const prop = curry((name, obj) => get(obj, name))
const zip = (...arrays) => {
return new Array(arrays[0].length).fill([])
.map((acc, index) => {
return arrays.map(item => item[index])
})
}
const _apply = curry((fn, items) => fn.apply(null, items))
// console.log(zip([1,2,3], ['a','b','c']));
// [1,2,3] [a, b, c] ---> [[1, a], [2, b], [3, c]]
/** APP */
const getPrice = get(prices)
const getTax = multi(TAX_RATE)
const items = basket.map(prop('name'))//.map(trace('item --> '))
const priceOfItems = items.map(getPrice)//.map(trace('price --> '))
const quantities = basket.map(prop('qty'))//.map(trace('qty --> '))
const pricesQuantities = zip(priceOfItems, quantities)
const subtotals = zip(priceOfItems, quantities).map(_apply(multi))
const subtotal = subtotals.reduce(sum, 0)
const tax = getTax(subtotal)
const total = sum(subtotal, tax)
trace('subtotal: ', subtotal)
trace('tax: ', tax)
trace('total: ', total)
// pricesQuantities.map(_apply(multi)).map(trace('subtotals -->')
// console.log(pricesQuantities)
// this is ugly
// pricesQuantities
// .map(items => multi.apply(null, items))
// .map(trace('subtotal --> '))
// console.log(getPrice('banana'))// undefined
// console.log(getPrice('game')) // 399.99
// console.log(priceOfItems)// [399.99, 49.99, 59.95]
@anthonybrown
Copy link
Author

This is still not as pure in the functional paradigm as we can go because I am doing things one line at a time instead of composing the functions together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment