mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-06 02:26:43 +00:00
Merge pull request #121 from alexlafroscia/update-purity-example
Update purity example
This commit is contained in:
commit
86d9603981
1 changed files with 19 additions and 9 deletions
28
readme.md
28
readme.md
|
|
@ -179,26 +179,36 @@ A function is pure if the return value is only determined by its
|
|||
input values, and does not produce side effects.
|
||||
|
||||
```js
|
||||
const greet = (name) => 'Hi, ' + name
|
||||
const greet = (name) => `Hi, ${name}`
|
||||
|
||||
greet('Brianne') // 'Hi, Brianne'
|
||||
|
||||
```
|
||||
|
||||
As opposed to:
|
||||
As opposed to each of the following:
|
||||
|
||||
```js
|
||||
window.name = 'Brianne'
|
||||
|
||||
let greeting
|
||||
|
||||
const greet = () => {
|
||||
greeting = 'Hi, ' + window.name
|
||||
}
|
||||
const greet = () => `Hi, ${window.name}`
|
||||
|
||||
greet() // "Hi, Brianne"
|
||||
|
||||
```
|
||||
|
||||
The above example's output is based on data stored outside of the function...
|
||||
|
||||
```js
|
||||
let greeting
|
||||
|
||||
const greet = (name) => {
|
||||
greeting = `Hi, ${name}`
|
||||
}
|
||||
|
||||
greet('Brianne')
|
||||
greeting // "Hi, Brianne"
|
||||
```
|
||||
|
||||
... and this one modifies state outside of the function.
|
||||
|
||||
## Side effects
|
||||
|
||||
A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state.
|
||||
|
|
|
|||
Loading…
Reference in a new issue