std.array

Eager helpers for transforming and grouping arrays.

Importuse std.array

Functions

fn sum(values: [int]) -> int#

Wrapping sum; empty arrays yield zero.

  • values: Values to add; the total wraps on overflow.
println(std.array.sum([3, 4, 5])) // 12
fn product(values: [int]) -> int#

Wrapping product; empty arrays yield one.

  • values: Values to multiply; the product wraps on overflow.
println(std.array.product([2, 3, 4])) // 24
fn max_val(values: [int]) -> int#

Returns 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])) // 3
fn min_val(values: [int]) -> int#

Returns the smallest value, or zero for an empty input.

  • values: Values to examine.
fn sort(values: [int]) -> [int]#

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]
fn sort_desc(values: [int]) -> [int]#

Returns the values sorted in descending order.

  • values: Values to sort; the input is left untouched.
fn unique(values: [int]) -> [int]#

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]
fn reverse(values: [int]) -> [int]#

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.
fn take(values: [int], count: int) -> [int]#

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]
fn drop(values: [int], count: int) -> [int]#

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.
fn reduce(values: [int], initial: int, combine: fn(int, int) -> int) -> int#

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 as combine(accumulator, value); its result is the next accumulator.
let total = std.array.reduce([1, 2, 3, 4], 0, |acc, v| acc + v)
println(total) // 10
fn any(values: [int], predicate: fn(int) -> bool) -> bool#

Reports 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)) // false
fn all(values: [int], predicate: fn(int) -> bool) -> bool#

Reports whether the predicate accepts every value.

  • values: Values to examine.
  • predicate: Test applied to values until one is rejected.
fn find_index(values: [int], predicate: fn(int) -> bool) -> int#

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)) // 1
fn count_by(values: [int], predicate: fn(int) -> bool) -> int#

Counts 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)) // 2
fn take_while(values: [int], predicate: fn(int) -> bool) -> [int]#

Returns 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]
fn drop_while(values: [int], predicate: fn(int) -> bool) -> [int]#

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.
fn for_each(values: [int], action: fn(int) -> void)#

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 20
fn contains_int(values: [int], needle: int) -> bool#

Reports 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")) // false
fn index_of(values: [int], needle: int) -> int#

Returns 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.
fn last_index_of(values: [int], needle: int) -> int#

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)) // 3
fn count_val(values: [int], needle: int) -> int#

Counts values equal to the requested value.

  • values: Values to search.
  • needle: Value to count.
println(std.array.count_val([1, 2, 1, 1], 1)) // 3
fn contains_str(values: [string], needle: string) -> bool#

Reports whether the string value is present.

  • values: Values to search.
  • needle: Value to look for.
fn index_of_str(values: [string], needle: string) -> int#

Returns the first matching index, or -1 when absent.

  • values: Values to search.
  • needle: Value to look for.
fn join(values: [string], separator: string) -> string#

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-c
fn reverse_str(values: [string]) -> [string]#

Returns the strings in reverse order.

  • values: Strings to reverse; the input is left untouched.
fn range(start: int, end: int) -> [int]#

Returns the half-open integer range from start to end.

  • start: First value included.
  • end: One past the last value; not greater than start yields an empty array.
println(std.array.range(0, 5)) // [0, 1, 2, 3, 4]
fn range_step(start: int, end: int, step: int) -> [int]#

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]
fn enumerate<T>(values: [T]) -> [(int, T)]#

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")]
fn flatten<T>(nested: [[T]]) -> [T]#

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]
fn repeat_val<T>(value: T, count: int) -> [T]#

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"]
fn zip<A, B>(left: [A], right: [B]) -> [(A, B)]#

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