Standard library · Arrays
- The language
- Agents and tools
- Model providers
- The Anthropic API
- Talking agents
- Standard library
- Glossary
- Package docs
- Telemetry
- Robots
- Servers
- Deploying
- tin.ad
- The tin command
- How it works
On this page
Arrays
These return new arrays and leave the original as it was, so xs.sort() on a line of its own is a compile error (it would do nothing): keep the result, xs = xs.sort(). The same holds for trim, upper, replace and the other string methods. push and pop (built in) change a var in place.
Example
let scores = [72, 95, 58, 88]
print(scores.filter(s => s >= 70).map(s => s / 10))
print(sort(scores).reverse().slice(0, 2))
print(scores.reduce(0, (best, s) => max(best, s)))
print(scores.find(s => s < 60))
print("{sum(scores)} over {scores.len}, any perfect: {scores.any(s => s == 100)}") [7, 9, 8]
[95, 88]
95
Some(58)
313 over 4, any perfect: false All 21 symbols
mapfilterreduceeachsumcountanyallfindrangefirstlastArray.sliceArray.reverseArray.concatArray.indexOfArray.containsArray.isEmptyfilledsortsortBy
map
fn map<T, U>(xs: T[], f: (T) => U): U[] A new array with f applied to every item.
filter
fn filter<T>(xs: T[], keep: (T) => bool): T[] A new array with the items for which `keep` is true.
reduce
fn reduce<T, A>(xs: T[], init: A, f: (A, T) => A): A Folds the array into one value: starts from `init` and combines it with each item in turn.
each
fn each<T>(xs: T[], f: (T) => void) Calls f for every item.
sum
fn sum<T>(xs: T[]): T All items added together. Works on numbers, durations and strings.
count
fn count<T>(xs: T[], pred: (T) => bool): int How many items satisfy `pred`.
any
fn any<T>(xs: T[], pred: (T) => bool): bool Whether at least one item satisfies `pred`. Stops at the first that does.
all
fn all<T>(xs: T[], pred: (T) => bool): bool Whether every item satisfies `pred`. Stops at the first that does not.
find
fn find<T>(xs: T[], pred: (T) => bool): Option<T> The first item that satisfies `pred`, or None.
range
fn range(from: int, to: int): int[] The integers from `from` up to, but not including, `to`.
first
fn first<T>(xs: T[]): Option<T> The first item, or None for an empty array.
last
fn last<T>(xs: T[]): Option<T> The last item, or None for an empty array.
Array.slice
fn Array.slice<T>(xs: T[], from: int, to: int): T[] A new array with the items from `from` up to, but not including, `to`. Positions past either end are clamped.
Array.reverse
fn Array.reverse<T>(xs: T[]): T[] A new array with the items in reverse order.
Array.concat
fn Array.concat<T>(xs: T[], ys: T[]): T[] A new array with the items of xs followed by those of ys.
Array.indexOf
fn Array.indexOf<T: Eq>(xs: T[], item: T): int Where `item` first occurs, or -1. Uses ==, so it works on numbers, strings and bools.
Array.contains
fn Array.contains<T: Eq>(xs: T[], item: T): bool Whether `item` occurs in xs.
Array.isEmpty
fn Array.isEmpty<T>(xs: T[]): bool Whether xs has no items.
filled
fn filled<T>(n: int, value: T): T[] A new array of n items, each a copy of value: filled(1000000, false). Panics if n is negative.
sort
fn sort<T: Ord>(xs: T[]): T[] A new array in ascending order (the Ord order): numbers, strings, durations and any type with `compare`; NaN sorts last. Stable (equal items keep their order), O(n log n) on every input.
sortBy
fn sortBy<T>(xs: T[], less: (T, T) => bool): T[] A new array ordered by `less`, which returns true when its first argument should come first. Stable, O(n log n).