7

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

5 Answers 5

7

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
Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

2

greaterThan is a function that returns another function as a result, m is a paraterer of that returned function. So in your code: var greaterThan10 = function(m) { return m > 10; };

and console.log(greaterThan10(11)); is the same as console.log(11 > 10);

Comments

2

when you call function greaterThan it returns another function but not a number of float. Inner function knows about n because it inside function greaterThan.

Because wrapper function returns another function you can call second one like this

var result = greaterThan(10)(11);

first argument 10 will be used for wrapper function but result is function so you can pass arguments for inner function immediately. this is possible only if you have return function(){...}

you can try something like

var a = function (x){
         return function(y){
            return function(z){
              return x*y*z;
            }
          }
        }
var result = a(5)(3)(8); 

Comments

1

You have two functions there.

The n comes from when the first function is called.

The m comes from when the second function (which is the return value of the first function) is called.

greaterThan10 = greaterThan(10);
//                          ^^  n
greaterThan10(11))
//  ^^ returned function
//            ^^ m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.