Last active
January 9, 2019 19:21
-
-
Save MKRhere/33607ad776e7fad82688776ca6801e0a to your computer and use it in GitHub Desktop.
Chaining pipe
This file contains hidden or 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 _makeProxy = f => new Proxy(f, { | |
get (fn, prop) { | |
return U[prop] && U[prop].bind({ hold: fn }); | |
} | |
}); | |
const U = { | |
add: function (x) { | |
return _makeProxy( | |
y => ( | |
this.hold && (y = this.hold(y)), | |
x + y | |
)); | |
}, | |
multiply: function (x) { | |
return _makeProxy( | |
y => ( | |
this.hold && (y = this.hold(y)), | |
x * y | |
)); | |
}, | |
eqBy: function (f) { | |
return x => _makeProxy( | |
y => ( | |
this.hold && (y = this.hold(y)), | |
f(x) === f(y) | |
)); | |
} | |
}; | |
console.log(( U.multiply(3).add(4) )(2)); // 10 | |
console.log(( U.add(3).multiply(4).multiply(3) )(2)); // 60 | |
console.log( | |
U | |
.add(3) | |
.multiply(4) | |
.multiply(3) | |
.eqBy(Math.abs)(60) | |
(2)); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment