Last active
June 29, 2018 07:27
-
-
Save oamaok/16da94f16e575d46f30dbc996c421982 to your computer and use it in GitHub Desktop.
function composition with `.` in javascript
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 accumulator = (fns) => { | |
const fn = () => {}; | |
fn.fns = fns; | |
return fn; | |
} | |
const handler = { | |
get: (target, fn) => { | |
return new Proxy(accumulator([...target.fns, fn]), handler); | |
}, | |
apply: (target, thisArg, args) => { | |
return target.fns.reduce((acc, fn) => x => acc(window[fn](x)), x => x)(args); | |
}, | |
} | |
const λ = new Proxy(accumulator([]), handler); | |
window.duplicate = x => [x, x]; | |
window.multiplyBy = a => b => a * b; | |
window.multiplyBy2 = multiplyBy(2); | |
console.log((λ . duplicate . multiplyBy2)(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment