-1

I am learning Higher-order functions from Eloquent JavaScript, I did my research for a similar question but was unable to find it. There was a similar question related to mine from the same topic and same chapter but it was a basic one. Here's the link for a similar one: Higher-order functions in Javascript.

I am facing a problem with the below code:

function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action(i);
  }
}

let unless = (test, then) => {
  if(!test)then();
};

repeat(3, n => {
  unless(n%2 == 1, () => {
    console.log(`${n} is even`);
  });
});

// Output:
/  → 0 is even
// → 2 is even
  1. How does this higher-order function work?
  2. How does the looping work in order to determine whether the number is even or odd?
  • *"There was a similar question related to mine from the same topic and same chapter but it was a basic one"* Please always link the questions that you think may be related. – T.J. Crowder Oct 26 '21 at 16:59
  • Sure will edit within a few minutes. –  Oct 26 '21 at 17:00
  • Truly said, there's no higher order functions here. HOF - is a function, that takes function(s) and RETURN FUNCTION. All ones listed above are voids. – Gorynych Oct 26 '21 at 17:10
  • Technically a higher order function can take a function as an argument or return a function. So this question shows a HOF but it’s not clear what the OP is running into trouble with. Please show a [mcve] that we can run to see the issue. Also read the linked question and see how specific the questions are, that makes it easier to answer. – Nathan Hughes Oct 26 '21 at 17:23

4 Answers4

1

Higher-order functions either accept a function as a parameter, or return a function; both unless and repeat accept functions as parameters.

repeat accepts a number and a function, and simply calls whatever function was passed to it, that number of times.

unless accepts a value and a function; if the value is falsy then the function will be called.

What this demonstrates is that functions can be passed around just like any other variable: inside repeat(), action refers to the function itself, and action() calls that function to get its result.

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
0
function repeat(n, action) {
 for (let i = 0; i < n; i++) {
   action(i);
 }
}

n is the number of time you want execute the action function.

the action function is called here a callback function

if you do repeat(5, console.log) you gonna have:

0
1
2
3
4

you can also execute another function like this

repeat(5, item => console.log(item));
s17
  • 71
  • 5
0

What is higher order function ?

Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.

Let me start with a small example and then we get back to the problem in the question.

function evenNumberFilter(number) {
  return number%2==0
}

function oddNumberFilter(number) {
  return !evenNumberFilter(number)
}

evenNumberFilter(2) // true
oddNumberFilter(3)  // true

Now we know if we call function

  1. evenNumberFilter with parameter X it will return true if it is even number
  2. oddNumberFilter with parameter X it will return true if it is odd number

Let's extend the problem and say

  1. "I would like to have all even numbers till number 10"
  2. "I would like to have all odd numbers till number 10"

function evenNumberFilter(number) {
  return number %2 == 0
}

function oddNumberFilter(number) {
  return !evenNumberFilter(number)
}

function evenNumberBeforeTen() {
  const result = []
  for(number=0; number<10; number++) {
    if (evenNumberFilter(number)) {
      result.push(number)
    }
  }
  return result
}

function oddNumberBeforeTen() {
  const result = []
  for(number=0; number<10; number++) {
    if (oddNumberFilter(number)) {
      result.push(number)
    }
  }
  return result
}

evenNumberBeforeTen();  //[0,2,4,6,8]
oddNumberBeforeTen();   //[1,3,5,7,9]

If we look at the code both evenNumberBeforeTen and oddNumberBeforeTen share good amount of common code

  1. The way for loop is itereated
  2. The way result is appended to an array
  3. The result that is returned at the end of the function.

Only difference between these two functions is, what filter it operates on either evenNumberFilter or oddNumberFilter.

So can we refactor the code and write one generic filter function which can take the predicate as a parameter ?

Predicate: a boolean-valued function P: X? {true, false}.

In our case both evenNumberFilter and oddNumberFilter are predicates

function evenNumberFilter(number) {
  return number %2 == 0
}

function oddNumberFilter(number) {
  return !evenNumberFilter(number)
}

function filter(predicate) {
  const result = []
  for(number=0; number<10; number++) {
    if (predicate(number)) {
      result.push(number)
    }
  }
  return result
}

filter(evenNumberFilter);  //[0,2,4,6,8]
filter(oddNumberFilter);   //[1,3,5,7,9]

Now, in the above example function filter is higher order function because it takes function predicate as parameter.

Let's get into the problem you mentioned in the question.

repeat is a higher order function which takes number and function action as a parameter.

unless is a higher order function which takes boolean and function then as a parameter

When you execute

repeat(3, n => {
  unless(n%2 == 1, () => {
    console.log(`${n} is even`);
  });
});
  1. repeat is called first and then it will execute function below 3 times.

function (n) => {
  unless(n%2 == 1, () => {
    console.log(`${n} is even`);
});
  1. When above function is called with parameters 0,1,2. It calls unless function with boolean of n%2==1 which will be either true or false and function below as parameter.

function() => {
  console.log(`${n} is even`);
});

When unless is called with true and then it will not execute the then function so no log is printed

When unless is called with false and then it will execute the then function which prints the log

Sumanth Kumar Mora
  • 637
  • 1
  • 5
  • 15
  • Hello, I want to take a moment and appreciate you for your answer. It's clear and simple it's starting to make sense now. Just let me know you got my comment. Coz the diamond moderator is deleting my comments whenever I try to thank or they downvote my question. This was my first question, anyways thanks a lot now I can continue again Eloquent JavaScript :) –  Oct 29 '21 at 13:04
  • You are welcome. Glad the answer helped you. – Sumanth Kumar Mora Oct 29 '21 at 16:40
-1

In general, ur unless function sounds like - take the condition, test it, then run the callback. The repeat func sounds like run action(s) n times. The result sounds like run action(s), and check inside if it satisfies the condition, then run unless function.

Gorynych
  • 100
  • 4