Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't understand about higher-order function parameter passed in example

I was reading article about higher-order functions. And they give code example like this:

var humans = function(data) {
  return data.filter(function(character) {
    return character.species === 'human';
  })
}

var images = function(data) {
  return data.map(function(character) {
    return character.img;
  })
}

function compose(func1, func2) {
  return function(data) {
    return func2(func1(data));
  };
}

var displayCharacterImages = compose(humans, images);

The one thing that’s I don’t understand is where is the data are gonna passed in compose function? I mean we only pass func1, func2 as params. So where can the data go in compose function?
I am still a newbie so please help me understand this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Compose function returns a function not a value.
So when you call compose you get a function.
displayCharacterImages is a function not a value.

The data variable is the returned functions argument.

Basically, you will be doing this :
displayCharacterImages(foo)
So data here will get the value you passed in foo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading