1

javascript book "eloquent javascript"

function negate(func) {
return function(x) {
return !func(x);
 };
}
var isNotNaN = negate(isNaN);
show(isNotNaN(NaN));

someone explain it and as title of question says what is higher-order function, what does this code do ?

4
  • a read stackoverflow.com/questions/13212940/… Commented Mar 31, 2013 at 10:05
  • Is your question what a higher order function is or what this specific function is doing? I think the function name and the following application already explains what it is doing, doesn't it? What a higher order function is, is explained on Wikipedia: en.wikipedia.org/wiki/Higher_order_function. Commented Mar 31, 2013 at 10:07
  • i dont understand this function can u explain it ? Commented Mar 31, 2013 at 10:11
  • javascript.github.com Commented Mar 31, 2013 at 10:18

2 Answers 2

1

When most people think of functions they accept objects or values as parameters and similarly return an object or value, such as function addTwoNumbers(int x, int y).

In mathematics and computer science, a "higher-order function" is just like any other function, except that in addition to arguments that are values it can also accept a function as an argument.

...that's all a higher-order function is, really :)

In the example you posted, negate is a higher-order function because it has a parameter func which is a function (or rather, assigned to a function).

negate goes further: it doesn't merely call func and negate its result, instead it returns an anonymous function (that's the inner return function(x) bit).

So the isNotNaN variable then has the type (and value) of that earlier anonymous function.

Sign up to request clarification or add additional context in comments.

2 Comments

where is this isNan coming from
0

Higher-order function is a function that:

  • Take one or more functions as input.
  • Give another function as output.

What does your code do? it negates the function isNan (to isNotNan). It accept a function (isNan), and then output the negation (isNotNan). It's just that simple.

2 Comments

The function does not return the negation of the argument; it returns a function that returns the negation of the argument.
that's what i'm trying to say. It returns the isNotNan function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.