Better understanding higher order functions - helpful resources

That line invokes the function that returns another function. There won’t be any return except that other function.

We would normally assign this to another variable and invoke it at a later time; but, what your instructor has done is immediately invoke the returned function. Examine the code closely…

const showName = function() {
    const name = “FP JavaScript”
    return function() {
        console.log(name)  // note the internal logging
    }
}()    // invoke the returned function
1 Like

Hello Roy. Can you give me a hand here by any chance? At the very last line, how does get understand that it’s called on the people array?

1 Like

It ‘knows’ because that is the argument passed into the function that is returned from the map() factory function.

My head will explode now. Thanks for your time but I don’t think I got it.

Functions may take an argument that is itself a function, and/or may return a function object to the caller.

The first function above, get() takes a value, specifically a property name from an object literal, or an index for an array object.

function get(prop) {
  return function(obj) {
    return obj[prop]
  }
}

The function that is returned looks like,

function(obj) {
  return obj[prop]
}

where prop in this case is the value that was passed to the get() function.

Let’s consider,

get_prop = get('property')

Now get_prop is itself a function with a scoped internal value, such that, prop === 'property'. If we call that function with the argument, {property: 'value'} (an object) it returns the value’.

console.log(get_prop({property: 'value'}))
 // should print, `value`

In like fashion, the map() function takes a function as its argument and returns a function with a scoped internal value, the function we passed in. That function is the one returned by get().

The returned function takes an array as an argument. Together with the internal function, these are both passed as arguments of .call() method.

array_map = map(get_prop('property'))

Now when we call that function, we give the name of the array we wish to map the return from get_prop().

objs = [{property: 'some value' }, {property: 'some other value' }]

console.log(array_map(objs))
// should print [ 'some value', 'some other value' ]

In the above example we assigned each returned function to it own variable. The example in your post doesn’t, but only passes the actual functions anonymously.

2 Likes

Thank you so much! Don’t think it could be any more clarified than that. I think I’m really getting an idea of how the arguments can be “chained” upon calling, like map(get('email'))(people), and mechanics behind that. I’ve saved your kind explanation to get back to it in a while for a hopefully even better (or at least more fluent) understanding. Thank you.

2 Likes

You’re welcome. Wasn’t sure it was clear enough given so many moving parts. Glad to hear you making some headway.

1 Like

This lesson is seriously bad.

I don’t know whether it’s the instructions, the examples used or just the general lack of information but I have literally no idea what’s going on and I’ve struggled with nothing on this course up to this point.

Definitely needs a rewrite because it might as well be in Chinese.

2 Likes

I have to agree with all the other students here. This lesson is extremely complicated and don’t believe the instructions are well written.

It seems there is a lot of context missing that would be helpful. For example, to even begin understanding HOF I think it is imperative to incorporate what exactly an ‘object’ is. Which I believe has not been taught yet…

Potentially, I think simplifying the use case would be helpful. The article below is what allowed me to slightly know what’s going on.

1 Like

Going to add to other comments here. I’ve been doing well with the lessons, but this one just completely stumped me. Very difficult to understand the instructions, what the purpose is, how to progress. The whole lesson ought to be re-written.

2 Likes

If there wasn’t for the Forum, I wouldn’t have gotten to the end of the lesson of humble three steps. Feeling clueless gradually mesmerised me in realisation I wasn’t alone.
Reading linked articles below the third task, about HOF, and related info users posted in a collective effort to overcome this hurdle, started clearing my mind.

Thinking of those problems were breaking my streaks, hopefully not for long and I haven’t given up learning to code. Is it for me keeps hanging over.

It dawned on me the next day, connecting apparently insignificant event of having received an Email from Codecademy; the quest earned me a badge.

izdvojeni citat

https://discuss.codecademy.com/badges/130/bookworm-bronze?username=nenad1

izdvojeni citat

Witnessing mtf’s (Codecademy moderator’s) eloquence in slicing inferred lesson to code bids lavishly commenting between the lines, made me think perhaps this lesson was purposely crippled. I have a shelf filled with books from the previous decades, where this dimension is absent completely. Feels so integrated at Codecademy.

Our grasping for clues is as important to the lesson’s completion, if not more. Answers are around us and this way Codecademy is growing us a habit of seeking and finding help. Quitting button remains available, but re-routing is much more productive then having erroneous code replaced with a correct one.

Much obliged Roy!

To my understanding, perhaps the most valuable trait of Codecademy is the attitude of hinting replies, encouraging us to pursue, instead of copy/paste remedy on problems which brought students to a halt.

3 Likes

Best explanation I found
3 Likes

Yes, it’s a relief to see that I’m not the only one hitting a wall with this topic!

The articles that were shared in this thread fortunately allowed me to grasp this concept a little better - though I’m sure it will be a whole different ballgame when it will come to practice HOF in bigger projects. Baby steps!

I’ve also found the following content really helpful:


(Now this guy knows how to be pedagogical on top of being entertaining)

And good old MDN:


Hang in there everyone :slight_smile:

4 Likes

I think this is a very valid point. My understanding of this exercise was improved by watching a YouTube video on how HOFs can be used and the benefits of them. The YouTube channel ‘Fun Fun Function’ has a good one.

1 Like

I’m with you, bigtime. This lesson has been very complex for me to grasp. Luckily I found a video series you youTube that seems helpful so I thought I’d share it in case it could benefit someone else who’s struggling with these concepts

Functional Programming in Javascript

2 Likes

Thanks so much for this thread and the replies. I thought it was just me, but this lesson literally had me scratching my head.

I went ahead and completed this lesson before giving up and looking at the code, and I was laughing because I was completely guessing what should go on each line and somehow CA was allowing me to advance, even though I didn’t really understand what I was doing at all. I am so grateful to @amelie.gorry.am53166, @tag4711444728, and @bobbieganujsh (great handle BTW … :yum: ) for sharing these resources. I’ll be digging in a little before moving on.

Cheers! :clap:

1 Like

Hi guys, I am still not getting the example in the lesson where it says:

const timeFuncRuntime = funcParameter => {
   let t1 = Date.now();
   funcParameter(); // why are we calling this function?
   let t2 = Date.now();
   return t2 - t1;
}

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

The idea of the timeFuncRuntime function is to time the function we pass in, which in this case is, addOneToOne.

When the function is given as an argument if is only by reference. We do not invoke it until the runtime function calls it. Note that there is a timestamp captured before and after. Their difference will be in milliseconds.

To see the actual runtime, log the return value…

console.log(timeFuncRuntime(addOneToOne))
1 Like

Thank you very much mtf! I’ll def go over the lesson one more time and read all the articles shared in this topic as well

Thank god Im not the only one. Did a few hours yesterday and all seemed to click fine. Sat down this morning to start higher order functions and felt like i didnt know a thing!

2 Likes