-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
81 lines (68 loc) · 1.82 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Use node v4.4.7 or higher
// Install ramda before running using the command `npm install ramda`
// Then run this file using the command `node --harmony examples.js`
var R = require('ramda');
// not functional / not pure version of increment
var a1 = 0;
function increment() {
a1 += 1;
}
increment();
increment();
console.log(a1); // a1 has been changed
// functional / pure version of increment
var a = 0;
function increment(a2) {
return a2 + 1;
}
console.log(increment(a));
console.log(increment(a));
console.log(a); // a remains unchanged
var myObj = { id: 1, isOn: false };
// enable function using javascript (ES6)
function enable(obj) {
return Object.assign({}, obj, {
isOn: true
});
}
// same enable function using Ramda
var ramdaEnable = R.assoc('isOn', true);
var newObj = enable(myObj);
var ramdaNewObj = ramdaEnable(myObj);
console.log(newObj);
console.log(ramdaNewObj);
console.log(myObj); // myObj remains unchanged
var originalState = {
todos: [
{
text: 'learn ramda',
complete: false
},
{
text: 'learn functional programming',
complete: false
}
]
};
var myAction = {
text: 'learn haskell'
};
// a redux reducer using javascript (ES6)
function addToDo(state, action) {
var newTodo = { text: action.text, complete: false };
var todos = [ ...state.todos, newTodo ];
return Object.assign({}, state, {
todos: todos
});
}
// the same redux reducer using Ramda
function ramdaAddToDo(state, action) {
var newTodo = { text: action.text, complete: false };
var todos = R.append(newTodo, state.todos);
return R.assoc('todos', todos, state);
}
var newState = addToDo(originalState, myAction);
var ramdaNewState = ramdaAddToDo(originalState, myAction);
console.log(newState);
console.log(ramdaNewState);
console.log(originalState); // original state remains unchanged