Transcript from the "Spread Adapter" Lesson
>> Kyle Simpson: Another example of shape adaptation that we commonly do, you will see this much more often than may be obvious at this moment. One kind of shape adaptation is I have a function that takes individual arguments, but I wanna call that function with an array. It's expecting individual arguments, but here I have an array.
[00:00:43] When you take an array and spread it out as individual arguments, that's called an apply. Here I've called it spreadArgs, just so you knew exactly what it's doing. It's spreading the argument, the array of arguments, out into individual ones. See how it does line three, the ..., that's the spread operator.
[00:01:26] So, when I call g with an array, f gets individual arguments. Everybody follow me? Here is a little pop quiz. We're not gonna actually show this code but I just want you to just play with this in your head, maybe type it in your code for just a moment.
[00:01:41] This is a pop quiz. If spreadArgs is a function that can take an array and spread it out onto individual ones, what's the inverse of that? What would unspreadArgs look like? Unapply, what would that look like? I would be able to pass in individual arguments, and the underlying function would receive what?
>> Speaker 2: An array.
>> Kyle Simpson: An array. Just think about that for just a moment, we'll just take like 30 seconds. As the pop quiz, I want you to think about, look at that spreadArgs and think how would I change this to do the inverse?
>> Kyle Simpson: It's not as complex as you might have thought the ..., operator on line three, we simply need to move it from line three to line two.
[00:02:27] We need to gather the arguments from line two and pass them as a single array, line three, that's it. That's the only difference between a spreadArgs and a gatherArgs or an apply and an unapply, is whether we gather first and then pass the array, or take an array and then spread it.
[00:02:51] It's the only difference between the two, and you'll find apply and unapply in all your favorite functional programming libraries.