Created
August 21, 2018 21:35
-
-
Save ordoghl/b3ba6cb3aa5448a0c44592cf76cc00e3 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 f1 = x => x + 1 | |
const f2 = x => x - 2 | |
const f3 = x => x * 3 | |
const f4 = x => x / 2 | |
const add = (x, y) => x + y | |
// const g = x => f2(f3(f1(x))) | |
// Array.prototype.get = function() { | |
// return this[0] | |
// } | |
// const g = x => [x] | |
// .map(f1) | |
// .map(f3) | |
// .map(f2) | |
// .get() | |
// // .find(x => true) | |
// function compose(f, g) { | |
// return function (...params) { | |
// return f(g(...params)) | |
// } | |
// } | |
function compose(...fs) { | |
const [g, f, ...rest] = fs.reverse() | |
if (g && !f) { | |
return g | |
} | |
if (rest.length === 0) { | |
return function(...params) { | |
return f(g(...params)) | |
} | |
} | |
const [fr, ...rrest] = rest | |
return compose( | |
...rrest.reverse(), | |
function(...params) { | |
return fr(f(g(...params))) | |
} | |
) | |
} | |
const g = compose(f4, f2, f3, f1, add) | |
console.log(g(8, 2)) // 31 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment