Intro to Recursion - Refactoring to a Pure Function

Shane Osbourne
InstructorShane Osbourne
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

Our previous solution used forEach and a globally available array that could be mutated from inside our function. We can improve upon this and create a function that is easier to maintain & test by swapping our forEach loop for reduce. By removing the global array and instead making getTasks return a value directly we end up with a pure function.

[00:00] In the previous lesson we created this recursive function, get tasks. This implementation, though, has some problems. First of all, because we have this array defined outside the function, and then we push items onto it from inside the function, it means that every time this runs now it relies on the external state of this array.

[00:23] Also, because this function does not return a value directly, it makes it extremely hard to test. Really what we want is to be able to assign this tasks variable to the result of calling this function. To do this we'll need to refer to how this works. To make your return of value we can swap for each for reduce. The signature for reduce is the previous value and the next value.

[00:52] On the first run-through, previous won't equal anything yet. We need to seed for the reduce function. We'll call that initial. We'll actually pass that in as the first argument to the get tasks function.

[01:01] This means that when we call it for the very first time we can send in an empty array. Our goal is to get an array out of the other end of this function and it will be the result of building up from this empty array. We call get tasks with an empty array and our input. The reduce function gets called on the input, passing in the initial value.

[01:35] The first time it runs, previous will be equal to this empty array and next will be equal to build. We do a very similar thing to the previous example, only this time we're looking at the next value, which is going to be each item of the array. We do the same check as before.

[01:55] But now, whereas previously we just called the task again and allowed the external array to be mutated, we instead need to return the result of this function and pass along not the initial value but the previous value. To actually build up that previous value where we previously pushed onto that tasks array we will instead return the previous value, which will be an array. We'll conquer onto that the current item.

[02:34] If we run this you can see we get exactly what we expected. The major difference here is that we are not relying on any external state and we have a return value from get tasks. This is now what we call a pure function, in that it has no side effects and calling it with the same values will always produce the same results.

[03:30] This has big implications for things such as testing and the ability to share this function across your application....

Joel Hooks
Joel Hooks
~ 8 years ago

This lesson video was updated!

Bartłomiej Smykla
Bartłomiej Smykla
~ 7 years ago

In Array.prototype.reduce method, first parameter is accumulator and second one is currentValue, not previous and next.

Tre' Codez
Tre' Codez
~ 7 years ago

These three vids were the absolute best intros to Recursion I've seen in my career; for the way I see programming anyway ☻

Tre' Codez
Tre' Codez
~ 7 years ago

To me that seems like semantics.

~ 2 years ago

Really valuable, thank you

Markdown supported.
Become a member to join the discussionEnroll Today