Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created March 13, 2020 05:51
Show Gist options
  • Save jsmanifest/eaa4d24ecc44f69da5f5481c41b2f3c9 to your computer and use it in GitHub Desktop.
Save jsmanifest/eaa4d24ecc44f69da5f5481c41b2f3c9 to your computer and use it in GitHub Desktop.
const compose = (...fns) => (arg) =>
fns.reduceRight((acc, fn) => (fn ? fn(acc) : acc), arg)
const add = (num1) => (num2) => num1 + num2
const multiply = (num1) => (num2) => num1 * num2
const subtract = (num1) => (num2) => num1 - num2
const composedOperations = compose(add(5), multiply(2), subtract(3))
const compute = (arr, initialNum = 0) =>
arr.reduce((acc, val) => composedOperations(acc), initialNum)
console.log(compute([-10, 25, 55, 22], 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment