arrays #

fn chunk #

fn chunk<T>(list []T, size int) [][]T

chunk array to arrays with n elements example: arrays.chunk([1, 2, 3], 2) => [[1, 2], [3]]

fn concat #

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

concatenate two arrays

fn flatten #

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

flattens n + 1 dimensional array into n dimensional array usage: 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

accumulates values with providing initial value and applying providing operation to current accumulator value and each elements.
usage: arrays.fold<string, byte>(['H', 'e', 'l', 'l', 'o'], 0, fn (r int, t string) int { return r + t[0] }) => 149

fn group #

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

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

fn group_by #

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

grouping list of elements with given key selector.
usage: arrays.assort<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

fn idx_min #

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

idx_min returns the index of the minimum value in the array

fn lower_bound #

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

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

fn max #

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

max returns the maximum the maximum value in the array

fn merge #

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

merge two sorted arrays (ascending) and maintain sorted order

fn min #

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

min returns the minimum value in the array

fn reduce #

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

accumulates values with the first element and applying providing operation to current accumulator value and each elements.
if the array is empty, then returns error.
usage: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? => 120

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.
Future work: Fix generic operator overloading detection issue.
usage: 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

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.

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