Guest User

Untitled

a guest
Jun 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. function add(a, b) {
  2. return a + b;
  3. }
  4.  
  5. function sub(a, b) {
  6. return a - b;
  7. }
  8.  
  9. function mul(a, b) {
  10. return a * b;
  11. }
  12.  
  13. add(4, 5); /*? */
  14. sub(12, 4); /*? */
  15. mul(3, 4); /*? */
  16.  
  17. function identityf(arg) {
  18. return function() {
  19. return arg;
  20. }
  21. }
  22.  
  23. var three = identityf(3);
  24. three(); /*? */
  25.  
  26. function addf(a) {
  27. return function(b) {
  28. return a + b;
  29. }
  30. }
  31.  
  32. addf(3)(4); /*? */
  33.  
  34. function liftf(fn) {
  35. return function(a) {
  36. return function(b) {
  37. return fn(a, b); /*? */
  38. }
  39. }
  40. }
  41.  
  42. var addf = liftf(add);
  43. addf(3)(4); /*? */
  44. liftf(mul)(3)(4); /*? */
  45.  
  46. function curry(fn, arg) {
  47. return function(x) {
  48. return fn(arg, x); /*? */
  49. }
  50. }
  51.  
  52. var add3 = curry(add, 3); /*? */
  53. add3(4); /*? */
  54. curry(mul, 5)(6);/*? */
  55.  
  56. // var inc = addf(1); /*? */
  57. // var inc = liftf(add)(1);
  58. var inc = curry(add, 1);
  59. inc(5); /*? */
  60. inc = liftf(add)(1); /*? */
  61. inc(inc(5)); /*? */
  62.  
  63. function twice(fn) {
  64. return function(x) {
  65. return fn(x, x); /*? */
  66. }
  67. }
  68.  
  69. add(11, 11); /*? */
  70. var doubl = twice(add);
  71. doubl(11); /*? */
  72.  
  73. function reverse(fn) {
  74. return function(...args) {
  75. return fn(...args.reverse());
  76. }
  77. }
  78.  
  79. var bus = reverse(sub); /*? */
  80. bus(3, 2); /*? */
  81.  
  82. function composeu(fn, fn1) {
  83. return function(x) {
  84. return fn1(fn(x));
  85. }
  86. }
  87.  
  88. composeu(doubl, mul)(5); /*? */
Add Comment
Please, Sign In to add comment