Skip to content

Instantly share code, notes, and snippets.

@oamaok
Last active June 29, 2018 07:27
Show Gist options
  • Save oamaok/16da94f16e575d46f30dbc996c421982 to your computer and use it in GitHub Desktop.
Save oamaok/16da94f16e575d46f30dbc996c421982 to your computer and use it in GitHub Desktop.
function composition with `.` in javascript
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