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 binary_search #
fn binary_search<T>(array []T, target T) !int
binary search, requires array to be sorted, returns index of found item or error.
Binary searches on sorted lists can be faster than other array searches because at maximum the algorithm only has to traverse log N elements
Example
arrays.binary_search([1, 2, 3, 4], 4)? // => 3
fn carray_to_varray #
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>(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 #
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 filter_indexed #
fn filter_indexed<T>(array []T, predicate fn (idx int, elem 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 #
fn flat_map<T, R>(array []T, transform fn (elem 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 #
fn flat_map_indexed<T, R>(array []T, transform fn (idx int, elem 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 #
fn flatten<T>(array [][]T) []T
flatten 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>(array []T, init R, fold_op fn (acc R, elem 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(a, 0,
fn (r int, t string) int { return r + t.len })
assert r == 5
fn fold_indexed #
fn fold_indexed<T, R>(array []T, init R, fold_op fn (idx int, acc R, elem 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 #
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<int>([1,2,3],[4,5,6]) // => [[1, 4], [2, 5], [3, 6]]
fn group_by #
fn group_by<K, V>(array []V, grouping_op fn (val 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>(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 #
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 lower_bound #
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 #
fn map_indexed<T, R>(array []T, transform fn (idx int, elem 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 max #
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 #
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>(array []T) !T
min returns the minimum value in the array
Example
arrays.min([1,2,3,0,9]) // => 0
fn reduce #
fn reduce<T>(array []T, reduce_op fn (acc T, elem 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 #
fn reduce_indexed<T>(array []T, reduce_op fn (idx int, acc T, elem 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 #
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 #
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 #
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<int>([1, 2, 3, 4, 5])? // => 15
fn upper_bound #
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 #
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 sizestep- 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
}
- README
- fn binary_search
- fn carray_to_varray
- fn chunk
- fn concat
- fn copy
- fn filter_indexed
- fn flat_map
- fn flat_map_indexed
- fn flatten
- fn fold
- fn fold_indexed
- fn group
- fn group_by
- fn idx_max
- fn idx_min
- fn lower_bound
- fn map_indexed
- fn max
- fn merge
- fn min
- fn reduce
- fn reduce_indexed
- fn rotate_left
- fn rotate_right
- fn sum
- fn upper_bound
- fn window
- struct WindowAttribute