array
stdlib/core/array.hk: fixed-size contiguous numeric arrays.
Array<T> is narrower than List<T>: T is one of f64, f32, i64 or i32, elements are stored unboxed at their exact width, and the size never changes in place. set follows functional-but-in-place (FBIP) semantics: a unique array reuses its buffer; a shared array is copied before the write; aliases continue to observe the old value.
ArrayElement
trait ArrayElement
The closed element set and its private runtime representation witness. A bound on every constructor makes Array<string> and other boxed element kinds a static type error; no dynamic rejection is needed.
arraykind
def _array_kind() -> i32
impl ArrayElement<f64>
arraykind
def _array_kind() -> i32
@no-doctest: private representation witness
impl ArrayElement<f32>
arraykind
def _array_kind() -> i32
@no-doctest: private representation witness
impl ArrayElement<i64>
arraykind
def _array_kind() -> i32
@no-doctest: private representation witness
impl ArrayElement<i32>
arraykind
def _array_kind() -> i32
@no-doctest: private representation witness
Array
type Array<T>
end
impl<T: ArrayElement> Array<T>
filled
def filled(n: int, x: T) -> Array<T>
A fixed-size array containing n copies of x. A count of zero or less produces an empty array.
xs: Array<i32> = Array.filled(3, 7i32)
xs.length => 3
xs.get(2) => Some(7i32)
from_list
def from_list(xs: List<T>) -> Array<T>
Copy a list into one contiguous array, preserving order.
xs: List<i64> = List.empty().append(2i64).append(4i64)
Array.from_list(xs).get_or(1, 0i64) => 4i64
get
def get(self, i: i64) -> Option<T>
The element at i, or None for a negative or past-the-end index.
xs: Array<f64> = Array.filled(2, 1.5f64)
match xs.get(0)
Some(_) -> 1
None -> 0
end => 1
get_or
def get_or(self, i: i64, fallback: T) -> T
The element at i, or fallback when it is out of bounds. This avoids allocating the Some wrapper in tight numeric loops.
xs: Array<i32> = Array.filled(2, 9i32)
xs.get_or(7, 3i32) => 3i32
set
def set(self, i: i64, x: T) -> Array<T>
Return the array with index i replaced by x. A unique receiver is updated in place; a shared receiver is copied first. An out-of-bounds index returns the receiver unchanged.
before: Array<i32> = Array.filled(2, 1i32)
after = before.set(1, 8i32)
before.get_or(1, 0i32) => 1i32
after.get_or(1, 0i32) => 8i32
copy_resize
def copy_resize(self, n: int, fill: T) -> Array<T>
Copy into a new fixed size, preserving the common prefix and filling a grown tail with fill.
xs: Array<i64> = Array.filled(2, 5i64)
ys = xs.copy_resize(4, 9i64)
ys.get_or(3, 0i64) => 9i64
impl<T> Array<T>
length
prop length(self) -> i64
The number of elements.
Array.filled(4, 0.0f32).length => 4
_filled
def _filled(kind: i32, n: int, x: T) -> Array<T>
@no-doctest: private representation-bearing constructor
fromlist
def _from_list(kind: i32, xs: List<T>) -> Array<T>
@no-doctest: private representation-bearing constructor
_get
def _get(self, kind: i32, i: i64) -> Option<T>
@no-doctest: private representation-bearing read
getor
def _get_or(self, kind: i32, i: i64, fallback: T) -> T
@no-doctest: private representation-bearing read
_set
def _set(self, kind: i32, i: i64, x: T) -> Array<T>
@no-doctest: private representation-bearing update
copyresize
def _copy_resize(self, kind: i32, n: int, fill: T) -> Array<T>
@no-doctest: private representation-bearing resize