-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-by-example.js
178 lines (136 loc) · 4.1 KB
/
js-by-example.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This is the introduction to the “Composing Software” series on learning functional programming and compositional
// software techniques in JavaScript ES6+ from the ground up. Stay tuned. There’s a lot more of this to come!
// https://medium.com/javascript-scene/composing-software-an-introduction-27b72500d6ea
// ============================
// Ternary operator variable assignment
// ============================
var myAge = 16, votingAge = 18
var canVote = myAge >= votingAge ? true : false
// ============================
// Short circuit variable assignment
// ============================
var myAge = 18, votingAge = 18
var canVote = myAge >= votingAge || false
// ============================
// Building composites with class inheritance:
// ============================
class Milk {
constructor () {
this.base = { milk: 'whole' }
}
}
class Milkshake extends Milk {
constructor (options) {
super(options)
this.additions = { iceCream: 'vanilla' }
}
}
const myMilkshake = new Milkshake()
// { base: { milk: 'whole' }, additions: { iceCream: 'vanilla' }
// ============================
// Building composites with mixin composition:
// ============================
const base = { milk: 'whole' }
const additions = { iceCream: 'vanilla' }
const myMilkshake = { base, additions }
// { base: { milk: 'whole' }, additions: { iceCream: 'vanilla' }
// ============================
// Function Composition
// ============================
const inc = n => n + 1
const double = n => n * 2
const doStuffBetter = x => inc(double(x))
doStuffBetter(20)
// 42
// ============================
// unary and n-ary function epressions
// ============================
function sum(x, y) {
return x + y
}
function first(x) {
function second(y) {
return x + y
}
}
// ============================
// Composing Two Functions
// ============================
// compose() takes the double function as the first argument, the inc function as the second,
// then applies the composition of those two functions to the argument 3
const compose = first => second => arg => first(second(arg))
const double = x => x * 2
const increment = x => x + 1
compose(double)(increment)(3)
// Non ES6 version for comparison
const double = function(n) {
return n * 2
}
const inc = function(n) {
return n + 1
}
const compose = function(first) {
return function(second) {
return function(argument) {
return first(second(argument))
}
}
}
// ============================
// Destructuring
// ============================
const [ one, two ] = [ 1, 2 ]
one // 1
two // 2
const obj = { key: 'value' }
// The following is equivalent to: const key = obj.key
const { key } = obj
key // 'value'
// Using a different name for the new binding
const { key: newKey } = obj
newKey // 'value'
// ============================
// Default Function parameters
// ============================
// Returns the argument or if none is supplied, returns 0
const orZero = (n = 0) => n
// ============================
// Named Arguments with Default Parameters
// ============================
const createUser = ({
name = 'Anonymous',
avatar = 'avatar.png'
}) => ({
name,
avatar
})
const george = createUser({
name: 'George',
avatar: 'george.png'
})
george
// {
// name: 'George',
// avatar: 'george.png'
// }
// ============================
// Rest/Spread Operator
// ============================
const firstToLast = (firstArg, ...otherArgs) => [...otherArgs, firstArg]
firstToLast(1, 2, 3, 4, 5) // [2, 3, 4, 5, 1]
// ============================
// Autocurry helper
// ============================
const curry = (f, arr = []) => (...args) => (a => a.length === f.length ? f(...a) : curry(f, a))([...arr, ...args])
const add3 = curry((a, b, c) => a + b + c)
add3(1, 2, 3) // 6
add3(1, 2)(3) // 6
add3(1)(2, 3) // 6
add3(1)(2)(3) // 6
// ============================
// Array methods
// ============================
// Map: applies a function to each item in the array
const double = n => n * 2
[1, 2, 3].map(double) // [2, 4, 6]
// https://medium.com/javascript-scene/higher-order-functions-composing-software-5365cf2cbe99#.egoxjg6x7