-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
393 lines (366 loc) · 11.6 KB
/
index.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!DOCTYPE html>
<html>
<head>
<title>Fun with Functions</title>
</head>
<body>
<pre>
<script>
function log(arg) {
document.writeln(arg);
}
function identity(x) {
return x;
}
// log(identity(3));
function add(first, second) {
return first + second;
}
function sub(first, second) {
return first - second;
}
function mul(first, second) {
return first * second;
}
// log(add(3, 4));
// log(sub(3, 4));
// log(mul(3, 4));
function identityf(x) {
return function() {
return x;
}
}
// var three = identityf(3);
// log(three());
// Write a function addf that adds from two invocations.
function addf(first) {
return function (second) {
return first + second;
}
}
// log(addf(3)(4));
// Write a function liftf that takes a binary function,
// and makes it callable with two invocations.
// Higher-order functions: receive other functions as parameters,
// and return other functions as results.
function liftf(binaryFunc) {
return function (first) {
return function (second) {
return binaryFunc(first, second);
}
}
}
// var addf = liftf(add);
// log(addf(3)(4));
// log(liftf(mul)(5)(6));
// Write a function curry that takes a binary function as an argument,
// and return a function that can take a second argument.
function curry(binaryFunc, first) {
return function (second) {
return binaryFunc(first, second);
}
// alternative: use liftf
// return liftf(binaryFunc)(first);
}
// var add3 = curry(add, 3);
// log(add3(4));
// log(curry(mul, 5)(6));
// Curry was a mathematician who did a lot of work with Church's lambda calculus.
// By normalizing all functions to take only one argument,
// it made a lot of operations easier to think about.
// Without writing any new functions,
// show three ways to create the inc function.
var inc = curry(add, 1);
var inc = liftf(add)(1);
var inc = addf(1);
// log(inc(5));
// log(inc(inc(5)));
// First rule of functional programming: let the functions do the work.
// If you've already written a function that does what you need, you don't need to write another one.
// Write a function twice that takes a binary function
// and returns a unary function
// that passes its argument to the binary function twice,
// e.g. doubl = twice(add); square = twice(mul);
function twice(binaryFunc) {
return function (x) {
return binaryFunc(x, x)
}
}
var doubl = twice(add);
// log(doubl(11));
var square = twice(mul);
// log(square(11));
// Write reverse, a function that reverses
// the arguments of a binary function.
function reverse(binaryFunc) {
return function (first, second) {
return binaryFunc(second, first);
}
}
// with ES6 and for any number of arguments:
// function reverse(func) {
// return function(...args) {
// return func(...args.reverse());
// }
// }
var bus = reverse(sub);
// log(bus(3, 2)); // -1
// Write a function composeu that takes two unary functions
// and returns a unary function that calls them both,
function composeu(func1, func2) {
return function (x) {
return func2(func1(x));
}
}
// log(composeu(doubl, square)(5)); // (5+5)^2 = 100
// Write a function composeb that takes two binary functions
// and returns a function that calls them both.
function composeb(binaryFunc1, binaryFunc2) {
return function (x, y, z) {
return binaryFunc2(binaryFunc1(x, y), z);
}
}
// log(composeb(add, mul)(2, 3, 7)); // (2+3) * 7 = 35
// Write a limit function that allows a binary function
// to be called a limited number of times.
function limit(binaryFunc, count) {
return function (x, y) {
if (count >= 1) {
count -= 1;
return binaryFunc(x, y)
}
return undefined; // explicitly document the behavior
}
}
var add_ltd = limit(add, 2);
// log(add_ltd(3, 4)); // 7
// log(add_ltd(3, 5)); // 8
// log(add_ltd(3, 6)); // undefined
// Write a from function that produces a generator
// that will produce a series of values.
function from(x) {
return function () {
var count = x;
x += 1;
return count;
}
}
var index = from(0);
// log(index()); // 0
// log(index()); // 1
// log(index()); // 2
// Write a to function that takes a generator and an end value,
// and returns a generator that will produce numbers up to that limit.
function to(generator, endValue) {
return function () {
var value = generator();
if (value < endValue) {
return value;
} else {
return undefined;
}
}
}
var index = to(from(1), 3);
// log(index()); // 1
// log(index()); // 2
// log(index()); // undefined
// Write a fromTo function that produces a generator
// that will produce values in a range.
function fromTo(startValue, endValue) {
return to(from(startValue), endValue);
}
var index = fromTo(0, 3);
// log(index()); // 0
// log(index()); // 1
// log(index()); // 2
// log(index()); // undefined
// Write an element function that takes an array and a generator
// and returns a generator that will produce elements from the array.
function element(arr, generator) {
return function () {
var index = generator();
if (index !== undefined) {
return arr[index];
}
}
}
var ele = element(['a', 'b', 'c', 'd'], fromTo(1, 3));
// log(ele()); // 'b'
// log(ele()); // 'c'
// log(ele()); // undefined
// Modify the element function so that the generator argument is optional.
// If a generator is not provided, then each of the elements of the array will be produced.
function element(arr, generator) { // ES6: generator = fromTo(...)
if (generator === undefined) {
generator = fromTo(0, arr.length);
}
return function () {
var index = generator();
if (index !== undefined) {
return arr[index];
}
}
}
var ele = element(['a', 'b', 'c', 'd']);
// log(ele()); // 'a'
// log(ele()); // 'b'
// log(ele()); // 'c'
// log(ele()); // 'd'
// log(ele()); // undefined
// Write a collect function that takes a generator and an array
// and produces a function that will collect the results in the array.
function collect(generator, arr) {
return function () {
var value = generator();
if (value !== undefined) {
arr.push(value);
}
return value;
}
}
var arr = [],
col = collect(fromTo(0, 2), arr);
// log(col()); // 0
// log(col()); // 1
// log(col()); // undefined
// log(arr); // [0, 1]
// Write a filter function that takes a generator and a predicate
// (a predicate is a function that returns a boolean),
// and produces a generator that produces only the values approved
// by the predicate.
function filter(generator, predicate) {
return function () {
var value;
do {
value = generator();
} while (
(value !== undefined) && (!predicate(value))
);
return value;
}
}
// ES6: tail recursion (the compiler is required to optimize the call)
// function filter(generator, predicate) {
// return function recur() {
// var value = generator();
// if (
// (value === undefined) || predicate(value)
// ) {
// return value;
// }
// return recur();
// }
// }
var fil = filter(
fromTo(0, 5),
function third(value) {
return (value % 3) === 0;
}
);
// log(fil()); // 0
// log(fil()); // 3
// log(fil()); // undefined
// Write a concat function that takes two generators
// and produces a generator that combines the sequences.
function concat(generator1, generator2) {
// var completedGenerators = []
var generator = generator1;
return function () {
var value = generator();
if (value !== undefined) {
return value;
}
// completedGenerators.push(generator);
// if (completedGenerators.length < 2) {
generator = generator2;
return generator();
// }
// return undefined;
}
}
// ES6, any number of generators:
// function concat(...generators) {
// var next = element(generators),
// generator = next();
// return function recur() {
// var value = generator();
// if (value === undefined) {
// generator = next();
// if (generator !== undefined) {
// return recur();
// }
// }
// return value;
// }
// }
var con = concat(fromTo(0, 3), fromTo(0, 2));
// log(con()); // 0
// log(con()); // 1
// log(con()); // 2
// log(con()); // 0
// log(con()); // 1
// log(con()); // undefined
// Make a function gensymf that makes a function that generates unique symbols.
function gensymf(prefix) {
var generator = from(1);
return function () {
return prefix + generator();
}
}
// official solution:
// function gensymf(prefix) {
// var number = 0;
// return function () {
// number += 1;
// return prefix + number;
// }
// }
var geng = gensymf("G");
var genh = gensymf("H");
// log(geng()); // 'G1'
// log(genh()); // 'H1'
// log(geng()); // 'G2'
// log(genh()); // 'H2'
// Write a function gensymff that takes a unary function and a seed
// and returns a geynsymf.
function gensymff(unaryFunc, seed) {
return function (prefix) {
var number = seed; // note this here so we don't get G1, H2, G3, H4
return function () {
number = unaryFunc(number);
return prefix + number;
}
}
}
var geynsymf = gensymff(inc, 0),
geng = gensymf("G"),
genh = gensymf("H");
// log(geng()); // 'G1'
// log(genh()); // 'H1'
// log(geng()); // 'G2'
// log(genh()); // 'H2'
// Make a function fibonaccif that returns a generator
// that will return the next fibonacci number.
function fibonaccif(a, b) {
return concat(
element([0, 1]),
function fibonacci() {
var next = a + b;
a = b;
b = next;
return next;
}
)
}
var fib = fibonaccif(0, 1);
log(fib()); // 0
log(fib()); // 1
log(fib()); // 1
log(fib()); // 2
log(fib()); // 3
log(fib()); // 5
</script>
</pre>
</body>
</html>