Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Higher order javascript functions

can someone explain to me what is going on in the following code. The function is receiving n as parameter, so where is the m coming from? The whole code is confusing.. if someone can explain?

  function greaterThan(n) {
  return function(m) { return m > n; };
  }
   var greaterThan10 = greaterThan(10);
   console.log(greaterThan10(11));
   // → true
like image 719
whatever Avatar asked May 03 '15 21:05

whatever


People also ask

What are the higher-order functions in JavaScript?

Let’s take the example of the Javascript Array object which has a lot of higher-order functions which some of them like sort, map, reduce, and filter we will cover these here, but before that let’s go through few javascript concepts that are very important for our understanding of the Higher-order functions later.

What is a higher order function called?

Higher-order functions. Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions. Since we have already seen that functions are regular values, there is nothing particularly remarkable about the fact that such functions exist.

What is higher-order arrow function in JavaScript?

Higher-Order Arrow function implies using arrow functions (in ES6) along with Higher-Order functions. In general way, the programmer instructs on how to perform the function rather than what is needed which increases the length of code and making it error prone.

How do you use higher order methods in arrays?

Arrays provide a number of useful higher-order methods. You can use forEach to loop over the elements in an array. The filter method returns a new array containing only the elements that pass the predicate function. Transforming an array by putting each element through a function is done with map.


2 Answers

This is exhibiting a functional programming technique called currying. (related also to partial function appliction)

Greater than > usually takes 2 arguments (one on the left and one on the right). This is a way to feed one at a time.

It might be easier to see what is happening if you call it inline:

greaterThan(10)(11);

As you can see from the example above, the 10 gets passed in for the n parameter and then the 11 gets passed in for the m parameter.

The first application that passes the 10 outputs a function that looks like this:

function(m) { return m > 10; };

This is the first application in the partial application.

From there it is easy to see how the 11 is passed in to get the final result.

So, to break it down:

function greaterThan(n) {
   return function(m) { return m > n; };
}

//var greaterThan10 = greaterThan(10); -- is equivalent to:
var greaterThan10 = function(m) { return m > 10; };

console.log(greaterThan10(11));  //--> true
like image 54
Davin Tryon Avatar answered Oct 31 '22 05:10

Davin Tryon


m is 11, passed in during the second call.

When you call greaterThan(10), it returns a new function that looks like:

function(m) {
    return m > 10;
}

which is then saved as greaterThan10. This is called currying.

like image 38
Mathletics Avatar answered Oct 31 '22 07:10

Mathletics