functional programming

Building software by creating pure functions, avoid shared state, mutable data, and side-effects.

application state flows through pure functions.

Side Effects and Pure Functions

Notice the data is the array of users.
We have our mutable function that literally uses scope to access users and change its contents.

For the pure functions we take in parameters, and we do not modify these inputs. Instead, we return new arrays with shallow copied data. Notice we never touch param arr, nor name. We simply get the data we need, and return new arrays.

We then use the pure functions to calculate new scores.
Then give it to our mutable functions to update our data.

Avoid Shared State

Javascript stores data in objects and variables. This is called state.

Notice how users are shared via closure scope. We need to void this by passing state (users)
from one function to another.

Mutation

how to avoid mutating? Clone it

but…for objects with nested data:

we need to convert it to string, then parse it back into an object.

We can do the same with arrays:

Object.assign and spread operator works the same way.

For Arrays, spreading a null would give an error

For Objects, both silently exclude the null value with no error.