array
Immutable arrays with constant-time indexing
Its functions are attached to array, so they need no import. Reach for an array over a list when you read elements by index: array.at takes the same time wherever the element is, where list.at walks to it. An array can’t grow or change; make a new one instead.
import nanyx/array
def xs = array.fromList([10, 20, 30])
println(array.length(xs))
println(array.at(xs, 1))
println(array.at(xs, 5))
println(xs(2))
println(xs \map({ x -> x / 10 }))
-- 3
-- #some(20)
-- #notFound
-- #some(30)
-- [1, 2, 3]
An array is callable: xs(i) is array.at(xs, i), so it gives #some(a) | #notFound and never traps. Use it as xs(0) ?? fallback. array.at is in the prelude, so a call of an array works in a module that doesn’t import nanyx/array.
array.create(n) fills each slot with the Default of the element type, so it needs to know that type: def zeros: Array(int) = array.create(3). == compares two arrays element by element. An array has no order and no hash, so it can’t be sorted against another, or put in a set or used as a map’s key.
Reference
Arrays: immutable, fixed-length sequences with constant-time indexing.
An Array(a) holds its elements in one block, so array.at reads any of them in the same time,
where at on a list walks to it. Nothing about an array changes once it is made, so it needs no
Memory. Write one as a literal, Array[1, 2, 3], or make it from a list with array.fromList,
from its indices with array.init, or filled with each slot’s default with array.create.
Each function is attached to array, so it is in scope wherever arrays are, without an import, and is called as
array.length(xs), or as xs \length when xs is known to be an array. == compares two arrays
element by element, and an array is written as a list is, [10, 20, 30].
array.fromList
def array.fromList: list(a) -> array(a)
An array of the elements of xs, in order.
array.fromSeq
def array.fromSeq: [c] ([c] seq(a)) -> array(a)
An array of the elements of s, pulled now, in order: (1..5) \Array.fromSeq is
[1, 2, 3, 4, 5]. It requires the contexts the sequence’s steps do. An endless sequence never
ends: cut it with seq.take.
array.build
def array.build: [c, d] ([c] () -> [d] seq(a)) -> array(a)
An array of what block yields: Array.build { yield 1 if more then yield 2 }. The same as
seq(block) \array.fromSeq.
array.toList
def array.toList: array(a) -> list(a)
The elements of xs, in order, as a list.
array.length
def array.length: array(a) -> int
How many elements xs holds.
array.init
def array.init: [c] int, ([c] int -> a) -> array(a)
An array of count elements, the one at each index f of it: array.init(3, { i -> i * i })
is [0, 1, 4]. A count of 0 or less makes an empty array.
array.create
def array.create: [Default(a)] int -> array(a)
An array of count elements, each the Default of its type: array.create(3) of ints is
[0, 0, 0]. A count of 0 or less makes an empty array.
array.map
def array.map: [c] array(a), ([c] a -> b) -> array(b)
An array of f of each element of xs, in order.
array.fold
def array.fold: [c] array(a), b, ([c] (b, a) -> b) -> b
step applied to initial and the first element of xs, then to that and the second, and so
on to the last: array.fold(xs, 0, { total, x -> total + x }) adds them up.
array.reduce
def array.reduce: [c] array(a), ([c] (a, a) -> a) -> #some(a) | #none
The elements of xs combined with f, in order, starting from the first: Array[1, 2, 3, 4, 5] \array.reduce { * } is #some(120), and an empty array, having no first element, gives #none.
To start from another value, use array.fold.