v / vlib / arrays / arrays.v
783 lines · 698 sloc · 20.41 KB · ddd4ac7880f392ad6c39719af05f01d29eb0ad3a
Raw
1module arrays
2
3import strings
4
5// Common arrays functions:
6// - min / max - return the value of the minimum / maximum
7// - idx_min / idx_max - return the index of the first minimum / maximum
8// - merge - combine two sorted arrays and maintain sorted order
9// - append - combine two arrays, by appending the second array to the first
10// - chunk - chunk array to arrays with n elements
11// - window - get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array
12// - group - merge two arrays by interleaving e.g. arrays.group([1,3,5], [2,4,6]) => [[1,2],[3,4],[5,6]]
13// - flatten - reduce dimensionality of array by one. e.g. arrays.flatten([[1,2],[3,4],[5,6]]) => [1,2,3,4,5,6]
14// - each - call a callback fn, for each element of the array, similar to a.map(), but unlike it, the callback should not return anything
15
16// min returns the minimum value in the array.
17// Example: arrays.min([1, 2, 3, 0, 9])! // => 0
18pub fn min[T](array []T) !T {
19 if array.len == 0 {
20 return error('.min called on an empty array')
21 }
22 mut val := array[0]
23 for e in array {
24 if e < val {
25 val = e
26 }
27 }
28 return val
29}
30
31// max returns the maximum value in the array.
32// Example: arrays.max([1, 2, 3, 0, 9])! // => 9
33pub fn max[T](array []T) !T {
34 if array.len == 0 {
35 return error('.max called on an empty array')
36 }
37 mut val := array[0]
38 for e in array {
39 if e > val {
40 val = e
41 }
42 }
43 return val
44}
45
46// idx_min returns the index of the minimum value in the array.
47// Example: arrays.idx_min([1, 2, 3, 0, 9])! // => 3
48pub fn idx_min[T](array []T) !int {
49 if array.len == 0 {
50 return error('.idx_min called on an empty array')
51 }
52 mut idx := 0
53 mut val := array[0]
54 for i, e in array {
55 if e < val {
56 val = e
57 idx = i
58 }
59 }
60 return idx
61}
62
63// idx_max returns the index of the maximum value in the array.
64// Example: arrays.idx_max([1, 2, 3, 0, 9])! // => 4
65pub fn idx_max[T](array []T) !int {
66 if array.len == 0 {
67 return error('.idx_max called on an empty array')
68 }
69 mut idx := 0
70 mut val := array[0]
71 for i, e in array {
72 if e > val {
73 val = e
74 idx = i
75 }
76 }
77 return idx
78}
79
80// merge two sorted arrays (ascending) and maintain sorted order.
81// Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
82@[direct_array_access]
83pub fn merge[T](a []T, b []T) []T {
84 mut m := []T{len: a.len + b.len}
85 mut ia := 0
86 mut ib := 0
87 mut j := 0
88 // TODO: efficient approach to merge_desc where: a[ia] >= b[ib]
89 for ia < a.len && ib < b.len {
90 if a[ia] <= b[ib] {
91 m[j] = a[ia]
92 ia++
93 } else {
94 m[j] = b[ib]
95 ib++
96 }
97 j++
98 }
99 // a leftovers
100 for ia < a.len {
101 m[j] = a[ia]
102 ia++
103 j++
104 }
105 // b leftovers
106 for ib < b.len {
107 m[j] = b[ib]
108 ib++
109 j++
110 }
111 return m
112}
113
114// append the second array `b` to the first array `a`, and return the result.
115// Note, that unlike arrays.concat, arrays.append is less flexible, but more efficient,
116// since it does not require you to use ...a for the second parameter.
117// Example: arrays.append([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 3, 5, 7, 2, 4, 6, 8]
118pub fn append[T](a []T, b []T) []T {
119 mut m := []T{cap: a.len + b.len}
120 m << a
121 m << b
122 return m
123}
124
125// group n arrays into a single array of arrays with n elements.
126// This function is analogous to the "zip" function of other languages.
127// To fully interleave two arrays, follow this function with a call to `flatten`.
128// NOTE: An error will be generated if the type annotation is omitted.
129// Example: arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
130pub fn group[T](arrs ...[]T) [][]T {
131 mut length := if arrs.len > 0 { arrs[0].len } else { 0 }
132 // calculate length of output by finding shortest input array
133 for ndx in 1 .. arrs.len {
134 if arrs[ndx].len < length {
135 length = arrs[ndx].len
136 }
137 }
138
139 if length > 0 {
140 mut arr := [][]T{cap: length}
141 // append all combined arrays into the resultant array
142 for ndx in 0 .. length {
143 mut grouped := []T{cap: arrs.len}
144 // combine each list item for the ndx position into one array
145 for arr_ndx in 0 .. arrs.len {
146 grouped << arrs[arr_ndx][ndx]
147 }
148 arr << grouped
149 }
150 return arr
151 }
152
153 return [][]T{}
154}
155
156// chunk array into a single array of arrays where each element is the next `size` elements of the original.
157// Example: arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
158pub fn chunk[T](array []T, size int) [][]T {
159 // allocate chunk array
160 mut chunks := [][]T{cap: array.len / size + if array.len % size == 0 { 0 } else { 1 }}
161
162 for i := 0; true; {
163 // check chunk size is greater than remaining element size
164 if array.len < i + size {
165 // check if there's no more element to chunk
166 if array.len <= i {
167 break
168 }
169
170 chunks << array[i..]
171
172 break
173 }
174
175 chunks << array[i..i + size]
176 i += size
177 }
178
179 return chunks
180}
181
182// chunk_while splits the input array `a` into chunks of varying length, using the `predicate`, passing to it pairs of adjacent elements `before` and `after`.
183// Each chunk, will contain all ajdacent elements, for which the `predicate` returned true.
184// The chunks are split *between* the `before` and `after` elements, for which the `predicate` returned false.
185// Example: assert arrays.chunk_while([0,9,2,2,3,2,7,5,9,5],fn(x int,y int)bool{return x<=y})==[[0,9],[2,2,3],[2,7],[5,9],[5]]
186// Example: assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y})==[[`a`,`a`,`a`,`a`],[`b`,`b`,`b`],[`c`,`c`],[`a`]]
187// Example: assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y}).map({it[0]:it.len})==[{`a`:4},{`b`:3},{`c`:2},{`a`:1}]
188pub fn chunk_while[T](a []T, predicate fn (before T, after T) bool) [][]T {
189 if a.len == 0 {
190 return []
191 }
192 mut chunks := [][]T{}
193 mut current := [a[0]]
194 mut i := 0
195 for i = 1; i < a.len; i++ {
196 if predicate(a[i - 1], a[i]) {
197 current << a[i]
198 continue
199 }
200 if current.len > 0 {
201 chunks << current
202 }
203 current = [a[i]]
204 }
205 if current.len > 0 {
206 chunks << current
207 }
208 return chunks
209}
210
211pub struct WindowAttribute {
212pub:
213 size int
214 step int = 1
215}
216
217// get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array.
218// - `size` - snapshot size
219// - `step` - gap size between each snapshot, default is 1.
220//
221// Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]
222// Example: arrays.window([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size: 3, step: 2) // => [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
223pub fn window[T](array []T, attr WindowAttribute) [][]T {
224 if array.len == 0 {
225 return [][]T{}
226 }
227 // allocate snapshot array
228 mut windows := [][]T{cap: array.len - attr.size + 1}
229
230 for i := 0; true; {
231 // check remaining elements size is less than snapshot size
232 if array.len < i + attr.size {
233 break
234 }
235
236 windows << array[i..i + attr.size]
237 i += attr.step
238 }
239
240 return windows
241}
242
243// sum up array, return an error, when the array has no elements.
244// Example: arrays.sum([1, 2, 3, 4, 5])! // => 15
245pub fn sum[T](array []T) !T {
246 if array.len == 0 {
247 return error('Cannot sum up array of nothing.')
248 } else {
249 mut head := array[0]
250
251 for e in array[1..] {
252 head += e
253 }
254
255 return head
256 }
257}
258
259// reduce sets `acc = array[0]`, then successively calls `acc = reduce_op(acc, elem)` for each remaining element in `array`.
260// returns the accumulated value in `acc`.
261// returns an error if the array is empty.
262// See also: [fold](#fold).
263// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })! // => 120
264pub fn reduce[T](array []T, reduce_op fn (acc T, elem T) T) !T {
265 if array.len == 0 {
266 return error('Cannot reduce array of nothing.')
267 } else {
268 mut value := array[0]
269
270 for e in array[1..] {
271 value = reduce_op(value, e)
272 }
273
274 return value
275 }
276}
277
278// reduce_indexed sets `acc = array[0]`, then successively calls `acc = reduce_op(idx, acc, elem)` for each remaining element in `array`.
279// returns the accumulated value in `acc`.
280// returns an error if the array is empty.
281// See also: [fold_indexed](#fold_indexed).
282pub fn reduce_indexed[T](array []T, reduce_op fn (idx int, acc T, elem T) T) !T {
283 if array.len == 0 {
284 return error('Cannot reduce array of nothing.')
285 } else {
286 mut value := array[0]
287
288 for i, e in array {
289 if i == 0 {
290 continue
291 } else {
292 value = reduce_op(i, value, e)
293 }
294 }
295
296 return value
297 }
298}
299
300// filter_indexed filters elements based on `predicate` function being invoked on each element with its index in the original array.
301pub fn filter_indexed[T](array []T, predicate fn (idx int, elem T) bool) []T {
302 mut result := []T{cap: array.len}
303
304 for i, e in array {
305 if predicate(i, e) {
306 result << e
307 }
308 }
309
310 return result
311}
312
313// fold sets `acc = init`, then successively calls `acc = fold_op(acc, elem)` for each element in `array`.
314// returns `acc`.
315// Example:
316// ```v
317// // Sum the length of each string in an array
318// a := ['Hi', 'all']
319// r := arrays.fold[string, int](a, 0,
320// fn (r int, t string) int { return r + t.len })
321// assert r == 5
322// ```
323pub fn fold[T, R](array []T, init R, fold_op fn (acc R, elem T) R) R {
324 $if R is $array {
325 mut value := init.clone()
326 for e in array {
327 value = fold_op(value, e)
328 }
329 return value
330 } $else {
331 mut value := init
332 for e in array {
333 value = fold_op(value, e)
334 }
335 return value
336 }
337}
338
339// fold_indexed sets `acc = init`, then successively calls `acc = fold_op(idx, acc, elem)` for each element in `array`.
340// returns `acc`.
341pub fn fold_indexed[T, R](array []T, init R, fold_op fn (idx int, acc R, elem T) R) R {
342 mut value := init
343
344 for i, e in array {
345 value = fold_op(i, value, e)
346 }
347
348 return value
349}
350
351// flatten flattens n + 1 dimensional array into n dimensional array.
352// Example: arrays.flatten[int]([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5]
353pub fn flatten[T](array [][]T) []T {
354 // calculate required capacity
355 mut required_size := 0
356
357 for e1 in array {
358 for _ in e1 {
359 required_size += 1
360 }
361 }
362
363 // allocate flattened array
364 mut result := []T{cap: required_size}
365
366 for e1 in array {
367 for e2 in e1 {
368 result << e2
369 }
370 }
371
372 return result
373}
374
375// flat_map creates a new array populated with the flattened result of calling transform function being invoked on each element of `list`.
376pub fn flat_map[T, R](array []T, transform fn (elem T) []R) []R {
377 mut result := [][]R{cap: array.len}
378
379 for v in array {
380 result << transform(v)
381 }
382
383 return flatten(result)
384}
385
386// flat_map_indexed creates a new array with the flattened result of calling the `transform` fn, invoked on each idx,elem pair from the original.
387pub fn flat_map_indexed[T, R](array []T, transform fn (idx int, elem T) []R) []R {
388 mut result := [][]R{cap: array.len}
389
390 for i, v in array {
391 result << transform(i, v)
392 }
393
394 return flatten(result)
395}
396
397// map_indexed creates a new array with the result of calling the `transform` fn, invoked on each idx,elem pair from the original.
398pub fn map_indexed[T, R](array []T, transform fn (idx int, elem T) R) []R {
399 mut result := []R{cap: array.len}
400
401 for i, v in array {
402 result << transform(i, v)
403 }
404
405 return result
406}
407
408// group_by groups together elements, for which the `grouping_op` callback produced the same result.
409// Example: arrays.group_by[int, string](['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
410pub fn group_by[K, V](array []V, grouping_op fn (val V) K) map[K][]V {
411 mut result := map[K][]V{}
412
413 for v in array {
414 key := grouping_op(v)
415
416 // check if key exists, if not, then create a new array with matched value, otherwise append.
417 if key in result {
418 result[key] << v
419 } else {
420 result[key] = [v]
421 }
422 }
423
424 return result
425}
426
427// concatenate an array with an arbitrary number of additional values.
428// NOTE: if you have two arrays, you should simply use the `<<` operator directly.
429// Example: assert arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6]
430// Example: assert arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6]
431// Example: mut arr := arrays.concat([1, 2, 3], 4); arr << [10,20]; assert arr == [1,2,3,4,10,20] // note: arr is mutable
432pub fn concat[T](a []T, b ...T) []T {
433 mut m := []T{cap: a.len + b.len}
434
435 m << a
436 m << b
437
438 return m
439}
440
441// returns the smallest element >= val, requires `array` to be sorted.
442// Example: arrays.lower_bound([2, 4, 6, 8], 3)! // => 4
443pub fn lower_bound[T](array []T, val T) !T {
444 if array.len == 0 {
445 return error('.lower_bound called on an empty array')
446 }
447 mut left, mut right := 0, array.len - 1
448 for ; left <= right; {
449 idx := (left + right) / 2
450 elem := array[idx]
451 if elem < val {
452 left = idx + 1
453 } else {
454 right = idx - 1
455 }
456 }
457 if left >= array.len {
458 return error('')
459 } else {
460 return array[left]
461 }
462}
463
464// returns the largest element <= val, requires `array` to be sorted.
465// Example: arrays.upper_bound([2, 4, 6, 8], 3)! // => 2
466pub fn upper_bound[T](array []T, val T) !T {
467 if array.len == 0 {
468 return error('.upper_bound called on an empty array')
469 }
470 mut left, mut right := 0, array.len - 1
471 for ; left <= right; {
472 idx := (left + right) / 2
473 elem := array[idx]
474 if elem > val {
475 right = idx - 1
476 } else {
477 left = idx + 1
478 }
479 }
480 if right < 0 {
481 return error('')
482 } else {
483 return array[right]
484 }
485}
486
487// binary_search, requires `array` to be sorted, returns index of found item or error.
488// Binary searches on sorted lists can be faster than other array searches because at maximum
489// the algorithm only has to traverse log N elements
490// Example: arrays.binary_search([1, 2, 3, 4], 4)! // => 3
491pub fn binary_search[T](array []T, target T) !int {
492 mut left := 0
493 mut right := array.len - 1
494 for ; left <= right; {
495 idx := (left + right) / 2
496 elem := array[idx]
497 if elem == target {
498 return idx
499 }
500 if elem < target {
501 left = idx + 1
502 } else {
503 right = idx - 1
504 }
505 }
506 return error('')
507}
508
509// rotate_left rotates the array in-place.
510// It does it in such a way, that the first `mid` elements of the array, move to the end,
511// while the last `array.len - mid` elements move to the front.
512// After calling `rotate_left`, the element previously at index `mid` will become the first element in the array.
513// Example:
514// ```v
515// mut x := [1,2,3,4,5,6]
516// arrays.rotate_left(mut x, 2)
517// println(x) // [3, 4, 5, 6, 1, 2]
518// ```
519pub fn rotate_left[T](mut array []T, mid int) {
520 assert mid <= array.len && mid >= 0
521 k := array.len - mid
522 p := unsafe { &T(array.data) }
523 unsafe {
524 ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
525 }
526}
527
528// rotate_right rotates the array in-place.
529// It does it in such a way, that the first `array.len - k` elements of the array, move to the end,
530// while the last `k` elements move to the front.
531// After calling `rotate_right`, the element previously at index `array.len - k` will become the first element in the array.
532// Example:
533// ```v
534// mut x := [1,2,3,4,5,6]
535// arrays.rotate_right(mut x, 2)
536// println(x) // [5, 6, 1, 2, 3, 4]
537// ```
538pub fn rotate_right[T](mut array []T, k int) {
539 assert k <= array.len && k >= 0
540 mid := array.len - k
541 p := unsafe { &T(array.data) }
542 unsafe {
543 ptr_rotate[T](mid, &T(usize(voidptr(p)) + usize(sizeof(T)) * usize(mid)), k)
544 }
545}
546
547@[unsafe]
548fn ptr_rotate[T](left_ int, mid &T, right_ int) {
549 sz := usize(sizeof(T))
550 mut left := usize(left_)
551 mut right := usize(right_)
552 limit := raw_array_cap[T]()
553 for {
554 delta := if left < right { left } else { right }
555 if delta <= usize(limit) {
556 break
557 }
558 unsafe {
559 swap_nonoverlapping[T](&T(usize(voidptr(mid)) - left * sz), &T(usize(voidptr(mid)) +
560 usize(right - delta) * sz), int(delta))
561 }
562 if left <= right {
563 right -= delta
564 } else {
565 left -= delta
566 }
567 }
568 unsafe {
569 rawarray := malloc(raw_array_malloc_size[T]())
570 defer {
571 free(rawarray)
572 }
573 dim := &T(usize(voidptr(mid)) - left * sz + right * sz)
574 if left <= right {
575 vmemcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
576 vmemmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), isize(right * sz))
577 vmemcpy(voidptr(dim), rawarray, isize(left * sz))
578 } else {
579 vmemcpy(rawarray, voidptr(mid), isize(right * sz))
580 vmemmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
581 vmemcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, isize(right * sz))
582 }
583 }
584}
585
586struct Block {
587mut:
588 x u64
589 y u64
590 z u64
591 w u64
592}
593
594struct UnalignedBlock {
595mut:
596 x u64
597 y u64
598 z u64
599 w u64
600}
601
602const extra_size = 32 * isize(sizeof(usize))
603
604fn raw_array_cap[T]() isize {
605 size := isize(sizeof(T))
606 if size > extra_size {
607 return 1
608 } else {
609 return extra_size / size
610 }
611}
612
613fn raw_array_malloc_size[T]() isize {
614 size := isize(sizeof(T))
615 if size > extra_size {
616 return size * 2
617 } else {
618 return extra_size
619 }
620}
621
622@[unsafe]
623fn memswap(x voidptr, y voidptr, len usize) {
624 block_size := isize(sizeof(Block))
625
626 mut i := usize(0)
627 for i + usize(block_size) <= len {
628 mut t_ := Block{}
629 t := voidptr(&t_)
630
631 xi := usize(x) + i
632 yi := usize(y) + i
633 unsafe {
634 vmemcpy(t, voidptr(xi), block_size)
635 vmemcpy(voidptr(xi), voidptr(yi), block_size)
636 vmemcpy(voidptr(yi), t, block_size)
637 }
638 i += usize(block_size)
639 }
640 if i < len {
641 mut t_ := UnalignedBlock{}
642 t := voidptr(&t_)
643 rem := isize(len - i)
644 xi := usize(x) + i
645 yi := usize(y) + i
646 unsafe {
647 vmemcpy(t, voidptr(xi), rem)
648 vmemcpy(voidptr(xi), voidptr(yi), rem)
649 vmemcpy(voidptr(yi), t, rem)
650 }
651 }
652}
653
654@[unsafe]
655fn swap_nonoverlapping[T](x_ &T, y_ &T, count int) {
656 x := voidptr(x_)
657 y := voidptr(y_)
658
659 len := usize(sizeof(T)) * usize(count)
660 unsafe {
661 memswap(x, y, len)
662 }
663}
664
665// copy copies the `src` array elements to the `dst` array.
666// The number of the elements copied is the minimum of the length of both arrays.
667// Returns the number of elements copied.
668pub fn copy[T](mut dst []T, src []T) int {
669 min_len := if dst.len < src.len { dst.len } else { src.len }
670 if min_len <= 0 {
671 return 0
672 }
673 if can_copy_bits[T]() {
674 blen := min_len * isize(sizeof(T))
675 unsafe { vmemmove(&T(dst.data), src.data, blen) }
676 } else {
677 for i in 0 .. min_len {
678 dst[i] = src[i]
679 }
680 }
681 return min_len
682}
683
684// can_copy_bits determines if T can be copied using `memcpy`.
685// false if autofree needs to intervene
686// false if type is not copyable e.g. map
687fn can_copy_bits[T]() bool {
688 // references, C pointers, integers, floats, runes
689 if T.name[0] in [`&`, `b`, `c`, `f`, `i`, `r`, `u`, `v`] {
690 return true
691 }
692 return false
693}
694
695// carray_to_varray copies a C byte array into a V array of type `T`.
696// See also: `cstring_to_vstring`
697@[unsafe]
698pub fn carray_to_varray[T](c_array_data voidptr, items int) []T {
699 mut v_array := []T{len: items}
700 total_size := items * isize(sizeof(T))
701 unsafe { vmemcpy(v_array.data, c_array_data, total_size) }
702 return v_array
703}
704
705// find_first returns the first element that matches the given predicate.
706// Returns `none` if no match is found.
707// Example: arrays.find_first([1, 2, 3, 4, 5], fn (i int) bool { return i == 3 })? // => 3
708pub fn find_first[T](array []T, predicate fn (elem T) bool) ?T {
709 if array.len == 0 {
710 return none
711 }
712 for item in array {
713 if predicate(item) {
714 return item
715 }
716 }
717 return none
718}
719
720// find_last returns the last element that matches the given predicate.
721// Returns `none` if no match is found.
722// Example: arrays.find_last([1, 2, 3, 4, 5], fn (i int) bool { return i == 3})? // => 3
723pub fn find_last[T](array []T, predicate fn (elem T) bool) ?T {
724 if array.len == 0 {
725 return none
726 }
727 for idx := array.len - 1; idx >= 0; idx-- {
728 item := array[idx]
729 if predicate(item) {
730 return item
731 }
732 }
733 return none
734}
735
736// join_to_string takes in a custom transform function and joins all elements into a string with
737// the specified separator
738@[manualfree]
739pub fn join_to_string[T](array []T, separator string, transform fn (elem T) string) string {
740 mut sb := strings.new_builder(array.len * 2)
741 defer {
742 unsafe { sb.free() }
743 }
744 for i, item in array {
745 x := transform(item)
746 sb.write_string(x)
747 unsafe { x.free() }
748 if i < array.len - 1 {
749 sb.write_string(separator)
750 }
751 }
752 return sb.str()
753}
754
755// partition splits the original array into pair of lists.
756// The first list contains elements for which the predicate fn returned true,
757// while the second list contains elements for which the predicate fn returned false.
758pub fn partition[T](array []T, predicate fn (elem T) bool) ([]T, []T) {
759 mut matching, mut non_matching := []T{}, []T{}
760 for item in array {
761 if predicate(item) {
762 matching << item
763 } else {
764 non_matching << item
765 }
766 }
767 return matching, non_matching
768}
769
770// each calls the callback fn `cb`, for each element of the given array `a`.
771pub fn each[T](a []T, cb fn (elem T)) {
772 for item in a {
773 cb(item)
774 }
775}
776
777// each_indexed calls the callback fn `cb`, for each element of the given array `a`.
778// It passes the callback both the index of the current element, and the element itself.
779pub fn each_indexed[T](a []T, cb fn (i int, e T)) {
780 for idx, item in a {
781 cb(idx, item)
782 }
783}
784