@@ -10,7 +10,7 @@ module arrays
10
10
// - flatten - reduce dimensionality of array by one. e.g. arrays.flatten([[1,2],[3,4],[5,6]]) => [1,2,3,4,5,6]
11
11
12
12
// min returns the minimum value in the array
13
- // Example: arrays.min([1,2,3,0, 9]) // => 0
13
+ // Example: arrays.min([1, 2, 3, 0, 9])! // => 0
14
14
pub fn min [T](array []T) ! T {
15
15
if array.len == 0 {
16
16
return error ('.min called on an empty array' )
@@ -25,7 +25,7 @@ pub fn min[T](array []T) !T {
25
25
}
26
26
27
27
// max returns the maximum value in the array
28
- // Example: arrays.max([1,2,3,0, 9]) // => 9
28
+ // Example: arrays.max([1, 2, 3, 0, 9])! // => 9
29
29
pub fn max [T](array []T) ! T {
30
30
if array.len == 0 {
31
31
return error ('.max called on an empty array' )
@@ -40,7 +40,7 @@ pub fn max[T](array []T) !T {
40
40
}
41
41
42
42
// idx_min returns the index of the minimum value in the array
43
- // Example: arrays.idx_min([1,2,3,0, 9]) // => 3
43
+ // Example: arrays.idx_min([1, 2, 3, 0, 9])! // => 3
44
44
pub fn idx_min [T](array []T) ! int {
45
45
if array.len == 0 {
46
46
return error ('.idx_min called on an empty array' )
@@ -57,7 +57,7 @@ pub fn idx_min[T](array []T) !int {
57
57
}
58
58
59
59
// idx_max returns the index of the maximum value in the array
60
- // Example: arrays.idx_max([1,2,3,0, 9]) // => 4
60
+ // Example: arrays.idx_max([1, 2, 3, 0, 9])! // => 4
61
61
pub fn idx_max [T](array []T) ! int {
62
62
if array.len == 0 {
63
63
return error ('.idx_max called on an empty array' )
@@ -74,7 +74,7 @@ pub fn idx_max[T](array []T) !int {
74
74
}
75
75
76
76
// merge two sorted arrays (ascending) and maintain sorted order
77
- // Example: arrays.merge([1,3,5, 7], [2,4,6, 8]) // => [1,2,3,4,5,6,7, 8]
77
+ // Example: arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
78
78
[direct_array_access ]
79
79
pub fn merge [T](a []T, b []T) []T {
80
80
mut m := []T{len: a.len + b.len}
@@ -113,7 +113,7 @@ pub fn merge[T](a []T, b []T) []T {
113
113
// To fully interleave two arrays, follow this function with a call to `flatten`.
114
114
//
115
115
// NOTE: An error will be generated if the type annotation is omitted.
116
- // Example: arrays.group< int> ([1,2, 3],[4,5, 6]) // => [[1, 4], [2, 5], [3, 6]]
116
+ // Example: arrays.group[ int] ([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
117
117
pub fn group [T](arrays ...[]T) [][]T {
118
118
mut length := if arrays.len > 0 { arrays[0 ].len } else { 0 }
119
119
// calculate length of output by finding shortest input array
@@ -199,7 +199,7 @@ pub fn window[T](array []T, attr WindowAttribute) [][]T {
199
199
// NOTICE: currently V has bug that cannot make sum function takes custom struct with + operator overloaded
200
200
// which means you can only pass array of numbers for now.
201
201
// TODO: Fix generic operator overloading detection issue.
202
- // Example: arrays.sum< int> ([1, 2, 3, 4, 5])? // => 15
202
+ // Example: arrays.sum[ int] ([1, 2, 3, 4, 5])! // => 15
203
203
pub fn sum [T](array []T) ! T {
204
204
if array.len == 0 {
205
205
return error ('Cannot sum up array of nothing.' )
@@ -283,7 +283,7 @@ pub fn filter_indexed[T](array []T, predicate fn (idx int, elem T) bool) []T {
283
283
// ```v
284
284
// // Sum the length of each string in an array
285
285
// a := ['Hi', 'all']
286
- // r := arrays.fold< string, int> (a, 0,
286
+ // r := arrays.fold[ string, int] (a, 0,
287
287
// fn (r int, t string) int { return r + t.len })
288
288
// assert r == 5
289
289
// ```
@@ -370,7 +370,7 @@ pub fn map_indexed[T, R](array []T, transform fn (idx int, elem T) R) []R {
370
370
}
371
371
372
372
// group_by groups together elements, for which the `grouping_op` callback produced the same result.
373
- // Example: arrays.group_by< int, string> (['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
373
+ // Example: arrays.group_by[ int, string] (['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
374
374
pub fn group_by [K, V](array []V, grouping_op fn (val V) K) map [K][]V {
375
375
mut result := map [K][]V{}
376
376
@@ -404,7 +404,7 @@ pub fn concat[T](a []T, b ...T) []T {
404
404
}
405
405
406
406
// returns the smallest element >= val, requires `array` to be sorted
407
- // Example: arrays.lower_bound([2, 4, 6, 8], 3)? // => 4
407
+ // Example: arrays.lower_bound([2, 4, 6, 8], 3)! // => 4
408
408
pub fn lower_bound [T](array []T, val T) ! T {
409
409
if array.len == 0 {
410
410
return error ('.lower_bound called on an empty array' )
@@ -452,7 +452,7 @@ pub fn upper_bound[T](array []T, val T) !T {
452
452
// binary search, requires `array` to be sorted, returns index of found item or error.
453
453
// Binary searches on sorted lists can be faster than other array searches because at maximum
454
454
// the algorithm only has to traverse log N elements
455
- // Example: arrays.binary_search([1, 2, 3, 4], 4)? // => 3
455
+ // Example: arrays.binary_search([1, 2, 3, 4], 4)! // => 3
456
456
pub fn binary_search [T](array []T, target T) ! int {
457
457
mut left := 0
458
458
mut right := array.len - 1
0 commit comments