The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Intro to Recursion - Refactoring to a Pure Function

Intro to Recursion - Refactoring to a Pure Function

3:37
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.
Watch this lesson now
Avatar
egghead.io

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.

Avatar
William Carroll

I enjoyed the video as the author made the concept of pure functions clearer to me. Correct me if I am wrong, but isn't the refactored function at the end of this video still "impure"? The author claims that it does not rely on any external state; however, within the if-statement, there is a reference to the config variable that exists outside of the function. It seems to me that this function would be more pure if instead the config state was passed into the function. Again, I enjoyed the video. And I may well be wrong about this. Happy to discuss!

In reply to egghead.io
Avatar
Shane

Thanks William. The focus of the lesson was to remove any mutation from within the function, but you are correct - it's not 100% pure for the reason you mentioned. I will update this video with an additional explanation regarding that point. :)

In reply to William Carroll
Avatar
Joel

This lesson video was updated!

Avatar
Bartłomiej

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

Avatar
Wayne

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

Avatar
Wayne

To me that seems like semantics.

In reply to Bartłomiej
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?