Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Higher-order functions are functions that take other functions as arguments, or return functions as their results. They are a powerful concept in JavaScript that can make your code more flexible and reusable.

  1. Understanding Higher-Order Functions:

    • A higher-order function is a function that operates on other functions.
    • It can take a function as an argument or return a function as its result.
    • Higher-order functions can help reduce code duplication and improve code readability.
  2. Creating Higher-Order Functions:

    • To create a higher-order function, you simply need to define a function that takes another function as an argument, or returns a function as its result.

    • Here's an example of a higher-order function that takes a function as an argument:

         function operateOnArray(arr, operatorFn) {
            return arr.map(operatorFn);
         }

      In this example, the operateOnArray function takes an array arr and a function operatorFn as arguments. It then applies the operatorFn function to each element in the array using the map function, and returns the resulting array.

  3. Using Higher-Order Functions:

    • To use a higher-order function, you can simply pass in a function as an argument, or store the returned function in a variable and use it later.
    • Here's an example of using the operateOnArray function with a callback function:
         function double(num) {
           return num * 2;
         }
      
         const arr = [1, 2, 3, 4];
         const doubledArr = operateOnArray(arr, double);
      
         console.log(doubledArr); // [2, 4, 6, 8]
      In this example, we define a double function that takes a number and returns its double. We then pass this function as an argument to the operateOnArray function, which applies it to each element in the array and returns a new array with the doubled values.
  4. Function Composition:

    • Function composition is the process of combining two or more functions to create a new function.
    • This can be useful for creating more complex functions out of smaller, reusable functions.
    • Here's an example of function composition:
       function add(a, b) {
         return a + b;
       }
       function multiply(a, b) {
         return a * b;
       }
    
       function compose(f, g) {
         return function(x) {
           return f(g(x));
         };
       }
    
       const addAndMultiply = compose(multiply.bind(null, 2), add);
    
       console.log(addAndMultiply(3, 4)); // 14

    In this example, we define two functions add and multiply. We then define a compose function that takes two functions f and g as arguments, and returns a new function that applies g to its argument, and then applies f to the result of that. Finally, we use the compose function to create a new function addAndMultiply that first adds its two arguments, and then multiplies the result by 2.

  5. Currying:

    • Currying is the process of transforming a function that takes multiple arguments into a series of functions that each take a single argument.

    • This can be useful for creating more flexible and reusable functions.

    • Here's an example of currying:

         function add(a, b){
           return a + b;
         }
      
         function curry(fn) {
           return function(a) {
             return function(b) {
               return fn(a, b);
             };
           };
          }
      
         const curriedAdd = curry(add);
      
         console.log(curriedAdd(2)(3)); // 5
    • In this example, we define an add function that takes two arguments and returns their sum. We then define a curry function that takes a function fn as an argument, and returns a new function that takes its arguments one at a time.

    • Finally, we use the curry function to create a new function curriedAdd that takes its arguments one at a time, and then applies the add function to them.