Skip to content

Instantly share code, notes, and snippets.

@KMNowak
Last active October 19, 2019 21:26
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 KMNowak/60ab9ddd46d079e5fabf148cd1072487 to your computer and use it in GitHub Desktop.
Save KMNowak/60ab9ddd46d079e5fabf148cd1072487 to your computer and use it in GitHub Desktop.
const addTwo = prop => {
console.log(`In addTwo, prop: ${prop}`)
return prop + 2
}
const addTen = prop => {
console.log(`In addTen, prop: ${prop}`)
return prop + 10
}
const squareResult = prop => {
console.log(`In squareResult, prop: ${prop}`)
return Math.sqrt(prop, 2)
}
const multiplyByTen = prop => {
console.log(`In multiplyByTen, prop: ${prop}`)
return prop * 10
}
// not cool
const withTwo = addTwo(2)
const withTen = addTen(withTwo)
const squared = squareResult(withTen)
multiplyByTen(squared) // (2)
// not any better
multiplyByTen(squareResult(addTen(addTwo(2)))) // (3)
const composer = (functions, prop) => functions.reduceRight((acc, fun) => fun(acc), prop)
composer([
multiplyByTen,
squareResult,
addTen,
addTwo
], 2) // (4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment