arrays: fix v doc -unsafe-run-examples -f none vlib/arrays/ too

This commit is contained in:
Delyan Angelov 2025-08-13 17:28:59 +03:00
parent 9771491c94
commit 33602047bc
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
3 changed files with 9 additions and 10 deletions

View file

@ -154,7 +154,7 @@ pub fn group[T](arrs ...[]T) [][]T {
}
// chunk array into a single array of arrays where each element is the next `size` elements of the original.
// Example: arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2)) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
// Example: arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
pub fn chunk[T](array []T, size int) [][]T {
// allocate chunk array
mut chunks := [][]T{cap: array.len / size + if array.len % size == 0 { 0 } else { 1 }}
@ -425,11 +425,10 @@ pub fn group_by[K, V](array []V, grouping_op fn (val V) K) map[K][]V {
}
// concatenate an array with an arbitrary number of additional values.
//
// NOTE: if you have two arrays, you should simply use the `<<` operator directly
// Example: arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6] // => true
// Example: arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6] // => true
// Example: arr << [4, 5, 6] // does what you need if arr is mutable
// NOTE: if you have two arrays, you should simply use the `<<` operator directly.
// Example: assert arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6]
// Example: assert arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6]
// Example: mut arr := arrays.concat([1, 2, 3], 4); arr << [10,20]; assert arr == [1,2,3,4,10,20] // note: arr is mutable
pub fn concat[T](a []T, b ...T) []T {
mut m := []T{cap: a.len + b.len}

View file

@ -2,7 +2,7 @@ module arrays
// index_of_first returns the index of the first element of `array`, for which the predicate fn returns true.
// If predicate does not return true for any of the elements, then index_of_first will return -1.
// Example: arrays.index_of_first([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 2
// Example: assert arrays.index_of_first([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 2
pub fn index_of_first[T](array []T, predicate fn (idx int, elem T) bool) int {
for i, e in array {
if predicate(i, e) {
@ -14,7 +14,7 @@ pub fn index_of_first[T](array []T, predicate fn (idx int, elem T) bool) int {
// index_of_last returns the index of the last element of `array`, for which the predicate fn returns true.
// If predicate does not return true for any of the elements, then index_of_last will return -1.
// Example: arrays.index_of_last([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 4
// Example: assert arrays.index_of_last([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 4
pub fn index_of_last[T](array []T, predicate fn (idx int, elem T) bool) int {
for i := array.len - 1; i >= 0; i-- {
e := array[i]

View file

@ -2,7 +2,7 @@ module arrays
// map_of_indexes returns a map, where each key is an unique value in `array`.
// Each value in that map for that key, is an array, containing the indexes in `array`, where that value has been found.
// Example: arrays.map_of_indexes([1,2,3,4,4,2,1,4,4,999]) == {1: [0, 6], 2: [1, 5], 3: [2], 4: [3, 4, 7, 8], 999: [9]}
// Example: assert arrays.map_of_indexes([1,2,3,4,4,2,1,4,4,999]) == {1: [0, 6], 2: [1, 5], 3: [2], 4: [3, 4, 7, 8], 999: [9]}
pub fn map_of_indexes[T](array []T) map[T][]int {
mut result := map[T][]int{}
for i, e in array {
@ -18,7 +18,7 @@ pub fn map_of_indexes[T](array []T) map[T][]int {
// map_of_counts returns a map, where each key is an unique value in `array`.
// Each value in that map for that key, is how many times that value occurs in `array`.
// It can be useful for building histograms of discrete measurements.
// Example: arrays.map_of_counts([1,2,3,4,4,2,1,4,4]) == {1: 2, 2: 2, 3: 1, 4: 4}
// Example: assert arrays.map_of_counts([1,2,3,4,4,2,1,4,4]) == {1: 2, 2: 2, 3: 1, 4: 4}
pub fn map_of_counts[T](array []T) map[T]int {
mut result := map[T]int{}
for e in array {