Functions
Wrapping sum; empty arrays yield zero.
values: Values to add; the total wraps on overflow.
println(std.array.sum([3, 4, 5])) // 12Wrapping product; empty arrays yield one.
values: Values to multiply; the product wraps on overflow.
println(std.array.product([2, 3, 4])) // 24Returns the largest value, or zero for an empty input.
values: Values to examine.
println(std.array.max_val([3, 9, 4])) // 9
println(std.array.min_val([3, 9, 4])) // 3Returns the smallest value, or zero for an empty input.
values: Values to examine.
Ascending integer sort over a fresh array.
values: Values to sort; the input is left untouched.
println(std.array.sort([3, 1, 2])) // [1, 2, 3]
println(std.array.sort_desc([3, 1, 2])) // [3, 2, 1]Returns the values sorted in descending order.
values: Values to sort; the input is left untouched.
Returns first occurrences in input order.
values: Values to deduplicate, keeping the first of each.
println(std.array.unique([1, 2, 1, 3, 2])) // [1, 2, 3]Returns the values in reverse order.
values: Values to reverse; the input is left untouched.
println(std.array.reverse([1, 2, 3])) // [3, 2, 1]
// reverse_str does the same for string arrays.Returns at most the requested number of leading values.
values: Values to take from.count: How many leading values to keep; clamped to the array length, and a negative count keeps none.
println(std.array.take([1, 2, 3, 4], 2)) // [1, 2]
println(std.array.drop([1, 2, 3, 4], 2)) // [3, 4]Drops the requested number of leading values.
values: Values to drop from.count: How many leading values to skip; clamped to the array length, and a negative count skips none.
Folds values from left to right with an initial accumulator.
values: Values to fold, visited left to right.initial: Accumulator the first call receives.combine: Called ascombine(accumulator, value); its result is the next accumulator.
let total = std.array.reduce([1, 2, 3, 4], 0, |acc, v| acc + v)
println(total) // 10Reports whether the predicate accepts any value.
values: Values to examine.predicate: Test applied to values until one is accepted.
println(std.array.any([1, 3, 4], |v| v % 2 == 0)) // true
println(std.array.all([1, 3, 4], |v| v % 2 == 0)) // falseReports whether the predicate accepts every value.
values: Values to examine.predicate: Test applied to values until one is rejected.
Returns the first index accepted by the predicate, or -1.
values: Values to search.predicate: Test applied left to right; the first accepted index wins.
println(std.array.find_index([5, 8, 13], |v| v > 6)) // 1Counts values accepted by the predicate.
values: Values to examine.predicate: Test applied to every value.
println(std.array.count_by([1, 2, 3, 4], |v| v > 2)) // 2Returns the longest leading run accepted by the predicate.
values: Values to scan from the front.predicate: Test that ends the run at its first rejection.
println(std.array.take_while([2, 4, 5, 6], |v| v % 2 == 0)) // [2, 4]
println(std.array.drop_while([2, 4, 5, 6], |v| v % 2 == 0)) // [5, 6]Drops the longest leading run accepted by the predicate.
values: Values to scan from the front.predicate: Test that ends the dropped run at its first rejection.
Calls the action once for every value in order.
values: Values to visit, in order.action: Called once per value for its side effect.
std.array.for_each([10, 20], |v| println(v)) // prints 10, then 20Reports whether the integer value is present.
values: Values to search.needle: Value to look for.
println(std.array.contains_int([1, 2, 3], 2)) // true
println(std.array.contains_str(["a", "b"], "z")) // falseReturns the first matching index, or -1 when absent.
values: Values to search.needle: Value to look for.
println(std.array.index_of([5, 7, 9], 7)) // 1
println(std.array.index_of([5, 7, 9], 8)) // -1
// index_of_str does the same for string arrays.Returns the last matching index, or -1 when absent.
values: Values to search.needle: Value to look for.
println(std.array.last_index_of([1, 2, 1, 2], 2)) // 3Counts values equal to the requested value.
values: Values to search.needle: Value to count.
println(std.array.count_val([1, 2, 1, 1], 1)) // 3Reports whether the string value is present.
values: Values to search.needle: Value to look for.
Returns the first matching index, or -1 when absent.
values: Values to search.needle: Value to look for.
Joins the supplied values into one result.
values: Pieces to concatenate, in order.separator: Value placed between consecutive pieces.
println(std.array.join(["a", "b", "c"], "-")) // a-b-cReturns the strings in reverse order.
values: Strings to reverse; the input is left untouched.
Returns the half-open integer range from start to end.
start: First value included.end: One past the last value; not greater thanstartyields an empty array.
println(std.array.range(0, 5)) // [0, 1, 2, 3, 4]Returns a stepped half-open integer range.
start: First value included.end: Bound the range stops before, in the direction of travel.step: Increment between values; a step of zero yields an empty array.
println(std.array.range_step(10, 0, -3)) // [10, 7, 4, 1]Pairs every value with its zero-based index.
values: Values to pair with their positions.
println(std.array.enumerate(["x", "y"])) // [(0, "x"), (1, "y")]Concatenates nested arrays in source order.
nested: Arrays to concatenate, in order.
println(std.array.flatten([[1, 2], [3], [4, 5]])) // [1, 2, 3, 4, 5]Repeats the value the requested number of times.
value: Value stored in every slot.count: How many slots to fill; zero or negative yields an empty array.
println(std.array.repeat_val("ha", 3)) // ["ha", "ha", "ha"]Pairs elements up to the shorter input length.
left: Values supplying the first half of each pair.right: Values supplying the second half; the longer input is truncated.
println(std.array.zip(["a", "b", "c"], [1, 2])) // [("a", 1), ("b", 2)]