Kaushani RC
Kaushani RC

Reputation: 43

higher order function in javascript

I am reading the book Eloquent to learn javascript. I came across this example

function unless(test, then) {
    if (!test) then();
}

function repeat(times, body) {
    for (var i = 0; i < times ; i++) body(i);
}

repeat(3, function (n) {
    unless(n % 2, function () {
        console.log(n, " is even ");
    });
});

// → 0 is even
// → 2 is even

I know functions can be passed as arguments and can be inside one another. But are then() and body() functions? Where are they defined? What's the value of n?

Upvotes: 0

Views: 134

Answers (3)

ismnoiet
ismnoiet

Reputation: 4159

In your example here you have the signature of the unless function:

function unless ( test , then ) {
// code here
}

both test and then are called parameters and change depending on the arguments you pass when you call unless

for example :

 function first(){}
 function second(){}

 unless(first,second)

means that test inside the unless is a function cause test is replaced with first function and then is replaced with the second function when calling unless

// unless(test,then) => unless(first,second)
//         |     |              |        |
//         |-----|---------------        |     
//               ------------------------|

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816364

but are then() and body() functions?

then and body are parameters. Their values depend on the values that are passed to unless and repeat. So in order to determine what the values are (i.e. whether they are functions or not) you need to look at where unless and repeat are called and which values are passed to them.

Keep in mind that, in general, repeat and unless can be called multiple times. The values of then and body can be different for every function call, but they will have to be of the type (number, string, function, ...) the function expects, otherwise it won't work correctly.

Where are they defined?

In your example, repeat and unless are called here:

repeat(3, function (n) {
    unless(n % 2, function () {
        document.write(n + " is even<br>");
    });
});

You can see that the second arguments passed to repeat and unless are indeed functions.

What's the value of n?

Lets have a look where the function is called. The function is passed as second argument to repeat. repeat uses body to refer to the second argument and it laters calls it as body(i) in a loop. i will have the values 0 to times - 1. times is the first argument passed to repeat which in your example is 3. So the function will be called multiple (three) times, receiving the values 0, 1 and 2.

Upvotes: 3

Neta Meta
Neta Meta

Reputation: 4047

Here is a little explanation.

function unless ( test , then ) {
  if (! test ) then () ;
}
function repeat ( times , body ) {
  for ( var i = 0; i < times ; i ++) body ( i ) ;
}

// in here you also pass 2 params to repeat.
// 3 and an anonymous function, the function is the argument 'body' in repeat
repeat (3 , function ( n ) {
  // as you can see here - you pass 2 params to unless, 
  // 1 is the result of n %2, and an anonymous  function, the function is the argument
  // "then" in unless.
  unless ( n % 2 , function () {
    console . log (n , " is even ") ;

  }) ;
}) ;

the value of n is changing from 0 to 3 it, its going to be 0,1,2

Also since your a beginner i would suggest using https://jsfiddle.net/ to test stuff out.

Upvotes: 0

Related Questions