Files
herolib/vdocs/arrays.md
2025-02-07 12:07:32 +03:00

7.4 KiB

module arrays

Contents

append

Example


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

[Return to contents]

Example


arrays.binary_search([1, 2, 3, 4], 4)! // => 3

[Return to contents]

carray_to_varray

[Return to contents]

chunk

Example


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

[Return to contents]

chunk_while

Examples


assert arrays.chunk_while([0,9,2,2,3,2,7,5,9,5],fn(x int,y int)bool{return x<=y})==[[0,9],[2,2,3],[2,7],[5,9],[5]]

assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y})==[[`a`,`a`,`a`,`a`],[`b`,`b`,`b`],[`c`,`c`],[`a`]]

assert arrays.chunk_while('aaaabbbcca'.runes(),fn(x rune,y rune)bool{return x==y}).map({it[0]:it.len})==[{`a`:4},{`b`:3},{`c`:2},{`a`:1}]

[Return to contents]

concat

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

[Return to contents]

copy

[Return to contents]

distinct

Example


assert arrays.distinct( [5, 5, 1, 5, 2, 1, 1, 9] ) == [1, 2, 5, 9]

[Return to contents]

each

[Return to contents]

each_indexed

[Return to contents]

filter_indexed

[Return to contents]

find_first

Example


arrays.find_first([1, 2, 3, 4, 5], fn (i int) bool { return i == 3 })? // => 3

[Return to contents]

find_last

Example


arrays.find_last([1, 2, 3, 4, 5], fn (i int) bool { return i == 3})? // => 3

[Return to contents]

flat_map

[Return to contents]

flat_map_indexed

[Return to contents]

flatten

Example


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

[Return to contents]

fold

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

[Return to contents]

fold_indexed

[Return to contents]

group

Example


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

[Return to contents]

group_by

Example


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

[Return to contents]

idx_max

Example


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

[Return to contents]

idx_min

Example


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

[Return to contents]

index_of_first

Example


arrays.index_of_first([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 2

[Return to contents]

index_of_last

Example


arrays.index_of_last([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 4

[Return to contents]

join_to_string

[Return to contents]

lower_bound

Example


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

[Return to contents]

map_indexed

[Return to contents]

map_of_counts

Example


arrays.map_of_counts([1,2,3,4,4,2,1,4,4]) == {1: 2, 2: 2, 3: 1, 4: 4}

[Return to contents]

map_of_indexes

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

[Return to contents]

max

Example


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

[Return to contents]

merge

Example


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

[Return to contents]

min

Example


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

[Return to contents]

partition

[Return to contents]

reduce

Example


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

[Return to contents]

reduce_indexed

[Return to contents]

rotate_left

Example


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

[Return to contents]

rotate_right

Example


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

[Return to contents]

sum

Example


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

[Return to contents]

uniq

Examples


assert arrays.uniq( []int{} ) == []

assert arrays.uniq( [1, 1] ) == [1]

assert arrays.uniq( [2, 1] ) == [2, 1]

assert arrays.uniq( [5, 5, 1, 5, 2, 1, 1, 9] ) == [5, 1, 5, 2, 1, 9]

[Return to contents]

uniq_all_repeated

Examples


assert arrays.uniq_all_repeated( []int{} ) == []

assert arrays.uniq_all_repeated( [1, 5] ) == []

assert arrays.uniq_all_repeated( [5, 5] ) == [5,5]

assert arrays.uniq_all_repeated( [5, 5, 1, 5, 2, 1, 1, 9] ) == [5, 5, 1, 1]

[Return to contents]

uniq_only

Examples


assert arrays.uniq_only( []int{} ) == []

assert arrays.uniq_only( [1, 1] ) == []

assert arrays.uniq_only( [2, 1] ) == [2, 1]

assert arrays.uniq_only( [1, 5, 5, 1, 5, 2, 1, 1, 9] ) == [1, 1, 5, 2, 9]

[Return to contents]

uniq_only_repeated

Examples


assert arrays.uniq_only_repeated( []int{} ) == []

assert arrays.uniq_only_repeated( [1, 5] ) == []

assert arrays.uniq_only_repeated( [5, 5] ) == [5]

assert arrays.uniq_only_repeated( [5, 5, 1, 5, 2, 1, 1, 9] ) == [5, 1]

[Return to contents]

upper_bound

Example


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

[Return to contents]

window

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

[Return to contents]

WindowAttribute

[Return to contents]

Powered by vdoc. Generated on: 7 Feb 2025 12:06:55