This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
iterable_groups.js
82 lines (64 loc) · 2.02 KB
/
iterable_groups.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
/*
3 - Iterable Groups (6/1/2019)
Make the Group class from the previous exercise iterable. Refer to the section about the iterator interface earlier in the chapter if you aren’t clear on the exact form of the interface anymore.
If you used an array to represent the group’s members, don’t just return the iterator created by calling the Symbol.iterator method on the array. That would work, but it defeats the purpose of this exercise.
It is okay if your iterator behaves strangely when the group is modified during iteration.
*/
class Group {
constructor() {
this.values = [];
}
add(value) {
if (!this.has(value)) {
this.values.push(value);
}
}
delete(value) {
if (this.has(value)) {
const removeDelete = this.values.indexOf(value);
delete this.values[removeDelete];
}
}
has(value) {
for (let member of this.values) {
if (member === value) {
return true;
}
}
return false;
}
static from(iterable) {
let group = new Group();
for (let element of iterable) {
group.add(element);
}
return group;
}
[Symbol.iterator]() {
// Makes Group class iterable and thus, can be utilized in for/of loops.
return new GroupIterator(this);
}
}
class GroupIterator {
constructor(group) {
this.currentIndex = 0;
this.group = group;
}
next() {
// next() returns an object containing two properties: `value` (Any type) & `done` (boolean).
if (this.currentIndex == this.group.values.length) {
return {done: true}; // Equivalent to {value: undefined, done: true}
}
else {
let value = this.group.values[this.currentIndex];
this.currentIndex += 1;
return {value, done: false};
}
}
}
for (let value of Group.from(["a", "b", "c"])) {
console.log(value);
}
// → a
// → b
// → c