|
| 1 | +#### Basic example of mutations |
| 2 | + |
| 3 | +```js |
| 4 | +let state = { |
| 5 | + wardens: 900, |
| 6 | + animals: 800 |
| 7 | +}; |
| 8 | +``` |
| 9 | + |
| 10 | +This above Object holds the information of a Zoo application. If we change the number of animals in the state Object: |
| 11 | + |
| 12 | +```js |
| 13 | +let state = { |
| 14 | + wardens: 900, |
| 15 | + animals: 800 |
| 16 | +}; |
| 17 | +state.animals = 90; |
| 18 | +``` |
| 19 | + |
| 20 | +Our state object will hold/encode a new information: |
| 21 | + |
| 22 | +```js |
| 23 | +state = { |
| 24 | + wardens: 900, |
| 25 | + animals: 90 |
| 26 | +}; |
| 27 | +``` |
| 28 | + |
| 29 | +This is called mutation. |
| 30 | + |
| 31 | +Immutability comes when we want to preserve our state. To keep our state from changing we have to create a new instance of our state objects. |
| 32 | + |
| 33 | +```js |
| 34 | +function bad(state) { |
| 35 | + state.prp = "yes"; |
| 36 | + return state; |
| 37 | +} |
| 38 | + |
| 39 | +function good(state) { |
| 40 | + let newState = { ...state }; |
| 41 | + newState.prp = "yes"; |
| 42 | + return newState; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Immutability makes our app state predictable, ups the performance rate of our apps and to easily track changes in state. |
| 47 | + |
| 48 | +#### Pure Functions, Side Effects |
| 49 | + |
| 50 | +Pure functions are functions that accept an input and returns a value without modifying any data outside its scope(Side Effects). Its output or return value must depend on the input/arguments and pure functions must return a value. |
| 51 | + |
| 52 | +```js |
| 53 | +function impure(arg) { |
| 54 | + finalR.s = 90; |
| 55 | + return arg * finalR.s; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +The above function is not a pure function because it modified a state finalR.s outside its scope. |
| 60 | + |
| 61 | +```js |
| 62 | +function impure(arg) { |
| 63 | + let f = finalR.s _ arg |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The above function also isn’t a pure function because it didn’t return a value though it didn’t modify any external state. |
| 68 | + |
| 69 | +```js |
| 70 | +function impure(arg) { |
| 71 | + return finalR.s _ 3 |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +The above function is impure, though it didn’t affect any external state, its output return finalR.s \_ 3 isn't dependent on the input arg. Not only must pure function return a value but it must depend on the input. |
| 76 | + |
| 77 | +```js |
| 78 | +function pure(arg) { |
| 79 | + return arg \_ 4 |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The above is a pure function. It didn’t side effect any external state and it returns an output based on the input. |
| 84 | + |
| 85 | +#### 1. A pure function is deterministic. This means, that given the same input, the function will always return the same output. To illustrate this as a function in mathematical terms it is a well defined function. Every input returns a single output, every single time. |
| 86 | + |
| 87 | +A pure function |
| 88 | + |
| 89 | +`const add = (x, y) => x + y` // A pure function |
| 90 | + |
| 91 | +add is a pure function because it’s output is solely dependent on the arguments it receives. Therefore, given the same values, it will always produce the same output. |
| 92 | + |
| 93 | +#### 2. A pure function will not cause side effects. A side effect is any change in the system that is observable to the outside world. |
| 94 | + |
| 95 | +`const calculateBill = (sumOfCart, tax) => sumOfCart * tax` |
| 96 | + |
| 97 | +Is calculateBill pure? Definitely :) It exhibits the two necessary characteristics: |
| 98 | + |
| 99 | +The function depends only on its arguments to produce a result |
| 100 | +The function does not cause any side effects |
| 101 | +The Mostly Adequate Guide states that side effects include, but are not limited to: |
| 102 | + |
| 103 | +changing the file system |
| 104 | +inserting a record into a database |
| 105 | +making an http call |
| 106 | +mutations |
| 107 | +printing to the screen / logging |
| 108 | +obtaining user input |
| 109 | +querying the DOM |
| 110 | +accessing system state |
| 111 | + |
| 112 | +#### Further Reading |
| 113 | + |
| 114 | +[https://hackernoon.com/javascript-and-functional-programming-pt-3-pure-functions-d572bb52e21c](https://hackernoon.com/javascript-and-functional-programming-pt-3-pure-functions-d572bb52e21c) |
0 commit comments