Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ex 1
- console.log("Exercise 1");
- add = (a, b) => a + b;
- mul = (a, b) => a * b;
- console.log(add(4, 7));
- console.log(mul(4, 7));
- //ex 2
- console.log("Exercise 2");
- const identify = function(argument){
- return function(){
- return argument;
- }
- };
- let idf = identify(5);
- console.log(idf());
- //ex 3
- console.log("Exercise 3");
- let addf = function(a){
- return function(b){
- return a + b;
- }
- }
- console.log(addf(4)(3));
- //ex 4
- console.log("Exercise 4");
- const applyf = function(func){
- return function(a){
- return function(b){
- return func(a, b);
- }
- }
- }
- addf = applyf(add);
- console.log(addf(10)(3));
- addf = applyf(mul);
- console.log(addf(10)(3));
- //ex 5
- console.log("Exercise 5");
- const curry = function(func, first){
- return function(second){
- return func(first, second);
- }
- }
- const add3 = curry(add, 3);
- console.log(add3(2));
- console.log(curry(mul, 5)(10));
- //ex 6
- console.log("Exercise 6");
- const twice = function(func){
- return function(arg){
- return func(arg, arg);
- }
- }
- const double = twice(add);
- const square = twice(mul);
- console.log(double(12));
- console.log(square(12));
- //ex 7
- console.log("Exercise 7");
- const composeu = function(first, second){
- return function(arg){
- return second(first(arg));
- }
- }
- console.log(composeu(double, square)(3));
- //ex 8
- console.log("Exercise 8");
- function composeb(first, second){
- return function(a, b, c){
- return second(first(a, b), c);
- }
- }
- console.log(composeb(add, mul)(2, 3, 5));
- //ex 9
- console.log("Exercise 9");
- const counterf = function(val){
- return {
- _value : val,
- inc : function(){
- this._value++;
- return this._value;
- },
- dec : function(){
- this._value--;
- return this._value;
- }
- }
- }
- const counter = counterf(10);
- console.log(counter.inc());
- console.log(counter.dec());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement