Module arrays stdlib

arrays
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
0
Imported by:
6
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imports

This section is empty.

Imported by

Overview

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
}

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

#fn carray_to_varray[T]

unsafe
fn carray_to_varray[T](c_array_data voidptr, items int) []T

carray_to_varray copies a C byte array into a V array of type T.

See also: cstring_to_vstring

#fn chunk[T]

fn chunk[T](array []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[T]

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

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

#fn copy[T]

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 filter_indexed[T]

fn filter_indexed[T](array []T, predicate fn (int, T) bool) []T

filter_indexed filters elements based on predicate function being invoked on each element with its index in the original array.

#fn flat_map[T, R]

fn flat_map[T, R](array []T, transform fn (T) []R) []R

flat_map creates a new array populated with the flattened result of calling transform function being invoked on each element of list.

#fn flat_map_indexed[T, R]

fn flat_map_indexed[T, R](array []T, transform fn (int, T) []R) []R

flat_map_indexed creates a new array populated with the flattened result of calling the transform function being invoked on each element with its index in the original array.

#fn flatten[T]

fn flatten[T](array [][]T) []T

flatten flattens n + 1 dimensional array into n dimensional array

Example:

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

#fn fold[T, R]

fn fold[T, R](array []T, init R, fold_op fn (R, T) R) R

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

returns acc.

Example:

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

#fn fold_indexed[T, R]

fn fold_indexed[T, R](array []T, init R, fold_op fn (int, R, T) R) R

fold_indexed sets acc = init, then successively calls acc = fold_op(idx, acc, elem) for each element in array.

returns acc.

#fn group[T]

fn group[T](arrays [][]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([1,2,3],[4,5,6]) // => [[1, 4], [2, 5], [3, 6]]

#fn group_by[K, V]

fn group_by[K, V](array []V, grouping_op fn (V) K) map[K][]V

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

Example:

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

#fn idx_max[T]

fn idx_max[T](array []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[T]

fn idx_min[T](array []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 index_of_first[T]

fn index_of_first[T](array []T, predicate fn (int, T) bool) int

index_of_first returns the index of the first element of array, for which the predicate function 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

#fn index_of_last[T]

fn index_of_last[T](array []T, predicate fn (int, T) bool) int

index_of_last returns the index of the last element of array, for which the predicate function 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

#fn lower_bound[T]

fn lower_bound[T](array []T, val T) !T

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

Example:

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

#fn map_indexed[T, R]

fn map_indexed[T, R](array []T, transform fn (int, T) R) []R

map_indexed creates a new array populated with the result of calling the transform function being invoked on each element with its index in the original array.

#fn map_of_counts[T]

fn map_of_counts[T](array []T) map[T]int

map_of_counts returns a map, where each key is an unique value in array, and each value for that key is how many times that value occures 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}

#fn map_of_indexes[T]

fn map_of_indexes[T](array []T) map[T][]int

map_of_indexes returns a map, where each key is an unique value in array, and each value 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]}

#fn max[T]

fn max[T](array []T) !T

max returns the maximum value in the array

Example:

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

#fn merge[T]

direct_array_access
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[T]

fn min[T](array []T) !T

min returns the minimum value in the array

Example:

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

#fn reduce[T]

fn reduce[T](array []T, reduce_op fn (T, T) T) !T

reduce sets acc = array[0], then successively calls acc = reduce_op(acc, elem) for each remaining element in array.

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 reduce_indexed[T]

fn reduce_indexed[T](array []T, reduce_op fn (int, T, T) T) !T

reduce_indexed sets acc = array[0], then successively calls acc = reduce_op(idx, acc, elem) for each remaining element in array.

returns the accumulated value in acc.

returns an error if the array is empty.

See also: fold_indexed.

#fn rotate_left[T]

fn rotate_left[T](mut array &[]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 array.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[T]

fn rotate_right[T](mut array &[]T, k int)

rotate_right rotates the array in-place such that the first array.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 array.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[T]

fn sum[T](array []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([1, 2, 3, 4, 5])? // => 15

#fn upper_bound[T]

fn upper_bound[T](array []T, val T) !T

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

Example:

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

#fn window[T]

fn window[T](array []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.

Example:

arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]]

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]]

Structs

#struct WindowAttribute

pub struct WindowAttribute {
	size int
	step int = 1
}

Interfaces

This section is empty.

Enums

This section is empty.