12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- ///|
16
- ///
17
- /// A `@array.View` represents a view into a section of an array without copying the data.
18
- ///
19
- /// # Example
20
- ///
21
- /// ```moonbit
22
- /// let arr = [1, 2, 3, 4, 5]
23
- /// let view = arr[1:4] // Creates a view of elements at indices 1,2,3
24
- /// assert_eq!(view[0], 2)
25
- /// assert_eq!(view.length(), 3)
26
- /// ```
27
- pub typealias View [T ] = ArrayView [T ]
28
-
29
15
///|
30
16
/// Reverses the elements in the array view in place.
31
17
///
@@ -42,7 +28,7 @@ pub typealias View[T] = ArrayView[T]
42
28
/// inspect!(arr, content="[5, 4, 3, 2, 1]")
43
29
/// }
44
30
/// ```
45
- pub fn View ::rev_inplace [T ](self : View [T ]) -> Unit {
31
+ pub fn ArrayView ::rev_inplace [T ](self : ArrayView [T ]) -> Unit {
46
32
let mid_len = self .length () / 2
47
33
for i = 0 ; i < mid_len ; i = i + 1 {
48
34
let j = self .length () - i - 1
@@ -69,7 +55,7 @@ pub fn View::rev_inplace[T](self : View[T]) -> Unit {
69
55
/// inspect!(sum, content="6")
70
56
/// }
71
57
/// ```
72
- pub fn View ::each [T ](self : View [T ], f : (T ) -> Unit ) -> Unit {
58
+ pub fn ArrayView ::each [T ](self : ArrayView [T ], f : (T ) -> Unit ) -> Unit {
73
59
for v in self {
74
60
f (v )
75
61
}
@@ -86,7 +72,7 @@ pub fn View::each[T](self : View[T], f : (T) -> Unit) -> Unit {
86
72
/// v.eachi(fn (i, x) { sum = sum + x + i })
87
73
/// assert_eq!(sum, 15)
88
74
/// ```
89
- pub fn View ::eachi [T ](self : View [T ], f : (Int , T ) -> Unit ) -> Unit {
75
+ pub fn ArrayView ::eachi [T ](self : ArrayView [T ], f : (Int , T ) -> Unit ) -> Unit {
90
76
for i , v in self {
91
77
f (i , v )
92
78
}
@@ -102,7 +88,7 @@ pub fn View::eachi[T](self : View[T], f : (Int, T) -> Unit) -> Unit {
102
88
/// assert_false!(v[:].all(fn(elem) { elem % 2 == 0 }))
103
89
/// assert_true!(v[1:4].all(fn(elem) { elem % 2 == 0 }))
104
90
/// ```
105
- pub fn View ::all [T ](self : View [T ], f : (T ) -> Bool ) -> Bool {
91
+ pub fn ArrayView ::all [T ](self : ArrayView [T ], f : (T ) -> Bool ) -> Bool {
106
92
for v in self {
107
93
if not (f (v )) {
108
94
return false
@@ -121,7 +107,7 @@ pub fn View::all[T](self : View[T], f : (T) -> Bool) -> Bool {
121
107
/// assert_true!(v.any(fn(ele) { ele < 6 }))
122
108
/// assert_false!(v.any(fn(ele) { ele < 1 }))
123
109
/// ```
124
- pub fn View ::any [T ](self : View [T ], f : (T ) -> Bool ) -> Bool {
110
+ pub fn ArrayView ::any [T ](self : ArrayView [T ], f : (T ) -> Bool ) -> Bool {
125
111
for v in self {
126
112
if f (v ) {
127
113
return true
@@ -151,7 +137,7 @@ pub fn View::any[T](self : View[T], f : (T) -> Bool) -> Bool {
151
137
/// inspect!(arr.contains(6), content="false")
152
138
/// }
153
139
/// ```
154
- pub fn View ::contains [T : Eq ](self : View [T ], value : T ) -> Bool {
140
+ pub fn ArrayView ::contains [T : Eq ](self : ArrayView [T ], value : T ) -> Bool {
155
141
for v in self {
156
142
if v == value {
157
143
break true
@@ -160,224 +146,3 @@ pub fn View::contains[T : Eq](self : View[T], value : T) -> Bool {
160
146
false
161
147
}
162
148
}
163
-
164
- ///|
165
- /// Returns an iterator that yields each element of the array view in sequence
166
- /// from start to end.
167
- ///
168
- /// Parameters:
169
- ///
170
- /// * `array_view` : The array view to iterate over.
171
- ///
172
- /// Returns an iterator that yields elements of type `A` from the array view.
173
- ///
174
- /// Example:
175
- ///
176
- /// ```moonbit
177
- /// test "View::iter" {
178
- /// let arr = [1, 2, 3]
179
- /// let view = arr[1:]
180
- /// let mut sum = 0
181
- /// view.iter().each(fn(x) { sum = sum + x })
182
- /// inspect!(sum, content="5")
183
- /// }
184
- /// ```
185
- pub fn View ::iter [A ](self : View [A ]) -> Iter [A ] {
186
- Iter ::new (fn (yield_ ) {
187
- for v in self {
188
- guard let IterContinue = yield_ (v ) else { x => break x }
189
-
190
- } else {
191
- IterContinue
192
- }
193
- })
194
- }
195
-
196
- ///|
197
- /// Fold out values from an View according to certain rules.
198
- ///
199
- /// # Example
200
- /// ```
201
- /// let sum = [1, 2, 3, 4, 5][:].fold(init=0, fn { sum, elem => sum + elem })
202
- /// assert_eq!(sum, 15)
203
- /// ```
204
- pub fn View ::fold [A , B ](self : View [A ], init ~ : B , f : (B , A ) -> B ) -> B {
205
- for i = 0 , acc = init ; i < self .length (); {
206
- continue i + 1 , f (acc , self [i ])
207
- } else {
208
- acc
209
- }
210
- }
211
-
212
- ///|
213
- /// Fold out values from an View according to certain rules in reversed turn.
214
- ///
215
- /// # Example
216
- /// ```
217
- /// let sum = [1, 2, 3, 4, 5][:].rev_fold(init=0, fn { sum, elem => sum + elem })
218
- /// assert_eq!(sum, 15)
219
- /// ```
220
- pub fn View ::rev_fold [A , B ](self : View [A ], init ~ : B , f : (B , A ) -> B ) -> B {
221
- for i = self .length () - 1 , acc = init ; i >= 0 ; {
222
- continue i - 1 , f (acc , self [i ])
223
- } else {
224
- acc
225
- }
226
- }
227
-
228
- ///|
229
- /// Fold out values from an View according to certain rules with index.
230
- ///
231
- /// # Example
232
- /// ```
233
- /// let sum = [1, 2, 3, 4, 5][:].foldi(init=0, fn { index, sum, _elem => sum + index })
234
- /// assert_eq!(sum, 10)
235
- /// ```
236
- pub fn View ::foldi [A , B ](self : View [A ], init ~ : B , f : (Int , B , A ) -> B ) -> B {
237
- for i = 0 , acc = init ; i < self .length (); {
238
- continue i + 1 , f (i , acc , self [i ])
239
- } else {
240
- acc
241
- }
242
- }
243
-
244
- ///|
245
- /// Fold out values from an View according to certain rules in reversed turn with index.
246
- ///
247
- /// # Example
248
- /// ```
249
- /// let sum = [1, 2, 3, 4, 5][:].rev_foldi(init=0, fn { index, sum, _elem => sum + index })
250
- /// assert_eq!(sum, 10)
251
- /// ```
252
- pub fn View ::rev_foldi [A , B ](
253
- self : View [A ],
254
- init ~ : B ,
255
- f : (Int , B , A ) -> B
256
- ) -> B {
257
- let len = self .length ()
258
- for i = len - 1 , acc = init ; i >= 0 ; {
259
- continue i - 1 , f (len - i - 1 , acc , self [i ])
260
- } else {
261
- acc
262
- }
263
- }
264
-
265
- ///|
266
- /// Maps a function over the elements of the array view.
267
- ///
268
- /// # Example
269
- /// ```
270
- /// let v = [3, 4, 5]
271
- /// let v2 = v[1:].map(fn (x) {x + 1})
272
- /// assert_eq!(v2, [5, 6])
273
- /// ```
274
- pub fn View ::map [T , U ](self : View [T ], f : (T ) -> U ) -> Array [U ] {
275
- if self .length () == 0 {
276
- return []
277
- }
278
- Array ::makei (self .length (), fn (i ) { f (self [i ]) })
279
- }
280
-
281
- ///|
282
- /// Maps a function over the elements of the array view in place.
283
- ///
284
- /// # Example
285
- /// ```
286
- /// let v = [3, 4, 5]
287
- /// v[1:].map_inplace(fn (x) {x + 1})
288
- /// assert_eq!(v, [3, 5, 6])
289
- /// ```
290
- pub fn View ::map_inplace [T ](self : View [T ], f : (T ) -> T ) -> Unit {
291
- for i , v in self {
292
- self [i ] = f (v )
293
- }
294
- }
295
-
296
- ///|
297
- /// Maps a function over the elements of the array view with index.
298
- ///
299
- /// # Example
300
- /// ```
301
- /// let v = [3, 4, 5]
302
- /// let v2 = v[1:].mapi(fn (i, x) {x + i})
303
- /// assert_eq!(v2, [4, 6])
304
- /// ```
305
- pub fn View ::mapi [T , U ](self : View [T ], f : (Int , T ) -> U ) -> Array [U ] {
306
- if self .length () == 0 {
307
- return []
308
- }
309
- Array ::makei (self .length (), fn (i ) { f (i , self [i ]) })
310
- }
311
-
312
- ///|
313
- /// Maps a function over the elements of the array view with index in place.
314
- ///
315
- /// # Example
316
- /// ```
317
- /// let v = [3, 4, 5]
318
- /// v[1:].mapi_inplace(fn (i, x) {x + i})
319
- /// assert_eq!(v, [3, 4, 6])
320
- /// ```
321
- pub fn View ::mapi_inplace [T ](self : View [T ], f : (Int , T ) -> T ) -> Unit {
322
- for i , v in self {
323
- self [i ] = f (i , v )
324
- }
325
- }
326
-
327
- ///|
328
- /// Filters the array view with a predicate function.
329
- ///
330
- /// # Example
331
- /// ```
332
- /// let arr = [1, 2, 3, 4, 5, 6]
333
- /// let v = arr[2:].filter(fn (x) { x % 2 == 0 })
334
- /// assert_eq!(v, [4, 6])
335
- /// ```
336
- pub fn View ::filter [T ](self : View [T ], f : (T ) -> Bool ) -> Array [T ] {
337
- let arr = []
338
- for v in self {
339
- if f (v ) {
340
- arr .push (v )
341
- }
342
- }
343
- arr
344
- }
345
-
346
- ///|
347
- pub impl [X : Show ] Show for View [X ] with output (self , logger ) {
348
- logger .write_iter (self .iter ())
349
- }
350
-
351
- ///|
352
- pub impl [T : Eq ] Eq for View [T ] with op_equal (self , other ) -> Bool {
353
- if self .length () != other .length () {
354
- return false
355
- }
356
- for i in 0 ..< self .length () {
357
- if not (self [i ] == other [i ]) {
358
- return false
359
- }
360
- } else {
361
- true
362
- }
363
- }
364
-
365
- ///|
366
- pub impl [T : Compare ] Compare for View [T ] with compare (self , other ) -> Int {
367
- let len_self = self .length ()
368
- let len_other = other .length ()
369
- if len_self < len_other {
370
- - 1
371
- } else if len_self > len_other {
372
- 1
373
- } else {
374
- for i in 0 ..< len_self {
375
- let cmp = self [i ].compare (other [i ])
376
- if cmp != 0 {
377
- break cmp
378
- }
379
- } else {
380
- 0
381
- }
382
- }
383
- }
0 commit comments