2

Here's the example code I'm struggling with:

function greaterThan(x) {
  return function(y) {
    return y > x;
  };
}

var greaterThanTen = greaterThan(10);
show(greaterThanTen(9));

Is there a way to put it in math terms or follow the flow or something? I don't know why 10 is x and 9 is y.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78

5 Answers5

3

In the line:

var greaterThanTen = greaterThan(10);

You are assinging the variable x to the value 10 and then you store the function in the greaterThanTen Variable to be called later. this means that:

greaterThanTen = function(y) {
    return y > 10;
};

So when you do:

greaterThanTen(9);  #y = 9

You are calling:

return 9 > 10;
Gazler
  • 83,029
  • 18
  • 279
  • 245
3

This function doesn't call a function, it returns a function.

This code is creating a new unary function where the original binary (greater than) operator's right-hand operand is prebound to a specific value.

In lambda calculus this binding is known as currying.

In Javascript the binding happens because the supplied value of the parameter x in greaterThan is permanently retained in the scope of the inner function (or "closure") that is returned.

So, when you call:

var greaterThanTen = greaterThan(10);

what you now have is a new function (named greaterThanTen) which always compares its single parameter to the bound value of 10.

Hence:

greaterThanTen(9);

will return false.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • That's cool. I have to read about currying to see how it works before I can talk with you about it, though. – Wolfpack'08 Oct 08 '11 at 14:41
  • Can anyone tell me what `→` means in `(X →Y) →Z`? – Wolfpack'08 Oct 08 '11 at 14:53
  • Interesting. Thanks for the edit. Nice resources. I don't see how 10 is being held, but it's interesting to know that it is.... Is it held just because of the environment? – Wolfpack'08 Oct 08 '11 at 14:59
  • 1
    +1, but maybe you should explain how js functions creates scope to help the OP understand why it works the way it does. a good link for reference is here: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting – Martin Jespersen Oct 08 '11 at 15:30
  • @MartinJespersen I added some more text about scope – Alnitak Oct 08 '11 at 19:09
1
  • Create greaterThan(10)
  • Create function:
    function(y){return y > x}
  • return function.

So, when you call greaterThan(10), the function returns a function whose local variable x is set to 10.

var greaterThanTen = greaterThan(10) equals:
var greaterThanTen = function(y){return y > 10};

To finish, greaterThanTen(9) is called, which equals 9 > 10, which is false.

Rob W
  • 341,306
  • 83
  • 791
  • 678
1

The only thing that greaterThan does is to set a value for x in

function(y) {return (y>x);}

and store the resulting function in a variable name, in this case greaterThanTen, now with the contents

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

Calling greaterThanTen(9) is the same as looking at

function(y = 9) {return (y>10);}

which is the same as

function(y = 9) {return (9>10);}

which is false. Hence false is returned.

Edit:

Example of function that returns a function here: https://i.stack.imgur.com/3iKQA.jpg (x and y is switched around in y>x)

Namaste

abcde123483
  • 3,885
  • 4
  • 41
  • 41
  • What confuses me, though, is that 9 is held when 10 seems to be passed as a new value. – Wolfpack'08 Oct 08 '11 at 14:55
  • 1
    It's easier with pictures: If you are used to picturing functions as small flow-machines like me, just picture a small flow-machine returning another small flow-machine: http://i.imgur.com/aiHSH.jpg I switched around x and y in y>x but you get the idea hopefully :) – abcde123483 Oct 08 '11 at 15:13
0

The greaterThanTen variable represents a function taking one argument and returning a boolean value whether this argument is greater then 10.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928