Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ex 1
  2. console.log("Exercise 1");
  3.  
  4. add = (a, b) => a + b;
  5. mul = (a, b) => a * b;
  6.  
  7. console.log(add(4, 7));
  8. console.log(mul(4, 7));
  9.  
  10. //ex 2
  11. console.log("Exercise 2");
  12.  
  13. const identify = function(argument){
  14.     return function(){
  15.         return argument;
  16.     }
  17. };
  18.  
  19. let idf = identify(5);
  20.  
  21. console.log(idf());
  22.  
  23. //ex 3
  24. console.log("Exercise 3");
  25.  
  26. let addf = function(a){
  27.     return function(b){
  28.         return a + b;
  29.     }
  30. }
  31.  
  32. console.log(addf(4)(3));
  33.  
  34. //ex 4
  35. console.log("Exercise 4");
  36.  
  37. const applyf = function(func){
  38.     return function(a){
  39.         return function(b){
  40.             return func(a, b);
  41.         }
  42.     }
  43. }
  44.  
  45. addf = applyf(add);
  46. console.log(addf(10)(3));
  47.  
  48. addf = applyf(mul);
  49. console.log(addf(10)(3));
  50.  
  51. //ex 5
  52. console.log("Exercise 5");
  53.  
  54. const curry = function(func, first){
  55.     return function(second){
  56.         return func(first, second);
  57.     }
  58. }
  59.  
  60. const add3 = curry(add, 3);
  61.  
  62. console.log(add3(2));
  63. console.log(curry(mul, 5)(10));
  64.  
  65. //ex 6
  66. console.log("Exercise 6");
  67.  
  68. const twice = function(func){
  69.     return function(arg){
  70.         return func(arg, arg);
  71.     }
  72. }
  73.  
  74. const double = twice(add);
  75. const square = twice(mul);
  76.  
  77. console.log(double(12));
  78. console.log(square(12));
  79.  
  80. //ex 7
  81. console.log("Exercise 7");
  82.  
  83. const composeu = function(first, second){
  84.     return function(arg){
  85.         return second(first(arg));
  86.     }
  87. }
  88.  
  89. console.log(composeu(double, square)(3));
  90.  
  91. //ex 8
  92. console.log("Exercise 8");
  93.  
  94. function composeb(first, second){
  95.     return function(a, b, c){
  96.         return second(first(a, b), c);
  97.     }
  98. }
  99.  
  100. console.log(composeb(add, mul)(2, 3, 5));
  101.  
  102. //ex 9
  103. console.log("Exercise 9");
  104.  
  105. const counterf = function(val){
  106.     return {
  107.         _value : val,
  108.         inc : function(){
  109.             this._value++;
  110.             return this._value;
  111.         },
  112.         dec : function(){
  113.             this._value--;
  114.             return this._value;
  115.         }
  116.     }
  117. }
  118.  
  119. const counter = counterf(10);
  120. console.log(counter.inc());
  121. console.log(counter.dec());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement