-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
595 lines (506 loc) · 18.1 KB
/
list.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// AYSON, JUSTIN
// COMP 333 (MON, WED) - LAB 2
// COMP 333 Assignment 2
//
// WORKING WITH NODE
// You will need node.js (https://nodejs.org/en/) installed and working
// to run this. It's also possible, with some tweaking, to get it working
// in a Web browser. In that case, you probably will want to replace
// `console.log` with some output routine.
//
// To work with node from the command line, you can do the following:
// 1.) Go to the directory containing the file (using the cd command)
// 2.) Start node.js with the `node` command
// 3.) Within the node.js prompt, type `.load list.js` and hit enter.
// This will read your program.
// 4.) Run the tests by typing `runTests()` and hitting enter.
// This will execute the `runTests()` function in this file.
//
// ASSIGNMENT: IMMUTABLE SINGLY-LINKED LIST IMPLEMENTATION
// In this assignment, you'll be defining code for an immutable
// singly-linked list. Lists are constructed with two kinds of objects:
// - A `Cons` object represents a non-empty list. It holds a single
// element of the list, along with the rest of the list.
// - A `Nil` object represents an empty list, containing no elements,
// and no other elements.
// These objects are not wrapped around anything; if you take a list,
// you take `Cons` or `Nil` objects.
//
// This list is intended to be used in an immutable way. This means
// For example, there is an `append` operation, but `append` does
// not modify the list it was called on. Instead, `append` will
// return a new list, representing the result of the append. For
// example, if we append `[1, 2]` onto `[3, 4, 5]`, `append` will
// return a new list representing `[1, 2, 3, 4, 5]`, and the
// original lists `[1, 2]`, and `[3, 4, 5]` will be unchanged.
//
// Your goal with this assignment is to get all the tests to pass,
// without modifying any of the testing code. There are enough
// unit tests that they serve as a (possibly incomplete) specification
// of what the code needs to do. Some of the provided tests pass
// out of the box without any changes you need to do on your end.
//
// HINTS:
// 1.) The behaviors for `append`, `contains`, `length`, `filter`,
// and `map` differ depending on whether or not they are called
// on `Cons` or `Nil`. Some tests force you to use virtual
// dispatch to encode this difference.
// 2.) Singly-linked lists are a recursive data structure, and
// the methods can most naturally be implemented with recursion.
// 3.) My reference solution contains less than 50 lines of code.
// If you start needing dramatically more than 50 lines, talk
// to me to make sure you're on the right track.
//
// join
//
// Parameters:
// - A List of elements
// - A delimeter to separate them by
// Returns a single string, which results from calling
// toString on each element, separated by the delimeter.
//
// For example:
// join(new Nil(), ", ") // "[]"
// join(new Cons(1, new Nil()), ", ") // [1]
// join(new Cons(2, new Cons(3, new Nil())), // [2, 3]
//
function join(list, delim) {
let retval = "[";
while (list instanceof Cons &&
!(list.tail instanceof Nil)) {
retval += list.head.toString() + delim;
list = list.tail;
}
if (list instanceof Cons) {
retval += list.head.toString();
}
retval += "]";
return retval;
} // join
function List() {}
List.prototype.join = function(delim) {
return join(this, delim);
};
List.prototype.toString = function() {
return this.join(", ");
};
function Cons(head, tail) {
this.head = head;
this.tail = tail;
}
Cons.prototype = new List();
Cons.prototype.isEmpty = function() {
return false;
}
Cons.prototype.length = function() {
return 1 + this.tail.length();
}
Cons.prototype.append = function(obj) {
return new Cons(this.head, this.tail.append(obj));
}
Cons.prototype.contains = function(value) {
return this.head == value || this.tail.contains(value);
}
Cons.prototype.filter = function(f) {
if(f(this.head))
return new Cons(this.head, this.tail.filter(f));
else
return this.tail.filter(f);
}
Cons.prototype.map = function(f) {
return new Cons(f(this.head), this.tail.map(f));
}
function Nil() {}
Nil.prototype = new List();
Nil.prototype.isEmpty = function() {
return true;
}
Nil.prototype.length = function() {return 0;}
Nil.prototype.append = function(obj) {return obj;}
Nil.prototype.contains = function(value) {return false;}
Nil.prototype.filter = function(f) {return this;}
Nil.prototype.map = function(f) {return this;}
// ---BEGIN CODE FOR TESTING---
// Do not modify! When I test your code myself,
// I won't use this code below, so I won't be working
// with any of your modifications!
function runTest(test) {
process.stdout.write(test.name + ": ");
try {
test();
process.stdout.write("pass\n");
} catch (error) {
process.stdout.write("FAIL\n");
console.log(error);
}
} // runTest
function assertEquals(expected, received) {
if (expected !== received) {
throw ("\tExpected: " + expected.toString() + "\n" +
"\tReceived: " + received.toString());
}
} // assertEquals
function test_nil_join() {
let nil = new Nil();
assertEquals("[]",
nil.join(", "));
} // test_nil_join
function test_nil_toString() {
let nil = new Nil();
assertEquals("[]",
nil.toString());
} // test_nil_toString
function test_nil_instanceof_list() {
let nil = new Nil();
assertEquals(true,
nil instanceof List);
} // test_nil_instanceof_list
function test_nil_has_no_head() {
let nil = new Nil();
assertEquals(false,
nil.hasOwnProperty("head"));
} // test_nil_has_no_head
function test_nil_has_no_tail() {
let nil = new Nil();
assertEquals(false,
nil.hasOwnProperty("tail"));
} // test_nil_has_no_tail
function test_nil_isEmpty() {
let nil = new Nil();
assertEquals(true,
nil.isEmpty());
} // test_nil_isEmpty
function test_nil_length() {
let nil = new Nil();
assertEquals(0,
nil.length());
} // test_nil_length
function test_nil_filter() {
let nil = new Nil();
let f = function(e) { return true; };
assertEquals("[]", nil.filter(f).toString());
} // test_nil_filter
function test_nil_map() {
let nil = new Nil();
let increment = function(e) { return e + 1; };
assertEquals("[]", nil.map(increment).toString());
} // test_nil_map
function test_cons_instanceof_list() {
let list = new Cons(1, new Nil());
assertEquals(true,
list instanceof List);
} // test_cons_instanceof_list
function test_cons_join_single_element() {
let list = new Cons(1, new Nil());
assertEquals("[1]",
list.join(":"));
} // test_cons_join_single_element
function test_cons_join_two_elements() {
let list = new Cons(1, new Cons(2, new Nil()));
assertEquals("[1:2]",
list.join(":"));
} // test_cons_join_two_elements
function test_cons_join_three_elements() {
let list = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals("[1:2:3]",
list.join(":"));
} // test_cons_join_three_elements
function test_cons_toString_single_element() {
let list = new Cons(1, new Nil());
assertEquals("[1]",
list.toString());
} // test_cons_toString_single_element
function test_cons_toString_two_elements() {
let list = new Cons(1, new Cons(2, new Nil()));
assertEquals("[1, 2]",
list.toString());
} // test_cons_toString_two_elements
function test_cons_toString_three_elements() {
let list = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals("[1, 2, 3]",
list.toString());
} // test_cons_toString_three_elements
function test_cons_head() {
let list = new Cons(1, new Nil());
assertEquals(1,
list.head);
} // test_cons_head
function test_cons_empty_tail() {
let list = new Cons(1, new Nil());
assertEquals("[]",
list.tail.toString());
} // test_cons_empty_tail
function test_cons_nonempty_tail() {
let list = new Cons(1, new Cons(2, new Nil()));
assertEquals("[2]",
list.tail.toString());
} // test_cons_nonempty_tail
function test_cons_isEmpty() {
let list = new Cons(1, new Nil());
assertEquals(false,
list.isEmpty());
} // test_cons_isEmpty
function test_cons_length_1() {
let list = new Cons("a", new Nil());
assertEquals(1,
list.length());
} // test_cons_length_1
function test_cons_length_2() {
let list = new Cons("a", new Cons("b", new Nil()));
assertEquals(2,
list.length());
} // test_cons_length_2
function test_cons_filter_has_element() {
let list = new Cons(1, new Nil());
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[1]",
list.filter(isOdd).toString());
} // test_cons_filter_has_element
function test_cons_filter_has_no_element() {
let list = new Cons(2, new Nil());
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[]",
list.filter(isOdd).toString());
} // test_cons_filter_has_no_element
function test_cons_filter_multi_1() {
let list = new Cons(2, new Cons(4, new Nil()));
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[]",
list.filter(isOdd).toString());
} // test_cons_filter_multi_1
function test_cons_filter_multi_2() {
let list = new Cons(2, new Cons(5, new Nil()));
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[5]",
list.filter(isOdd).toString());
} // test_cons_filter_multi_2
function test_cons_filter_multi_3() {
let list = new Cons(3, new Cons(4, new Nil()));
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[3]",
list.filter(isOdd).toString());
} // test_cons_filter_multi_3
function test_cons_filter_multi_4() {
let list = new Cons(3, new Cons(5, new Nil()));
let isOdd = function(e) { return e % 2 == 1; };
assertEquals("[3, 5]",
list.filter(isOdd).toString());
} // test_cons_filter_multi_4
function test_cons_filter_multi_5() {
let list = new Cons(1, new Cons(5, new Cons(2, new Cons(6, new Cons(3, new Cons(7, new Cons(4, new Nil())))))));
let f = function(e) { return e < 6; }
assertEquals("[1, 5, 2, 3, 4]",
list.filter(f).toString());
} // test_cons_filter_multi_5
function test_nil_nil_append() {
let nil1 = new Nil();
let nil2 = new Nil();
assertEquals("[]",
nil1.append(nil2).toString());
} // test_nil_nil_append
function test_cons_map_1() {
let list = new Cons(1, new Nil());
let increment = function(e) { return e + 1; };
assertEquals("[2]", list.map(increment).toString());
} // test_cons_map_1
function test_cons_map_2() {
let list = new Cons(1, new Cons(2, new Nil()));
let increment = function(e) { return e + 1; };
assertEquals("[2, 3]", list.map(increment).toString());
} // test_cons_map_2
function test_cons_map_3() {
let list = new Cons(2, new Cons(5, new Nil()));
let multBy3 = function(e) { return e * 3; };
assertEquals("[6, 15]", list.map(multBy3).toString());
} // test_cons_map_3
function test_cons_map_4() {
let list = new Cons("alpha", new Cons("beta", new Cons("gamma", new Nil())));
let identity = function(e) { return e; };
assertEquals("[alpha, beta, gamma]", list.map(identity).toString());
} // test_cons_map_4
function test_nil_cons_append() {
let nil = new Nil();
let list = new Cons(1, new Cons(2, new Nil()));
assertEquals("[1, 2]",
nil.append(list).toString());
} // test_nil_cons_append
function test_cons_nil_append() {
let list = new Cons(1, new Cons(2, new Nil()));
let nil = new Nil();
assertEquals("[1, 2]",
list.append(nil).toString());
} // test_cons_nil_append
function test_cons_cons_append_1() {
let list1 = new Cons(1, new Cons(2, new Nil()));
let list2 = new Cons(3, new Cons(4, new Cons(5, new Nil())));
assertEquals("[1, 2, 3, 4, 5]",
list1.append(list2).toString());
} // test_cons_cons_append_1
function test_cons_cons_append_2() {
let list1 = new Cons(1, new Cons(2, new Nil()));
let list2 = new Cons(3, new Cons(4, new Cons(5, new Nil())));
assertEquals("[3, 4, 5, 1, 2]",
list2.append(list1).toString());
} // test_cons_cons_append_2
function test_nil_contains() {
let nil = new Nil();
assertEquals(false,
nil.contains(1));
} // test_nil_contains
function test_cons_contains_first() {
let list = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals(true,
list.contains(1));
} // test_cons_contains_first
function test_cons_contains_second() {
let list = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals(true,
list.contains(2));
} // test_cons_contains_second
function test_cons_contains_nowhere() {
let list = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals(false,
list.contains(4));
} // test_cons_contains_nowhere
function test_nil_and_cons_have_different_prototypes() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
Object.getPrototypeOf(nil) == Object.getPrototypeOf(cons));
} // test_nil_and_cons_have_different_prototypes
function getGrandparent(obj) {
return Object.getPrototypeOf(Object.getPrototypeOf(obj));
} // getGrandparent
function test_nil_and_cons_have_same_grandparent_prototypes() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(getGrandparent(nil),
getGrandparent(cons));
} // test_nil_and_cons_have_same_grandparent_prototypes
function test_nil_grandparent_prototype_has_join() {
let nil = new Nil();
assertEquals(true,
getGrandparent(nil).hasOwnProperty("join"));
} // test_nil_grandparent_prototype_has_join
function test_nil_grandparent_prototype_has_toString() {
let cons = new Cons(1, new Nil());
assertEquals(true,
getGrandparent(cons).hasOwnProperty("toString"));
} // test_nil_grandparent_prototype_has_toString
function test_nil_and_cons_have_different_isEmpty() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.isEmpty == cons.isEmpty);
} // test_nil_and_cons_have_different_isEmpty
function test_nil_and_cons_have_different_append() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.append == cons.append);
} // test_nil_and_cons_have_different_append
function test_nil_and_cons_have_different_contains() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.contains == cons.contains);
} // test_nil_and_cons_have_different_contains
function test_nil_and_cons_have_different_length() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.length == cons.length);
} // test_nil_and_cons_have_different_length
function test_nil_and_cons_have_different_filter() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.filter == cons.filter);
} // test_nil_and_cons_have_different_filter
function test_nil_and_cons_have_different_map() {
let nil = new Nil();
let cons = new Cons(1, new Nil());
assertEquals(false,
nil.map == cons.map);
} // test_nil_and_cons_have_different_map
function runTests() {
// ---begin tests for nil---
// instanceof
runTest(test_nil_instanceof_list);
// join
runTest(test_nil_join);
// toString
runTest(test_nil_toString);
// head
runTest(test_nil_has_no_head);
// tail
runTest(test_nil_has_no_tail);
// isEmpty
runTest(test_nil_isEmpty);
// length
runTest(test_nil_length);
// append
runTest(test_nil_nil_append);
runTest(test_nil_cons_append);
// contains
runTest(test_nil_contains);
// filter
runTest(test_nil_filter);
// map
runTest(test_nil_map);
// ---end tests for nil---
// ---begin tests for cons---
// join
runTest(test_cons_join_single_element);
runTest(test_cons_join_two_elements);
runTest(test_cons_join_three_elements);
// toString
runTest(test_cons_toString_single_element);
runTest(test_cons_toString_two_elements);
runTest(test_cons_toString_three_elements);
// instanceof
runTest(test_cons_instanceof_list);
// head
runTest(test_cons_head);
// tail
runTest(test_cons_empty_tail);
runTest(test_cons_nonempty_tail);
runTest(test_cons_isEmpty);
// length
runTest(test_cons_length_1);
runTest(test_cons_length_2);
// append
runTest(test_cons_nil_append);
runTest(test_cons_cons_append_1);
runTest(test_cons_cons_append_2);
// contains
runTest(test_cons_contains_first);
runTest(test_cons_contains_second);
runTest(test_cons_contains_nowhere);
// filter
runTest(test_cons_filter_has_element);
runTest(test_cons_filter_has_no_element);
runTest(test_cons_filter_multi_1);
runTest(test_cons_filter_multi_2);
runTest(test_cons_filter_multi_3);
runTest(test_cons_filter_multi_4);
runTest(test_cons_filter_multi_5);
// map
runTest(test_cons_map_1);
runTest(test_cons_map_2);
runTest(test_cons_map_3);
runTest(test_cons_map_4);
// ---end tests for cons---
// ---begin tests relating to prototypes---
runTest(test_nil_and_cons_have_different_prototypes);
runTest(test_nil_and_cons_have_same_grandparent_prototypes);
runTest(test_nil_grandparent_prototype_has_join);
runTest(test_nil_grandparent_prototype_has_toString);
runTest(test_nil_and_cons_have_different_isEmpty);
runTest(test_nil_and_cons_have_different_append);
runTest(test_nil_and_cons_have_different_contains);
runTest(test_nil_and_cons_have_different_length);
runTest(test_nil_and_cons_have_different_filter);
runTest(test_nil_and_cons_have_different_map);
// ---end tests relating to prototypes---
} // runTests