7.4 KiB
7.4 KiB
module arrays
Contents
- append
- binary_search
- carray_to_varray
- chunk
- chunk_while
- concat
- copy
- distinct
- each
- each_indexed
- filter_indexed
- find_first
- find_last
- flat_map
- flat_map_indexed
- flatten
- fold
- fold_indexed
- group
- group_by
- idx_max
- idx_min
- index_of_first
- index_of_last
- join_to_string
- lower_bound
- map_indexed
- map_of_counts
- map_of_indexes
- max
- merge
- min
- partition
- reduce
- reduce_indexed
- rotate_left
- rotate_right
- sum
- uniq
- uniq_all_repeated
- uniq_only
- uniq_only_repeated
- upper_bound
- window
- WindowAttribute
append
Example
arrays.append([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 3, 5, 7, 2, 4, 6, 8]
binary_search
Example
arrays.binary_search([1, 2, 3, 4], 4)! // => 3
carray_to_varray
chunk
Example
arrays.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 2)) // => [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
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}]
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
copy
distinct
Example
assert arrays.distinct( [5, 5, 1, 5, 2, 1, 1, 9] ) == [1, 2, 5, 9]
each
each_indexed
filter_indexed
find_first
Example
arrays.find_first([1, 2, 3, 4, 5], fn (i int) bool { return i == 3 })? // => 3
find_last
Example
arrays.find_last([1, 2, 3, 4, 5], fn (i int) bool { return i == 3})? // => 3
flat_map
flat_map_indexed
flatten
Example
arrays.flatten[int]([[1, 2, 3], [4, 5]]) // => [1, 2, 3, 4, 5]
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
fold_indexed
group
Example
arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
group_by
Example
arrays.group_by[int, string](['H', 'el', 'lo'], fn (v string) int { return v.len }) // => {1: ['H'], 2: ['el', 'lo']}
idx_max
Example
arrays.idx_max([1, 2, 3, 0, 9])! // => 4
idx_min
Example
arrays.idx_min([1, 2, 3, 0, 9])! // => 3
index_of_first
Example
arrays.index_of_first([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 2
index_of_last
Example
arrays.index_of_last([4,5,0,7,0,9], fn(idx int, x int) bool { return x == 0 }) == 4
join_to_string
lower_bound
Example
arrays.lower_bound([2, 4, 6, 8], 3)! // => 4
map_indexed
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}
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]}
max
Example
arrays.max([1, 2, 3, 0, 9])! // => 9
merge
Example
arrays.merge([1, 3, 5, 7], [2, 4, 6, 8]) // => [1, 2, 3, 4, 5, 6, 7, 8]
min
Example
arrays.min([1, 2, 3, 0, 9])! // => 0
partition
reduce
Example
arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })! // => 120
reduce_indexed
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]
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]
sum
Example
arrays.sum([1, 2, 3, 4, 5])! // => 15
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]
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]
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]
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]
upper_bound
Example
arrays.upper_bound([2, 4, 6, 8], 3)! // => 2
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]]