Skip to content

arrays #

Description:

arrays is a module that provides utility functions to make working with arrays easier.

Examples:

import arrays

fn main() {
    a := [1, 5, 7, 0, 9]
    assert arrays.min(a)? == 0
    assert arrays.max(a)? == 9
    assert arrays.idx_min(a)? == 3
}

fn carray_to_varray #

unsafe
fn carray_to_varray<T>(c_array voidptr, c_array_len int) []T

carray_to_varray copies a C byte array into a V array of type T.
See also: cstring_to_vstring

fn chunk #

fn chunk<T>(list []T, size int) [][]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]]

fn concat #

fn concat<T>(a []T, b ...T) []T

concatenate an array with an arbitrary number of additional values

NOTE: if you have two arrays, you should simply use the << operator directly

Examples

arrays.concat([1, 2, 3], 4, 5, 6) == [1, 2, 3, 4, 5, 6] // => true
arrays.concat([1, 2, 3], ...[4, 5, 6]) == [1, 2, 3, 4, 5, 6] // => true
arr << [4, 5, 6] // does what you need if arr is mutable

fn copy #

fn copy<T>(mut dst []T, src []T) int

copy copies the src array elements to the dst array.
The number of the elements copied is the minimum of the length of both arrays.
Returns the number of elements copied.

fn flatten #

fn flatten<T>(list [][]T) []T

flattens n + 1 dimensional array into n dimensional array

Example

arrays.flatten<int>([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5]

fn fold #

fn fold<T, R>(list []T, init R, fold_op fn (r R, t T) R) R

fold sets acc = init, then successively calls acc = fold_op(acc, elem) for each element in list.
returns acc.

Example

// Sum the length of each string in an array
a := ['Hi', 'all']
r := arrays.fold(a, 0,
	fn (r int, t string) int { return r + t.len })
assert r == 5

fn group #

fn group<T>(lists ...[]T) [][]T

group n arrays into a single array of arrays with n elements

This function is analogous to the "zip" function of other languages.
To fully interleave two arrays, follow this function with a call to flatten.

NOTE: An error will be generated if the type annotation is omitted.

Example

arrays.group<int>([1,2,3],[4,5,6]) // => [[1, 4], [2, 5], [3, 6]]

fn group_by #

fn group_by<K, V>(list []V, grouping_op fn (v V) K) map[K][]V

group_by groups together elements, for which the grouping_op callback produced the same result.

Example

arrays.group_by<int, string>(['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}

fn idx_max #

fn idx_max<T>(a []T) ?int

idx_max returns the index of the maximum value in the array

Example

arrays.idx_max([1,2,3,0,9]) // => 4

fn idx_min #

fn idx_min<T>(a []T) ?int

idx_min returns the index of the minimum value in the array

Example

arrays.idx_min([1,2,3,0,9]) // => 3

fn lower_bound #

fn lower_bound<T>(arr []T, val T) ?T

returns the smallest element >= val, requires arr to be sorted

Example

arrays.lower_bound([2, 4, 6, 8], 3)? // => 4

fn max #

fn max<T>(a []T) ?T

max returns the maximum value in the array

Example

arrays.max([1,2,3,0,9]) // => 9

fn merge #

fn merge<T>(a []T, b []T) []T

merge two sorted arrays (ascending) and maintain sorted order

Example

arrays.merge([1,3,5,7], [2,4,6,8]) // => [1,2,3,4,5,6,7,8]

fn min #

fn min<T>(a []T) ?T

min returns the minimum value in the array

Example

arrays.min([1,2,3,0,9]) // => 0

fn reduce #

fn reduce<T>(list []T, reduce_op fn (t1 T, t2 T) T) ?T

reduce sets acc = list[0], then successively calls acc = reduce_op(acc, elem) for each remaining element in list.
returns the accumulated value in acc.
returns an error if the array is empty.
See also: fold.

Example

arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? // => 120

fn rotate_left #

fn rotate_left<T>(mut arr []T, mid int)

rotate_left rotates the array in-place such that the first mid elements of the array move to the end while the last arr.len - mid elements move to the front. After calling rotate_left, the element previously at index mid will become the first element in the array.

Example

mut x := [1,2,3,4,5,6]
arrays.rotate_left(mut x, 2)
println(x) // [3, 4, 5, 6, 1, 2]

fn rotate_right #

fn rotate_right<T>(mut arr []T, k int)

rotate_right rotates the array in-place such that the first arr.len - k elements of the array move to the end while the last k elements move to the front. After calling rotate_right, the element previously at index arr.len - k will become the first element in the array.

Example

mut x := [1,2,3,4,5,6]
arrays.rotate_right(mut x, 2)
println(x) // [5, 6, 1, 2, 3, 4]

fn sum #

fn sum<T>(list []T) ?T

sum up array, return nothing when array has no elements

NOTICE: currently V has bug that cannot make sum function takes custom struct with + operator overloaded which means you can only pass array of numbers for now.
TODO: Fix generic operator overloading detection issue.

Example

arrays.sum<int>([1, 2, 3, 4, 5])? // => 15

fn upper_bound #

fn upper_bound<T>(arr []T, val T) ?T

returns the largest element <= val, requires arr to be sorted

Example

arrays.upper_bound([2, 4, 6, 8], 3)? // => 2

fn window #

fn window<T>(list []T, attr WindowAttribute) [][]T

get snapshots of the window of the given size sliding along array with the given step, where each snapshot is an array.

  • size - snapshot size
  • step - gap size between each snapshot, default is 1.

Examples

arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]
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]]

struct WindowAttribute #

struct WindowAttribute {
	size int
	step int = 1
}