Last active
October 19, 2019 21:26
-
-
Save KMNowak/60ab9ddd46d079e5fabf148cd1072487 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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