Skip to content

Instantly share code, notes, and snippets.

@thephilip
Last active June 22, 2023 13:09
Show Gist options
  • Save thephilip/9e64ec340db26ffc2f4d4b5c3c696fd4 to your computer and use it in GitHub Desktop.
Save thephilip/9e64ec340db26ffc2f4d4b5c3c696fd4 to your computer and use it in GitHub Desktop.
const map = fn => array => array.map(fn);
const multiply = x => y => x * y;
const pluck = key => object => object[key];
const discount = multiply(0.98);
const tax = multiply(1.0925);
const custReq = request({
headers: { 'X-Custom': 'mykey' }
});
// 3 iterations
customReq({ url: '/cart/items' })
.then(map(pluck('price')))
.then(map(discount))
.then(map(tax));
// with composition...1 iteration
customReq({ url: '/cart/items' })
.then(map(
compose(
tax, // 3
discount, // 2
pluck('price') // 1
)
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment