-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes-collections.js
218 lines (171 loc) · 5.23 KB
/
types-collections.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
/**
* @flow
*/
import * as Immutable from 'immutable'
import Iterable from './iterable'
import {Lens, makeLens} from './types-lib'
import type {Updater} from './types-lib'
/**
* Immutable map class based on Immutable.Map, but with some extra features like
* type-safe lenses.
*/
export class IMap<K, V> extends Iterable<[K, V]> {
backingMap: Immutable.Map<K, V>;
size: number;
constructor(backingMap: Immutable.Map<K, V>) {
super();
this.backingMap = backingMap;
this.size = backingMap.size;
}
static make<K, V>(...args): IMap<K, V> {
return new IMap(new Immutable.Map(...args));
}
set(key: K, value: V): IMap<K, V> {
return new IMap(this.backingMap.set(key, value));
}
update(key: K, updater: Updater<V>): IMap<K, V> {
return new IMap(this.backingMap.update(key, updater));
}
get(key: K): V {
return this.backingMap.get(key);
}
keys(): Iterator<K> {
return this.backingMap.keys();
}
hasKey(key: K): boolean {
return this.backingMap.has(key);
}
isEmpty(): boolean {
return this.backingMap.isEmpty();
}
delete(key: K): IMap<K, V> {
return new IMap(this.backingMap.delete(key));
}
lens(): MapLens<K, V, IMap<K, V>> {
return this.makeLens((newMap) => newMap);
}
makeLens<Result>(replace: (map: IMap<K, V>) => Result):
MapLens<K, V, Result> {
return new MapLens(this, replace);
}
iterator(): Iterator<[K, V]> {
return (this.backingMap: any)[Symbol.iterator]();
}
equals(other: IMap<K, V>): boolean {
return this.backingMap.equals((other.backingMap: any));
}
hashCode(): number {
return this.backingMap.hashCode();
}
toString(): string {
return this.backingMap.toString();
}
}
export class MapLens<K, V, Result> extends Lens<IMap<K, V>, Result> {
atKey(key: K): Lens<V, Result> {
const replaceChild = newChildVal =>
this.replace(this.value.set(key, newChildVal));
return makeLens(this.value.get(key), replaceChild);
}
deleteKey(key: K): Result {
return this.replace(this.value.delete(key));
}
}
export class IList<T> extends Iterable<T> {
backingList: Immutable.List<T>;
size: number;
constructor(backingList: Immutable.List<T>) {
super();
this.backingList = backingList;
this.size = backingList.size;
}
static make<T>(...args): IList<T> {
return new IList(new Immutable.List(...args));
}
set(index: number, value: T): IList<T> {
return new IList(this.backingList.set(index, value));
}
push(value: T): IList<T> {
return new IList(this.backingList.push(value));
}
pop(): IList<T> {
return new IList(this.backingList.pop());
}
update(index: number, updater: Updater<T>): IList<T> {
return new IList(this.backingList.update(index, updater));
}
map<U>(mapper: (t: T, i: number) => U): IList<U> {
return new IList(this.backingList.map(mapper));
}
slice(begin?: number, end?: number): IList<T> {
return new IList(this.backingList.slice(begin, end));
}
get(index: number): T {
return this.backingList.get(index);
}
toArray(): Array<T> {
return this.backingList.toArray();
}
sort(): IList<T> {
return new IList(this.backingList.sort());
}
lens(): ListLens<T, IList<T>> {
return this.makeLens((newList) => newList);
}
makeLens<Result>(replace: (list: IList<T>) => Result):
ListLens<T, Result> {
return new ListLens(this, replace);
}
iterator(): Iterator<T> {
return (this.backingList: any)[Symbol.iterator]();
}
equals(other: IList<T>): boolean {
return this.backingList.equals((other.backingList: any));
}
hashCode(): number {
return this.backingList.hashCode();
}
toString(): string {
return this.backingList.toString();
}
}
export class ListLens<T, Result> extends Lens<IList<T>, Result> {
atIndex(index: number): Lens<T, Result> {
const replaceChild = newChildVal =>
this.replace(this.value.set(index, newChildVal));
return makeLens(this.value.get(index), replaceChild);
}
}
export class ISet<T> extends Iterable<T> {
backingSet: Immutable.Set<T>;
size: number;
constructor(backingSet: Immutable.Set<T>) {
super();
this.backingSet = backingSet;
this.size = backingSet.size;
}
static make<K, V>(...args): ISet<T> {
return new ISet(new Immutable.Set(...args));
}
add(value: T): ISet<T> {
return new ISet(this.backingSet.add(value));
}
has(value: T): boolean {
return this.backingSet.has(value);
}
union(other: Iterator<T>): ISet<T> {
return new ISet(this.backingSet.union(other));
}
iterator(): Iterator<T> {
return (this.backingSet: any)[Symbol.iterator]();
}
equals(other: ISet<T>): boolean {
return this.backingSet.equals((other.backingSet: any));
}
hashCode(): number {
return this.backingSet.hashCode();
}
toString(): string {
return this.backingSet.toString();
}
}