-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
range2.ts
195 lines (154 loc) · 4.96 KB
/
range2.ts
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
namespace $ {
/** Lazy computed lists with native Array interface. $mol_range2_array is mutable but all derived ranges are immutable. */
export function $mol_range2< Item = number >(
item: Item[] | ( ( index : number )=> Item ) = index => index as any ,
size = ()=> Number.POSITIVE_INFINITY ,
): Item[] {
const source = typeof item === 'function' ? new $mol_range2_array< Item >() : item
if( typeof item !== 'function' ) {
item = index => source[ index ]
size = ()=> source.length
}
return new Proxy( source , {
get( target , field ) {
if( typeof field === 'string' ) {
if( field === 'length' ) return size()
const index = Number( field )
if( index < 0 ) return undefined
if( index >= size() ) return undefined
if( index === Math.trunc( index ) ) return ( item as any )( index )
}
return $mol_range2_array.prototype[ field as any ]
} ,
set( target , field ) {
return $mol_fail( new TypeError( `Lazy range is read only (trying to set field ${ JSON.stringify( field ) })` ) )
} ,
ownKeys( target ) {
return [ ... Array( size() ) ].map( ( v, i ) => String( i ) ).concat( 'length' )
} ,
getOwnPropertyDescriptor( target , field ) : PropertyDescriptor | undefined {
if( field === "length" ) return {
value : size() ,
writable : true ,
enumerable : false ,
configurable : false ,
}
const index = Number( field )
if( index === Math.trunc( index ) ) return {
get : ()=> this.get!( target , field , this ) ,
enumerable : true ,
configurable : true ,
}
return Object.getOwnPropertyDescriptor( target , field )
}
} )
}
export class $mol_range2_array< Item > extends Array< Item > {
// Lazy
concat( ... tail : Item[][] ) : Item[] {
if( tail.length === 0 ) return this as any
if( tail.length > 1 ) {
let list = this as any
for( let item of tail ) list = list.concat( item )
return list
}
return $mol_range2(
index => index < this.length ? this[ index ] : tail[0][ index - this.length ] ,
()=> this.length + tail[0].length ,
)
}
// Lazy
filter< Context > (
check : ( val : Item , index : number , list : Item[] )=> boolean ,
context? : Context ,
) {
const filtered = [] as Item[]
let cursor = -1
return $mol_range2(
index => {
while( cursor < this.length && index >= filtered.length - 1 ) {
const val = this[ ++ cursor ]
if( check( val, cursor, this ) ) filtered.push( val )
}
return filtered[ index ]
},
()=> cursor < this.length ? Number.POSITIVE_INFINITY : filtered.length,
)
}
// Diligent
forEach< Context > (
proceed : ( this : Context , val : Item , index : number , list : Item[] )=> void ,
context? : Context,
) {
for( let [ key , value ] of this.entries() ) proceed.call( context as Context , value , key , this )
}
// Lazy
map< Item_out , Context > (
proceed : ( this : Context , val : Item , index : number , list : Item[] )=> Item_out ,
context? : Context ,
) : Item_out[] {
return $mol_range2(
index => proceed.call( context as Context , this[ index ] , index , this ) ,
()=> this.length ,
)
}
// Diligent
reduce< Result > (
merge : ( result : Result , val : Item , index : number , list : Item[] )=> Result ,
result? : Result ,
) {
let index = 0
if( arguments.length === 1 ) {
result = this[ index ++ ] as any
}
for( ; index < this.length ; ++ index ) {
result = merge( result as Result , this[ index ] , index , this as any )
}
return result
}
// Lazy
toReversed(): Item[] {
return $mol_range2(
index => this[ this.length - 1 - index ] ,
()=> this.length ,
)
}
// Lazy
slice( from = 0 , to = this.length ) {
return $mol_range2(
index => this[ from + index ] ,
()=> Math.min( to , this.length ) - from ,
)
}
// Lazy
some< Context > (
check : ( this : Context , val : Item , index : number , list : Item[] )=> boolean ,
context? : Context ,
) {
for( let index = 0 ; index < this.length ; ++ index ) {
if( check.call( context as Context , this[ index ] , index , this ) ) return true
}
return false
}
// Lazy
every< Narrow extends Item, Context = null >( check: ( value: Item, index: number, array: Item[] )=> value is Narrow, context?: Context ): this is Narrow[]
every< Context = null > (
check : ( this : Context , val : Item , index : number , list : Item[] )=> boolean ,
context? : Context ,
) {
for( let index = 0 ; index < this.length ; ++ index ) {
if( !check.call( context as Context , this[ index ] , index , this as any as Item[] ) ) return false
}
return true
}
reverse() {
return $mol_fail( new TypeError( `Mutable reverse is forbidden. Use toReversed instead.` ) )
}
sort() {
return $mol_fail( new TypeError( `Mutable sort is forbidden. Use toSorted instead.` ) )
}
[Symbol.toPrimitive]() {
return $mol_guid()
}
}
}