Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e3369f

Browse files
authored
Pure Functions
1 parent 5976ed7 commit 0e3369f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,58 @@ console.log(twoByTwo); // [[a, b], [b, c], [c, d], [d, e], [e, f], [f, g]]
9494

9595
<a href="https://codepen.io/Bunlong/pen/JBJWRa" target="_blank">Edit on Codepen</a>
9696

97+
## Pure Functions
98+
99+
A function that given the same input, will always return the same output. A pure function produces no side effects.
100+
101+
Note: Side effects may include:
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+
```javascript
113+
var xs = [1, 2, 3, 4, 5];
114+
115+
// ==== pure ====
116+
xs.slice(0, 3);
117+
//=> [1, 2, 3]
118+
119+
xs.slice(0, 3);
120+
//=> [1, 2, 3]
121+
122+
xs.slice(0, 3);
123+
//=> [1, 2, 3]
124+
125+
// ==== pure ====
126+
xs.splice(0, 3);
127+
//=> [1, 2, 3]
128+
129+
xs.splice(0, 3);
130+
//=> [4, 5]
131+
132+
xs.splice(0, 3);
133+
//=> []
134+
135+
// pure
136+
var checkAge = function(age) {
137+
var minimum = 21;
138+
return age >= minimum;
139+
};
140+
141+
// impure
142+
var minimum = 21;
143+
144+
var checkAge = function(age) {
145+
return age >= minimum;
146+
};
147+
```
148+
97149
## Currying
98150

99151
Currying allows a function with multiple arguments to be translated into a sequence of functions. Curried functions can be tailored to match the signature of another function.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /