Solution Review
Understand how to review and refactor JavaScript functions to follow pure function principles, including cloning inputs and eliminating side effects. Discover techniques to maintain purity while using common methods like assoc, append, and sort, enhancing your functional programming skills with Ramda.
We'll cover the following...
assoc
We learned that a pure function must follow 2 rules
- Same inputs => same outputs
- No side-effects
assoc currently mutates the input object, which is a side-effect.
Cure it by cloning the input and merging the desired properties.
getNames
This was sort of a trick question. getNames is pure if you remove console.log.
You can now refactor it to a single-line statement, if you prefer.
append
This is impure because it mutates the input list. Like assoc, you can return a clone with the desired output.
sortAscending
Once again we’re using an impure Array method, sort. Cloning the input purifies us right up.