-
Notifications
You must be signed in to change notification settings - Fork 14
/
list.js
351 lines (291 loc) · 8.16 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
import {Typed, Type, Union, Any, typeOf, construct} from "./typed"
import * as Immutable from 'immutable'
const ImmutableList = Immutable.List
const {Indexed} = Immutable.Iterable
const $store = Typed.store
const $type = Typed.type
const $read = Typed.read
const $step = Typed.step
const $init = Typed.init
const $result = Typed.result
const $label = Typed.label
const $typeName = Typed.typeName
const $empty = Typed.empty
const change = (list, f) => {
const store = f(list[$store])
if (store === list[$store]) {
return list
} else {
const result = list.__ownerID ? list : construct(list)
result[$store] = store
result.size = store.size
return result
}
}
const clear = target => target.clear()
const pop = target => target.pop()
const shift = target => target.shift()
class TypeInferer extends Type {
[Typed.typeName]() {
return 'TypeInferer'
}
[Typed.read](value) {
// typeOf usually creates type for the value with that
// value being a default. For type inference we should
// actually use a base type instead of type with default
// there for we use prototype of the constructor.
const type = typeOf(value).constructor.prototype
this.type = this.type ? Union(this.type, type) : type
return value
}
}
function BaseImmutableList() {}
BaseImmutableList.prototype = ImmutableList.prototype
class TypeInferedList extends BaseImmutableList {
static from(list) {
const result = construct(this.prototype)
result[$store] = list[$store]
result.size = list.size
return result
}
constructor(value) {
super();
return TypeInferedList.prototype[$read](value)
}
[Typed.init]() {
const result = construct(this).asMutable()
result[$type] = new TypeInferer()
return result
}
[Typed.result](result) {
const list = result.asImmutable()
list[$type] = result[$type].type
return list
}
[Typed.read](input) {
const Type = this.constructor
if (input === null || input === void(0)) {
if (!this[$empty]) {
const result = construct(this)
result[$store] = ImmutableList()
result.size = 0
this[$empty] = result
}
return this[$empty]
}
if (input instanceof Type && input && input.constructor === Type) {
return input
}
const source = Indexed(input)
const isEmpty = source.size === 0
if (isEmpty && this[$empty]) {
return this[$empty]
}
let list = this[$init]()
list.size = source.size
source.forEach((value, index) => {
list.set(index, value)
})
list = this[$result](list)
if (isEmpty) {
this[$empty] = list
}
return list
}
[Typed.step](result, [key, value]) {
return change(result, (store=ImmutableList()) => store.set(key, value))
}
[Typed.typeName]() {
return this[$label] || `Typed.List(${this[$type][$typeName]()})`
}
toString() {
return this.__toString(this[$typeName]() + '([', '])')
}
has(key) {
return this[$store].has(key)
}
get(index, notSetValue) {
return this[$store] ? this[$store].get(index, notSetValue) :
notSetValue
}
clear() {
if (this.__ownerID) {
return change(this, clear)
}
return this[$empty] || this[$read]()
}
remove(index) {
return change(this, store => store && store.remove(index))
}
set(index, value) {
if (index > this.size) {
throw TypeError(`Index "${index}" is out of bound`)
}
const result = this[$type][$read](value)
if (result instanceof TypeError) {
throw TypeError(`Invalid value: ${result.message}`)
}
return this[$step](this, [index, result])
}
push(...values) {
const type = this[$type]
const items = []
const count = values.length
let index = 0
while (index < count) {
const value = values[index]
const result = type[$read](value)
if (result instanceof TypeError) {
throw TypeError(`Invalid value: ${result.message}`)
}
items.push(result)
index = index + 1
}
return change(this, store =>
store ? store.push(...items) : ImmutableList(...items))
}
pop() {
return change(this, pop)
}
unshift(...values) {
const type = this[$type]
const items = []
const count = values.length
let index = 0
while (index < count) {
const value = values[index]
const result = type[$read](value)
if (result instanceof TypeError) {
throw TypeError(`Invalid value: ${result.message}`)
}
items.push(result)
index = index + 1
}
return change(this, store =>
store ? store.unshift(...items) : ImmutableList(...items))
}
shift() {
return change(this, shift)
}
setSize(size) {
if (size > this.size) {
throw TypeError(`setSize may only downsize`)
}
return change(this, store => store.setSize(size))
}
slice(begin, end) {
return change(this, store => store && store.slice(begin, end))
}
wasAltered() {
return this[$store].wasAltered()
}
__ensureOwner(ownerID) {
const result = this.__ownerID === ownerID ? this :
!ownerID ? this :
construct(this)
result.__ownerID = ownerID
result[$store] = this[$store] ? this[$store].__ensureOwner(ownerID) :
ImmutableList().__ensureOwner(ownerID)
result.size = result[$store].size
return result
}
__iterator(type, reverse) {
return Indexed(this[$store]).map((_, key) => this.get(key)).__iterator(type, reverse)
}
__iterate(f, reverse) {
return Indexed(this[$store]).map((_, key) => this.get(key)).__iterate(f, reverse)
}
}
TypeInferedList.prototype[Typed.DELETE] = TypeInferedList.prototype.remove;
const BaseTypeInferedList = function() {}
BaseTypeInferedList.prototype = TypeInferedList.prototype
class TypedList extends BaseTypeInferedList {
constructor() {
super()
}
[Typed.init]() {
return construct(this).asMutable()
}
[Typed.result](result) {
return result.asImmutable()
}
map(mapper, context) {
if (this.size === 0) {
return this
} else {
const result = TypeInferedList.from(this).map(mapper, context)
if (this[$store] === result[$store]) {
return this
}
if (result[$type] === this[$type]) {
const list = construct(this)
list[$store] = result[$store]
list.size = result.size
return list
} else {
return result
}
}
}
flatMap(mapper, context) {
if (this.size === 0) {
return this
} else {
const result = TypeInferedList.from(this).flatMap(mapper, context)
if (this[$store] === result[$store]) {
return this
}
if (result[$type] === this[$type]) {
const list = construct(this)
list[$store] = result[$store]
list.size = result.size
return list
} else {
return result
}
}
}
}
export const List = function(descriptor, label) {
if (descriptor === void(0)) {
throw TypeError("Typed.List must be passed a type descriptor")
}
if (descriptor === Any) {
return Immutable.List
}
const type = typeOf(descriptor)
if (type === Any) {
throw TypeError(`Typed.List was passed an invalid type descriptor: ${descriptor}`)
}
const ListType = function(value) {
const isListType = this instanceof ListType
const Type = isListType ? this.constructor : ListType
if (value instanceof Type) {
return value
}
const result = Type.prototype[$read](value)
if (result instanceof TypeError) {
throw result
}
// `list.map(f)` will in fact cause `list.constructor(items)` to be
// invoked there for we need to check if `this[$store]` was
// assigned to know if it's that or if it's a `new ListType()` call.
if (isListType && !this[$store]) {
this[$store] = result[$store]
this.size = result.size
} else {
return result
}
return this
}
ListType.of = ImmutableList.of
ListType.prototype = Object.create(ListPrototype, {
constructor: {value: ListType},
[$type]: {value: type},
[$label]: {value: label}
})
return ListType
}
List.Type = TypedList
List.prototype = TypedList.prototype
const ListPrototype = TypedList.prototype