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 concat(array: Array(a), and other: Array(a)) -> Array(a)
pub fn copy_within(
  array: Array(a),
  to target: Int,
  from start: Int,
) -> Array(a)
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 every(
  in array: Array(a),
  satisfying predicate: fn(a) -> Bool,
) -> Bool
pub fn fill(array: Array(a), with value: a) -> Array(a)
pub fn fill_range(
  array: Array(a),
  with value: a,
  from start: Int,
  to end: Int,
) -> Array(a)
pub fn filter(
  in array: Array(a),
  keeping predicate: fn(a) -> Bool,
) -> Array(a)
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 flat(array: Array(Array(a))) -> Array(a)
pub fn flat_map(
  over array: Array(a),
  with callback: fn(a) -> Array(b),
) -> Array(b)
pub fn for_each(
  in array: Array(a),
  run callback: fn(a) -> b,
) -> Nil
pub fn from_list(list: List(a)) -> Array(a)
pub fn from_list_mapped(
  list: List(a),
  with mapper: fn(a) -> b,
) -> Array(b)
pub fn includes(in array: Array(a), value value: a) -> Bool
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_filter(
  in array: Array(a),
  keeping predicate: fn(a, Int) -> Bool,
) -> Array(a)
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_map(
  over array: Array(a),
  with callback: fn(a, Int) -> b,
) -> Array(b)
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 join(array: Array(a), with separator: String) -> String
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 length(of array: Array(a)) -> Int
pub fn map(
  over array: Array(a),
  with callback: fn(a) -> b,
) -> Array(b)
pub fn new() -> Array(a)
pub fn pop(from array: Array(a)) -> Result(a, Nil)

Returns the removed element, or an error if the array is empty.

pub fn push(to array: Array(a), value value: a) -> Array(a)
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 reverse(array: Array(a)) -> Array(a)
pub fn shift(from array: Array(a)) -> Result(a, Nil)

Returns the removed element, or an error if the array is empty.

pub fn slice(array: Array(a)) -> Array(a)
pub fn slice_from(array: Array(a), start: Int) -> Array(a)
pub fn slice_range(
  array: Array(a),
  from start: Int,
  to end: Int,
) -> Array(a)
pub fn some(
  in array: Array(a),
  satisfying predicate: fn(a) -> Bool,
) -> Bool
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_list(array: Array(a)) -> List(a)
pub fn to_reversed(array: Array(a)) -> Array(a)
pub fn to_sorted(
  array: Array(a),
  by compare: fn(a, a) -> order.Order,
) -> Array(a)
pub fn to_spliced(
  array: Array(a),
  from start: Int,
  removing count: Int,
) -> Array(a)
pub fn to_spliced_with(
  array: Array(a),
  from start: Int,
  removing count: Int,
  inserting items: List(a),
) -> Array(a)
pub fn to_string(array: Array(a)) -> String
pub fn unshift(to array: Array(a), value value: 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.

Search Document