gossamer/array
Types
A JS Array — an indexed, ordered, mutable list of values.
For most Gleam use cases, prefer List. This binding exists for JS
interop where a JS Array is specifically required.
Value-based search functions (includes, index_of,
last_index_of) match object values (records, lists, tuples) by
JS reference identity, not value equality — two equal-by-value
tuples constructed separately are distinct. Primitive values use
value equality. Predicate-based search (find, every, some,
etc.) is unaffected.
See Array on MDN.
pub type Array(a)
Values
pub fn at(array: Array(a), index index: Int) -> Result(a, Nil)
Returns the element at index, or Error(Nil) if the index is out of
range. Negative indices count from the end.
pub fn copy_within_range(
array: Array(a),
to target: Int,
from start: Int,
up_to end: Int,
) -> Array(a)
pub fn entries(
of array: Array(a),
) -> iterator.Iterator(#(Int, a), Nil, Nil)
pub fn find(
in array: Array(a),
one_that predicate: fn(a) -> Bool,
) -> Result(a, Nil)
Returns the first value matching predicate, or Error(Nil) if none
match.
pub fn find_index(
in array: Array(a),
one_that predicate: fn(a) -> Bool,
) -> Result(Int, Nil)
Returns the index of the first value matching predicate, or
Error(Nil) if none match.
pub fn find_last(
in array: Array(a),
one_that predicate: fn(a) -> Bool,
) -> Result(a, Nil)
Returns the last value matching predicate, or Error(Nil) if none
match.
pub fn find_last_index(
in array: Array(a),
one_that predicate: fn(a) -> Bool,
) -> Result(Int, Nil)
Returns the index of the last value matching predicate, or
Error(Nil) if none match.
pub fn from_list_mapped(
list: List(a),
with mapper: fn(a) -> b,
) -> Array(b)
pub fn includes_from(
in array: Array(a),
value value: a,
from index: Int,
) -> Bool
pub fn index_every(
in array: Array(a),
satisfying predicate: fn(a, Int) -> Bool,
) -> Bool
pub fn index_find(
in array: Array(a),
one_that predicate: fn(a, Int) -> Bool,
) -> Result(a, Nil)
Like find, but passes the index alongside each value to predicate.
pub fn index_find_index(
in array: Array(a),
one_that predicate: fn(a, Int) -> Bool,
) -> Result(Int, Nil)
Like find_index, but passes the index alongside each value to
predicate.
pub fn index_find_last(
in array: Array(a),
one_that predicate: fn(a, Int) -> Bool,
) -> Result(a, Nil)
Like find_last, but passes the index alongside each value to
predicate.
pub fn index_find_last_index(
in array: Array(a),
one_that predicate: fn(a, Int) -> Bool,
) -> Result(Int, Nil)
Like find_last_index, but passes the index alongside each value to
predicate.
pub fn index_for_each(
in array: Array(a),
run callback: fn(a, Int) -> b,
) -> Nil
pub fn index_of(
in array: Array(a),
value value: a,
) -> Result(Int, Nil)
Returns the first index of value in the array, or Error(Nil) if the
value is not found.
pub fn index_of_from(
in array: Array(a),
value value: a,
from index: Int,
) -> Result(Int, Nil)
Like index_of, but starts searching from index.
pub fn index_reduce(
over array: Array(a),
from initial: b,
with callback: fn(b, a, Int) -> b,
) -> b
pub fn index_reduce_right(
over array: Array(a),
from initial: b,
with callback: fn(b, a, Int) -> b,
) -> b
pub fn index_some(
in array: Array(a),
satisfying predicate: fn(a, Int) -> Bool,
) -> Bool
pub fn keys(
of array: Array(a),
) -> iterator.Iterator(Int, Nil, Nil)
pub fn last_index_of(
in array: Array(a),
value value: a,
) -> Result(Int, Nil)
Returns the last index of value in the array, or Error(Nil) if the
value is not found.
pub fn last_index_of_from(
in array: Array(a),
value value: a,
from index: Int,
) -> Result(Int, Nil)
Like last_index_of, but searches backwards from index.
pub fn pop(from array: Array(a)) -> Result(a, Nil)
Returns the removed element, or an error if the array is empty.
pub fn reduce(
over array: Array(a),
from initial: b,
with callback: fn(b, a) -> b,
) -> b
pub fn reduce_right(
over array: Array(a),
from initial: b,
with callback: fn(b, a) -> b,
) -> b
pub fn shift(from array: Array(a)) -> Result(a, Nil)
Returns the removed element, or an error if the array is empty.
pub fn sort(
array: Array(a),
by compare: fn(a, a) -> order.Order,
) -> Array(a)
pub fn splice(
in array: Array(a),
from start: Int,
removing count: Int,
) -> Array(a)
Removes elements and returns them. Mutates the array.
pub fn splice_with(
in array: Array(a),
from start: Int,
removing count: Int,
inserting items: List(a),
) -> Array(a)
Removes elements, inserts new ones, and returns the removed elements. Mutates the array.
pub fn to_sorted(
array: Array(a),
by compare: fn(a, a) -> order.Order,
) -> Array(a)
pub fn to_spliced_with(
array: Array(a),
from start: Int,
removing count: Int,
inserting items: List(a),
) -> Array(a)
pub fn values(
of array: Array(a),
) -> iterator.Iterator(a, Nil, Nil)
pub fn with(
array: Array(a),
at_index index: Int,
value value: a,
) -> Result(Array(a), js_error.JsError)
Returns a copy of the array with the element at index replaced by
value. Negative indices count from the end. Returns an error if
index is out of range.