Enterprise Architecture Patterns Higher Order Functions
Transcript from the "Higher Order Functions" Lesson
>> For the fourth and final element of our Kung Fu, saga, collections and iterators. And so just before I get started in this, I just want to make sure that we understand when we talk about iterators is that we have more than one thing and we need to perform some action on that collection of things.
[00:01:05] In other words is you're kind of measuring this object if you will. So it's like, okay, I'm gonna start at zero and I'm gonna go to clients. How many do you have? Okay. 10. I'm gonna keep going over and it each iteration, I'm gonna have to ask the clients or right like, hey, I'm in position number two, what client are you and you're constantly having to reference via array notation.
[00:02:09] And instead of delegating the iteration of that collection to something else you're allowing the array to handle that in iterate over itself, and not only is it able to iterate over itself, but it's able to keep track of whatever item it's on because it knows what it is, because It is controlling itself.
[00:02:37] So I look at this and I can think of only one reason I've only ever seen one reason in, I don't know, the six or seven years that have elapsed or transpired since I started moving to ES6. And it's if you need to do In iteration where you need to loop for a specific length, so like I need to have 100 iterations of something and then I need to fill or need to push items into an array and it's a very specific number that you need to iterate.
[00:03:12] You cannot use hydro higher order functions for that. But for everything else higher order functions. So my three favourite ones outside of for each is filter, map and reduce. And so we've kind of hinted at these through the workshop but all the filter does is say, I wanna take this array, I wanna loop over and I wanna return everything that matches this condition.
[00:03:43] A map allows you to loop over a collection and perform some kind of logical operation on that collection. Reduce what this does Is it takes the current state or the accumulated state and then the current value and it is simply, or it's simply performs a logical operation or performs a specific logical operation until you get to the end.
[00:04:17] And then it would turn into a final reduced value for that operation. So what you're doing is you're taking all these elements and you're reducing them into a single value. Ironically, this is very close to how the reducer works in Redux.