Skip to content

Instantly share code, notes, and snippets.

@MKRhere
Last active January 9, 2019 19:21
Show Gist options
  • Save MKRhere/33607ad776e7fad82688776ca6801e0a to your computer and use it in GitHub Desktop.
Save MKRhere/33607ad776e7fad82688776ca6801e0a to your computer and use it in GitHub Desktop.
Chaining pipe
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