-
Notifications
You must be signed in to change notification settings - Fork 0
/
kollections.js
99 lines (78 loc) · 1.3 KB
/
kollections.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
class List extends Array {
constructor(items) {
super(...items)
}
[Symbol.iterator]() {
let i = 0;
return {
next: () => {
if (i < this.length) {
return {value: this[i++], done: false}
} else {
return {done: true}
}
}
}
}
get size() {
return this.length
}
get first() {
return this[0]
}
get last() {
return this[this.size - 1]
}
get(i) {
return this[i]
}
// mutable!!
set(i, k) {
this[i] = k
}
// mutable!!
add(k) {
this.push(k)
}
// mutable!!
remove(i) {
// this = this.slice(0, i) + this.slice(i+1)
}
contains(k) {
return k in this
}
isEmpty() {
return this.size === 0
}
joinToString(sep = ',') {
return this.join(sep)
}
reverse() {
return new List(this.toArray().reverse())
}
filter(cb) {
return new List(this.toArray().filter(cb))
}
forEach(cb) {
this.toArray().forEach(cb)
}
onEach(cb) {
this.toArray().forEach(cb)
return this
}
map(cb) {
return new List(this.toArray().map(cb))
}
toString() {
return `List [${this.toArray()}]`
}
toArray() {
return [...this]
}
get [Symbol.toStringTag]() {
return 'List'
}
}
export function listOf(...items) {
return new List(items)
}