Skip to content

Commit 71e1f65

Browse files
committed
Adding pure functions
1 parent 6f796ed commit 71e1f65

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,37 @@ A Cookbook for writing FP in JavaScript using ES6
33

44
### Summary
55

6+
* [Pure functions](#pure-functions)
67
* [Higher-order functions](#higher-order-functions)
78
* [Recursion](#recursion)
89
* [Functor](#functor)
910
* [Destructuring](#destructuring)
1011
* [Currying](#currying)
1112

13+
### Pure functions
14+
Returns the same result given same parameters. It's execution doesn't depend on the state of the system
15+
16+
1) Impure
17+
18+
```javascript
19+
let number = 1;
20+
let increment = () => {
21+
return number += 1;
22+
};
23+
increment();
24+
// 2
25+
```
26+
27+
2) Pure
28+
29+
```javascript
30+
let increment = (n) => {
31+
return n + 1;
32+
};
33+
increment(1);
34+
// 2
35+
```
36+
1237
### Higher-order functions
1338
Functions that operate on other functions, either by taking them as arguments or by returning them
1439

@@ -213,3 +238,7 @@ addFive(10);
213238
[https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84](https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84)
214239

215240
[http://functionaljavascript.blogspot.com.br/2013/07/functors.html](http://functionaljavascript.blogspot.com.br/2013/07/functors.html)
241+
242+
[http://nicoespeon.com/en/2015/01/pure-functions-javascript/](http://nicoespeon.com/en/2015/01/pure-functions-javascript/)
243+
244+
[https://drboolean.gitbooks.io/mostly-adequate-guide/](https://drboolean.gitbooks.io/mostly-adequate-guide/)

examples.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
// pure fuctions
2+
var number = 1;
3+
var increment = function() {
4+
return number += 1;
5+
};
6+
console.log(increment());
7+
8+
var increment = function(n) {
9+
return n + 1;
10+
};
11+
console.log(increment(1));
12+
113
// Sum
214
let sum = (x, y) => {
315
return x + y;

0 commit comments

Comments
 (0)