Guest User

Untitled

a guest
Sep 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. const _makeProxy = f => new Proxy(f, {
  2. get (fn, prop) {
  3. return U[prop] && U[prop].bind({ hold: fn });
  4. }
  5. });
  6.  
  7. const U = {
  8. add: function (x) {
  9. return _makeProxy(
  10. y => (
  11. this.hold && (y = this.hold(y)),
  12. x + y
  13. ));
  14. },
  15. multiply: function (x) {
  16. return _makeProxy(
  17. y => (
  18. this.hold && (y = this.hold(y)),
  19. x * y
  20. ));
  21. },
  22. eqBy: function (f) {
  23. return x => _makeProxy(
  24. y => (
  25. this.hold && (y = this.hold(y)),
  26. f(x) === f(y)
  27. ));
  28. }
  29. };
  30.  
  31. console.log(( U.multiply(3).add(4) )(2)); // 10
  32. console.log(( U.add(3).multiply(4).multiply(3) )(2)); // 60
  33. console.log(
  34. U
  35. .add(3)
  36. .multiply(4)
  37. .multiply(3)
  38. .eqBy(Math.abs)(60)
  39. (2)); // true
Add Comment
Please, Sign In to add comment