-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.js
524 lines (452 loc) · 10.7 KB
/
exercise.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Exercises from Effective JavaScript by Doug Crockford
// from class taught June 10-12, 2014
//
// The exercises are a sequence of problems that successively add more
// functions to this file. You can see it step by step by looking at
// the git commits (https://github.com/dandoug/effectiveJS).
//
// The test cases that exercise the functions are at the bottom. As I
// did the exercises, I would comment out old test cases so the output
// would only be for the current exercise.
//
// Speaking of exercises, you can find the notes I took from the class,
// as well as the slides for the exercises and the solutions presented
// in class here: https://www.evernote.com/pub/dandoug/efectivejavascriptjune2014
//
// This file can be run three ways:
// 1) By pointing a web-browser at the index.html file in the same directory
// 2) On the command-line, node exercise.js
// 3) Within an IDE like Eclipse (with nodeclipse installed)
// write out arg
// Writen to write to a DOM if document is defined, otherwise
// use console.log()
function log(arg) {
if (typeof(document) != "undefined") {
document.writeln(arg);
} else {
console.log(arg);
}
}
// Exercises start here....
// identity function
function identity(x) {
return x;
}
// add two numbers
function add(a, b) {
return +a + +b;
}
// subtract two numbers
function sub(a, b) {
return +a - +b;
}
// multiply two numbers
function mul(a, b) {
return +a * +b;
}
// function that returns a function that returns its argument
function identityf(func) {
return function() {
return func;
};
}
// funciton that adds from two invocations
function addf(a) {
return function(b) {
return +a + +b;
};
}
// function that takes a binary function and makes it callable with two invocation
function liftf(f) {
return function(a) {
return function(b) {
return f(a,b);
};
};
}
// take a function an argument, return function that adds second argument
function curry(f, a) {
return function(b) {
return f(a, b);
};
}
// return a unary function that invokes a binary function with its arg passed twice
function twice(f) {
return function(a) {
return f(a,a);
};
}
// reverse the args of a binary function
function reverse(f) {
return function(a,b) {
return f(b,a);
};
}
// compose two unary functions
function composeu(fa, fb) {
return function(a) {
return fb(fa(a));
};
}
// invoke two binary functions
function composeb(f, g) {
return function(a, b, c) {
return g( f(a, b), c);
};
}
// invoke a binary function exactly once
function once(f) {
var called = false;
return function(a,b) {
if (! called) {
called = true;
return f(a,b);
}
return undefined;
};
}
// generator that produces values in a range
function fromTo(from, to) {
return function() {
var answer = from;
if (answer < to) {
from += 1;
return answer;
} else {
return undefined;
}
};
}
// generator that uses generator to return elements from an array
function element(arr, ixfn) {
if (ixfn === undefined) {
ixfn = fromTo(0, arr.length);
}
return function() {
return arr[ixfn()];
};
}
// take a generator and an array, return function that collects what is generated into the array
function collect(gen, arr) {
return function() {
var ele = gen();
if (ele !== undefined) {
arr.push(ele);
}
return ele;
};
}
// filter a generator
function filter(gen, pred) {
return function() {
do {
var value = gen();
} while (value !== undefined && !pred(value));
return value;
};
}
// concatenation two generators
function concat(gena, genb) {
return function() {
var value = gena();
if (value === undefined) {
value = genb();
}
return value;
};
}
// construct an object
function counter(value) {
return {
next: function() {
value += 1;
return value;
},
prev: function() {
value -= 1;
return value;
}
};
}
// make a revocable function wrapper
function revocable(func) {
var callable = true;
return {
invoke : function(a) {
if (callable) {
return func(a);
}
return undefined;
},
revoke : function() {
callable = false;
}
};
}
// symbol generator
function gensymf(root) {
var r = String(root),
i = 0;
return function() {
i += 1;
return r + i;
};
}
// a factory to make a symbol generator
function gensymff(func, seed) {
return function(root) {
var i = seed,
r = String(root);
return function() {
i = func(i);
return r + i;
};
};
}
// finaonacci sequence generator
function fibonaccif(a, b) {
var x = undefined,
y = undefined;
return function() {
if (x === undefined) {
x = a;
return x;
}
if (y == undefined) {
y = b;
return y;
}
var value = x + y;
x = y;
y = value;
return value;
};
}
// make an object that has value and source as a string
function m(value, source) {
return {
value: value,
source: source || String(value)
};
}
// build a op that adds m
function addm( m1, m2 ) {
return m( m1.value + m2.value, "(" + m1.source + "+" + m2.source + ")");
}
//// generate functions that journal binary m operations
//function liftm(func, op) {
// return function(m1, m2) {
// return m( func(m1.value,m2.value), "(" + m1.source + op + m2.source + ")");
// };
//}
//generate functions that journal binary m operations, wrap number inputs
function liftm(func, op) {
var mify = function(x) {
var mfy = x;
if (typeof(x) === 'number') {
mfy = m(x);
}
return mfy;
};
return function(x1, x2) {
var m1 = mify(x1);
var m2 = mify(x2);
return m( func(m1.value,m2.value), "(" + m1.source + op + m2.source + ")");
};
}
//// evaluate an expression that is either an array like [mul, 3, 3] or a number
//function exp(x) {
// if (Array.isArray(x)) {
// return x[0](x[1],x[2]);
// }
// return x;
//}
//evaluate an expression that is either an array like [mul, 3, 3] or a number
function exp(x) {
if (Array.isArray(x)) {
var a = exp(x[1]);
var b = exp(x[2]);
return x[0](a,b);
}
return x;
}
//// invoke add on succssive arguments until reaching a null invocation
//function addg(x) {
// var acc = x;
// var recurse = function(y) {
// if (y === undefined) {
// return acc;
// }
// acc += y;
// return recurse;
// }
// return (x === undefined) ? acc : recurse;
//}
//invoke add on succssive arguments until reaching a null invocation
//function addg(x, y) {
// return (x === undefined) ? y : function(z) {
// return addg(z, (y === undefined) ? x : x + y);
// };
//}
// factory function to consume invocations through a binary function
function liftg(func) {
return function liftfunc(x, y) {
return (x === undefined) ? y : function(z) {
return liftfunc(z, (y === undefined) ? x : func(x,y));
};
};
}
var addg = liftg(add); // gonk add
//build up array from invocations
function arrayg(x, y) {
y = (y === undefined) ? [] : y;
return (x === undefined) ? y : function(z) {
y.push(x);
return arrayg(z, y);
};
}
function unaryc(func) {
return function(callback, arg) {
callback( func(arg) );
}
}
// --- Test cases that exercise the above functions are below
//log(identity(3));
//log(add(3,4));
//log(sub(3,4));
//log(mul(3,4));
//var idf3 = identityf(3);
//log(idf3());
//log( addf(3)(4) );
//var addf = liftf(add);
//log( addf(3)(4) ); // 7
//log( liftf(mul)(5)(6) ); // 30
//var add3 = curry(add, 3);
//log( add3(4) ); //7
//log( curry(mul,5)(6) ); //30
var inc = curry(add,1);
//var inc_1 = curry(add,1);
//var inc_2 = liftf(add)(1);
//var inc_3 = addf(1);
//log( inc_1(5) ); //6
//log( inc_1(inc_1(5)) ); //7
//log( inc_2(5) ); //6
//log( inc_2(inc_2(5)) ); //7
//log( inc_3(5) ); //6
//log( inc_3(inc_3(5)) ); //7
// log( add(11,11) ); //22
var doubl = twice(add);
// log( doubl(11) ); //22
var square = twice(mul);
// log( square(11) ); //121
// var bus = reverse(sub);
// log( bus(3,2) ); // -1
//log( composeu(doubl, square)(5) ); // 100
//log( composeb(add, mul)(2, 3, 7) ); // 35
// var add_once = once(add);
// log ( add_once(3,4) ); //7
// log ( add_once(3,5) ); //undefined
// var index = fromTo(0, 3);
// log( index() ); //0
// log( index() ); //1
// log( index() ); //2
// log( index() ); //undefined
//
//var ele = element(['a', 'b', 'c', 'd'], fromTo(1,3));
//log( ele() ); //b
//log( ele() ); //c
//log( ele() ); //undefined
//
//var elf = element(['a', 'b', 'c', 'd']);
//log( elf() ); //a
//log( elf() ); //b
//log( elf() ); //c
//log( elf() ); //d
//log( elf() ); //undefined
//var array = [],
// col = collect(fromTo(5,7), array);
//log( col() ); //5
//log( col() ); //g
//log( col() ); //undefined
//log( array ); // [5,6]
//var fil = filter(fromTo(0,5),
// function third(value) {
// return (value % 3) === 0;
// });
//log( fil() ); // 0
//log( fil() ); // 3
//log( fil() ); // undefined
//
//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
//var object = counter(10),
// next = object.next,
// prev = object.prev;
//log ( next() ); // 11
//log ( prev() ); // 10
//log ( prev() ); // 9
//log ( next() ); // 10
//var rev = revocable(identity),
// invoke = rev.invoke;
//log( invoke(7) ); //7
//rev.revoke();
//log( invoke(8) ); //undefined
//var geng = gensymf("G"),
// genh = gensymf("H");
//log( geng() ); // "G1"
//log( genh() ); // "H1"
//log( geng() ); // "G2"
//log( genh() ); // "H2"
//var gensymf = gensymff(inc, 0),
// geng = gensymf("G"),
// genh = gensymf("H");
//log( geng() ); // "G1"
//log( genh() ); // "H1"
//log( geng() ); // "G2"
//log( genh() ); // "H2"
//var fib = fibonaccif(0,1);
//log( fib() ); // 0
//log( fib() ); // 1
//log( fib() ); // 1
//log( fib() ); // 2
//log( fib() ); // 3
//log( fib() ); // 5
//log( JSON.stringify(m(1)) );
//log( JSON.stringify(m(Math.PI, "pi")) );
//log( JSON.stringify(addm(m(3),m(4))) );
//log( JSON.stringify(addm(m(1), m(Math.PI, "pi"))) );
//
//var addm = liftm(add, "+");
//log( JSON.stringify(addm(m(3),m(4))) );
//log( JSON.stringify(liftm(mul,"*")(m(3), m(4))) );
//var addm = liftm(add, "+");
//log( JSON.stringify(addm(3,4)) );
//log( JSON.stringify(addm(m(3),m(4))) );
//log( JSON.stringify(liftm(mul,"*")(m(3), m(4))) );
//var sae = [mul, 3, 3];
//log( exp(sae) ); // 9
//log( exp(42) ); // 42
//var nae = [
// Math.sqrt,
// [add, [square,3], [square,4]]
//];
//log( exp(nae) ); // 5
//log( addg() ); // undefined
//log( addg(2)() ); // 2
//log( addg(2)(7)() ); // 9
//log( addg(3)(4)(0)() ); // 7
//log( addg(1)(2)(4)(8)() ); // 15
//log( liftg(mul)() ); // undefined
//log( liftg(mul)(3)() ); // 3
//log( liftg(mul)(3)(4)(0)() ); // 0
//log( liftg(mul)(1)(2)(4)(8)() ); // 64
//log( arrayg() ); // []
//log( arrayg(3)() ); // [ 3 ]
//log( arrayg(3)(4)(5)() ); // [ 3, 4, 5 ]
sqrtc = unaryc(Math.sqrt);
sqrtc(log, 81); //9