std.collections — Array and map helpers
Implemented — importable on both engines, byte-identical.
std.collections provides array and map utility operations. The *_str
variants are string-array companions to the integer-array helpers. Map
iterating relies on the runtime builtin map_keys_of (wired into the Go
resolver, Go codegen and the kcc checker/sema/codegen), plus the builtins
push, len and hasKey.
All functions are implemented in pure canonical Karkain with untyped
parameters and are identical on the Go and kcc engines.
Importing
import std.collections
Module reference
Lookup
array_contains(arr, val)Returns
trueif the integer arrayarrcontainsval. Returns:boolarray_contains_str(arr, val)Returns
trueif the string arrayarrcontainsval. Returns:boolarray_index_of(arr, val)Returns the index of the first occurrence of
val(anint) inarr, or-1if not found. Returns:intarray_index_of_str(arr, val)Returns the index of the first occurrence of
val(a string) inarr, or-1if not found. Returns:int
Construction
array_fill(val, n)Returns a new array of
nelements all set toval. Note the parameter order: value first, count second. Returns:arrayarray_copy(arr)Returns a shallow copy of the integer array
arr. Returns:arrayarray_copy_str(arr)Returns a shallow copy of the string array
arr. Returns:arrayarray_reverse(arr)Returns a new array with the elements of the integer array
arrin reverse order (the original is unmodified). Returns:arrayarray_reverse_str(arr)Returns a new array with the elements of the string array
arrin reverse order. Returns:array
Shaping
array_slice(arr, start, end)Returns a sub-array of the integer array
arrfromstartup to (but not including)end, bounded bylen(arr). Returns:arrayarray_slice_str(arr, start, end)Returns a sub-array of the string array
arrfromstartup to (but not including)end. Returns:arrayarray_remove(arr, val)Returns a new array with the first occurrence of
valremoved. Returns:arrayarray_remove_at(arr, index)Returns a new array with the element at
indexremoved. Returns:arrayarray_insert(arr, index, val)Returns a new array with
valinserted atindex, shifting subsequent elements to the right. Returns:arrayarray_unique(arr)Returns a new array with duplicate elements removed (first occurrence kept). Returns:
arrayarray_flatten(arr)Concatenates an array of arrays into a single flat array. Returns:
array
Aggregation
array_sum(arr)Returns the sum of all elements. Returns:
intarray_min(arr)Returns the minimum element, or
0for an empty array. Returns:intarray_max(arr)Returns the maximum element, or
0for an empty array. Returns:intarray_max_2(arr)Returns
[index, value]identifying the maximum element;indexis-1andvalueis0for an empty array. Returns:array—[int, int]
Maps
map_keys(m)Returns an array of all keys in the map
m. Delegates to the runtime builtinmap_keys_of. Returns:arraymap_values(m)Returns an array of all values in the map
m, in the same order asmap_keys. Returns:arraymap_contains_key(m, key)Returns
trueif the mapmcontainskey. Delegates tohasKey. Returns:boolmap_merge(a, b)Returns a new map combining
aandb; values fromboverride values fromafor overlapping keys. Returns:map
Example
import std.collections
func main() {
let nums = [3, 1, 4, 1, 5]
println(array_contains(nums, 4)) // true
println(array_fill(9, 3)) // [9, 9, 9]
println(array_sum(nums)) // 14
println(array_max(nums)) // 5
let m = {"a": 1, "b": 2}
println(map_keys(m)) // keys of m
println(map_values(m)) // [1, 2] in key order
println(map_contains_key(m, "a")) // true
}
Note
Map iteration order follows map_keys_of (the runtime builtin): keys are
returned in the runtime’s internal order, which is deterministic for a given
program but is not insertion order. Do not rely on map_keys /
map_values ordering across programs.
See also
collections — the language’s built-in array and map support.