Skip to content

Array

All array functions return new arrays. They never mutate the original.

All stdlib functions are pipe-friendly: the first argument is the data, so they work naturally with |>.

[3, 1, 2]
|> Array.sort
|> Array.map((n) => n * 10)
|> Array.reverse
// [30, 20, 10]
FunctionSignatureDescription
Array.sortArray<number> -> Array<number>Sort numerically (returns new array)
Array.sortByArray<T>, (T) -> number -> Array<T>Sort by numeric key function
Array.mapArray<T>, (T) -> U -> Array<U>Transform each element
Array.filterArray<T>, (T) -> boolean -> Array<T>Keep elements matching predicate
Array.findArray<T>, (T) -> boolean -> Option<T>First element matching predicate
Array.findIndexArray<T>, (T) -> boolean -> Option<number>Index of first match
Array.findLastArray<T>, (T) -> boolean -> Option<T>Last element matching predicate
Array.flatMapArray<T>, (T) -> Array<U> -> Array<U>Map then flatten one level
Array.filterMapArray<T>, (T) -> Option<U> -> Array<U>Map + filter in one pass (keeps Some values)
Array.atArray<T>, number -> Option<T>Safe index access
Array.containsArray<T>, T -> booleanCheck if element exists (structural equality)
Array.headArray<T> -> Option<T>First element
Array.lastArray<T> -> Option<T>Last element
Array.takeArray<T>, number -> Array<T>First n elements
Array.takeWhileArray<T>, (T) -> boolean -> Array<T>Take elements while predicate holds
Array.dropArray<T>, number -> Array<T>All except first n elements
Array.dropWhileArray<T>, (T) -> boolean -> Array<T>Drop elements while predicate holds
Array.reverseArray<T> -> Array<T>Reverse order (returns new array)
Array.flattenArray<Array<T>> -> Array<T>Flatten one level of nesting
Array.reduceArray<T>, (U, T) -> U, U -> UFold with reducer and initial value
Array.lengthArray<T> -> numberNumber of elements
Array.anyArray<T>, (T) -> boolean -> booleanTrue if any element matches predicate
Array.allArray<T>, (T) -> boolean -> booleanTrue if all elements match predicate
Array.sumArray<number> -> numberSum all elements
Array.joinArray<string>, string -> stringJoin elements with separator
Array.isEmptyArray<T> -> booleanTrue if array has no elements
Array.chunkArray<T>, number -> Array<Array<T>>Split into chunks of given size
Array.uniqueArray<T> -> Array<T>Remove duplicate elements
Array.uniqueByArray<T>, (T) -> U -> Array<T>Deduplicate by key function (keeps first)
Array.indexOfArray<T>, T -> Option<number>Find index of element (None if not found)
Array.groupByArray<T>, (T) -> string -> RecordGroup elements by key function
Array.zipArray<T>, Array<U> -> Array<(T, U)>Pair elements by index from two arrays
Array.concatArray<T>, Array<T> -> Array<T>Concatenate two arrays
Array.appendArray<T>, T -> Array<T>Append an element to the end
Array.prependArray<T>, T -> Array<T>Prepend an element to the start
Array.fromT, (T, number) -> U -> Array<U>Create array from iterable with mapping
Array.partitionArray<T>, (T) -> boolean -> (Array<T>, Array<T>)Split into (matching, non-matching)
Array.intersperseArray<T>, T -> Array<T>Insert element between every pair
Array.mapResultArray<T>, (T) -> Result<U, E> -> Result<Array<U>, E>Map fallible function, short-circuit on first Err
// Sort returns a new array, original unchanged
const nums = [3, 1, 2]
const sorted = Array.sort(nums) // [1, 2, 3]
// nums is still [3, 1, 2]
// Safe access returns Option
const first = Array.head([1, 2, 3]) // Some(1)
const empty = Array.head([]) // None
// Structural equality for contains
const user1 = User(name: "Ryan")
const found = Array.contains(users, user1) // true if any user matches by value
// Pipe chains with dot shorthand
const result = users
|> Array.filter(.active)
|> Array.sortBy(.name)
|> Array.take(10)
|> Array.map(.email)
// Check predicates
const hasAdmin = users |> Array.any(.role == "admin") // true/false
const allActive = users |> Array.all(.active) // true/false
// Aggregate
const total = [1, 2, 3] |> Array.sum // 6
const csv = ["a", "b", "c"] |> Array.join(", ") // "a, b, c"
// filterMap — map + filter in one pass
const ages = inputs |> Array.filterMap((s) => Number.parse(s) |> Result.toOption)
// partition — split into two groups
const (adults, minors) = users |> Array.partition(.age >= 18)
// intersperse — great for React
const items = ["Home", "About", "Contact"]
|> Array.intersperse(" | ")
// ["Home", " | ", "About", " | ", "Contact"]
// Utilities
const empty = Array.isEmpty([]) // true
const chunks = [1, 2, 3, 4, 5] |> Array.chunk(2) // [[1, 2], [3, 4], [5]]
const deduped = [1, 2, 2, 3] |> Array.unique // [1, 2, 3]
const dedupedBy = users |> Array.uniqueBy(.email) // keeps first per unique email
const idx = ["a", "b", "c"] |> Array.indexOf("b") // Some(1)
const grouped = users |> Array.groupBy(.role) // { admin: [...], user: [...] }