Skip to content

Arr

const Arr: object

Defined in: Data/Arr.ts:1160

append: <A>(value) => (data) => NonEmptyArr<A>

Appends a value to the end of an array, returning a NonEmptyArr.

A

A

(data) => NonEmptyArr<A>

pipe([1, 2], Arr.append(3)); // [1, 2, 3]

at: (index) => <A>(data) => Maybe<A>

Safely looks up an element by index. Supports negative indices counting back from the end. Returns None if the index is out of bounds.

number

<A>(data) => Maybe<A>

pipe([10, 20, 30], Arr.at(1));  // Some(20)
pipe([10, 20, 30], Arr.at(-1)); // Some(30)
pipe([10, 20, 30], Arr.at(5));  // None

chunkBy: <A, K>(keyFn) => (data) => readonly readonly A[][]

Groups consecutive elements that share the same key returned by keyFn.

A

K

(a) => K

(data) => readonly readonly A[][]

pipe(
  [1, 1, 2, 3, 3, 1],
  Arr.chunkBy((n) => n)
); // [[1, 1], [2], [3, 3], [1]]

chunksOf: (n) => <A>(data) => readonly readonly A[][]

Splits an array into chunks of the given size.

number

<A>(data) => readonly readonly A[][]

pipe([1, 2, 3, 4, 5], Arr.chunksOf(2)); // [[1, 2], [3, 4], [5]]

compact: <A>(data) => readonly A[]

Narrows a list of Maybe values down to a list of their underlying values, discarding all None instances.

A

readonly Maybe<A>[]

readonly A[]

Arr.compact([Maybe.make.some(1), Maybe.make.none(), Maybe.make.some(3)]); // [1, 3]

concat: <A>(other) => (data) => readonly A[]

Concatenates a standard array with another array.

A

readonly A[]

(data) => readonly A[]

pipe([1, 2], Arr.concat([3, 4])); // [1, 2, 3, 4]

dedupeAdjacent: <A>(eq) => (data) => readonly A[]

Removes consecutive duplicate elements. An optional Equality<A> can be provided (defaults to Object.is).

A

Equality<A> = ...

(data) => readonly A[]

Arr.dedupeAdjacent()([1, 1, 2, 2, 1, 3]); // [1, 2, 1, 3]

drop: (n) => <A>(data) => readonly A[]

Drops the first n elements from an array.

number

<A>(data) => readonly A[]

pipe([1, 2, 3, 4], Arr.drop(2)); // [3, 4]

dropWhile: <A>(predicate) => (data) => readonly A[]

Drops elements from the start while the predicate holds.

A

(a) => boolean

(data) => readonly A[]

pipe([1, 2, 3, 1], Arr.dropWhile(n => n < 3)); // [3, 1]

every: <A>(predicate) => (data) => boolean

Returns true if all elements satisfy the predicate.

A

(a) => boolean

(data) => boolean

pipe([1, 2, 3], Arr.every(n => n > 0)); // true

filter: <A>(predicate) => (data) => readonly A[]

Filters elements that satisfy the predicate.

A

(a) => boolean

(data) => readonly A[]

pipe([1, 2, 3, 4], Arr.filter(n => n % 2 === 0)); // [2, 4]

filterMap: <A, B>(f) => (data) => readonly B[]

Maps each element to a Maybe and collects only the Some values. Combines map and filter in a single pass.

A

B

(a) => Maybe<B>

(data) => readonly B[]

const parseNum = (s: string): Maybe<number> => {
  const n = Number(s);
  return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
};

pipe(["1", "abc", "3"], Arr.filterMap(parseNum)); // [1, 3]

findFirst: <A>(predicate) => (data) => Maybe<A>

Returns the first element matching the predicate, or None.

A

(a) => boolean

(data) => Maybe<A>

pipe([1, 2, 3, 4], Arr.findFirst(n => n > 2)); // Some(3)

findIndex: <A>(predicate) => (data) => Maybe<number>

Returns the index of the first element matching the predicate, or None.

A

(a) => boolean

(data) => Maybe<number>

pipe([1, 2, 3, 4], Arr.findIndex(n => n > 2)); // Some(2)

findLast: <A>(predicate) => (data) => Maybe<A>

Returns the last element matching the predicate, or None.

A

(a) => boolean

(data) => Maybe<A>

pipe([1, 2, 3, 4], Arr.findLast(n => n > 2)); // Some(4)

findMap: <A, B>(f) => (data) => Maybe<B>

Finds the first element in an array for which f returns Some(b).

A

B

(a) => Maybe<B>

(data) => Maybe<B>

pipe(
  ["1", "a", "2"],
  Arr.findMap((s) => isNaN(Number(s)) ? Maybe.make.none() : Maybe.make.some(Number(s)))
); // Some(1)

flatMap: <A, B>(f) => (data) => readonly B[]

Maps each element to an array and flattens the result.

A

B

(a) => readonly B[]

(data) => readonly B[]

pipe([1, 2, 3], Arr.flatMap(n => [n, n * 10])); // [1, 10, 2, 20, 3, 30]

flatten: <A>(data) => readonly A[]

Flattens a nested array by one level.

A

readonly readonly A[][]

readonly A[]

Arr.flatten([[1, 2], [3], [4, 5]]); // [1, 2, 3, 4, 5]

frequencies: <A>(data) => ReadonlyMap<A, number>

Counts occurrences of each element in an array, returning a ReadonlyMap<A, number>.

A

readonly A[]

ReadonlyMap<A, number>

Arr.frequencies(["a", "b", "a", "c", "b", "a"]);
// ReadonlyMap { "a" => 3, "b" => 2, "c" => 1 }

from: object = ArrFrom

Array: <A>(data) => Maybe<NonEmptyArr<A>>

A

readonly A[]

Maybe<NonEmptyArr<A>>

groupBy: <A>(f) => (data) => Record<string, NonEmptyArr<A>>

Groups elements by a key function.

A

(a) => string

(data) => Record<string, NonEmptyArr<A>>

pipe(
  ["apple", "avocado", "banana"],
  Arr.groupBy(s => s[0])
); // { a: ["apple", "avocado"], b: ["banana"] }

head: <A>(data) => Maybe<A>

Returns the first element of an array, or None if the array is empty.

A

readonly A[]

Maybe<A>

Arr.head([1, 2, 3]); // Some(1)
Arr.head([]); // None

indexBy: <A, K>(keyFn) => (data) => ReadonlyMap<K, A>

Indexes elements of an array into a ReadonlyMap<K, A> using a key extraction function.

A

K

(a) => K

(data) => ReadonlyMap<K, A>

pipe(
  [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }],
  Arr.indexBy((u) => u.id)
); // ReadonlyMap { 1 => { id: 1, name: "Alice" }, 2 => { id: 2, name: "Bob" } }

init: <A>(data) => Maybe<readonly A[]>

Returns all elements except the last, or None if the array is empty.

A

readonly A[]

Maybe<readonly A[]>

Arr.init([1, 2, 3]); // Some([1, 2])
Arr.init([]); // None

insertAt: <A>(index, item) => (data) => readonly A[]

Returns a new array with item inserted before the element at index. Negative indices are clamped to 0; indices beyond the array length append to the end.

A

number

A

(data) => readonly A[]

pipe([1, 2, 3], Arr.insertAt(1, 99)); // [1, 99, 2, 3]
pipe([1, 2, 3], Arr.insertAt(0, 99)); // [99, 1, 2, 3]
pipe([1, 2, 3], Arr.insertAt(3, 99)); // [1, 2, 3, 99]

intersperse: <A>(sep) => (data) => readonly A[]

Inserts a separator between every element.

A

A

(data) => readonly A[]

pipe([1, 2, 3], Arr.intersperse(0)); // [1, 0, 2, 0, 3]

is: object = ArrIs

empty: <A>(data) => data is readonly []

A

readonly A[]

data is readonly []

nonEmpty: <A>(data) => data is NonEmptyArr<A>

A

readonly A[]

data is NonEmptyArr<A>

last: <A>(data) => Maybe<A>

Returns the last element of an array, or None if the array is empty.

A

readonly A[]

Maybe<A>

Arr.last([1, 2, 3]); // Some(3)
Arr.last([]); // None

map: <A, B>(f) => (data) => readonly B[]

Transforms each element of an array.

A

B

(a) => B

(data) => readonly B[]

pipe([1, 2, 3], Arr.map(n => n * 2)); // [2, 4, 6]

mapWithIndex: <A, B>(f) => (data) => readonly B[]

Transforms each element using both its value and its zero-based index.

A

B

(i, a) => B

(data) => readonly B[]

pipe(
  ["a", "b", "c"],
  Arr.mapWithIndex((i, s) => ({ position: i + 1, value: s }))
); // [{ position: 1, value: "a" }, { position: 2, value: "b" }, { position: 3, value: "c" }]

NonEmpty: object = ArrNonEmpty

concat: <A>(other) => (data) => NonEmptyArr<A>

A

readonly A[]

(data) => NonEmptyArr<A>

from: object

Array: <A>(data) => Maybe<NonEmptyArr<A>>

A

readonly A[]

Maybe<NonEmptyArr<A>>

head: <A>(data) => A

A

NonEmptyArr<A>

A

intersperse: <A>(sep) => (data) => NonEmptyArr<A>

A

A

(data) => NonEmptyArr<A>

last: <A>(data) => A

A

NonEmptyArr<A>

A

map: <A, B>(f) => (data) => NonEmptyArr<B>

A

B

(a) => B

(data) => NonEmptyArr<B>

mapWithIndex: <A, B>(f) => (data) => NonEmptyArr<B>

A

B

(i, a) => B

(data) => NonEmptyArr<B>

reduce: <A>(f) => (data) => A

A

(acc, a) => A

(data) => A

reverse: <A>(data) => NonEmptyArr<A>

A

NonEmptyArr<A>

NonEmptyArr<A>

singleton: <A>(value) => NonEmptyArr<A>

A

A

NonEmptyArr<A>

tail: <A>(data) => readonly A[]

A

NonEmptyArr<A>

readonly A[]

partition: <A>(predicate) => (data) => readonly [readonly A[], readonly A[]]

Splits an array into two groups based on a predicate. First group contains elements that satisfy the predicate, second group contains the rest.

A

(a) => boolean

(data) => readonly [readonly A[], readonly A[]]

pipe([1, 2, 3, 4], Arr.partition(n => n % 2 === 0)); // [[2, 4], [1, 3]]

partitionMap: <A, E, B>(f) => (data) => readonly [readonly E[], readonly B[]]

Maps each element to a Result, and separates the results into a tuple of failures and successes.

A

E

B

(a) => Result<E, B>

(data) => readonly [readonly E[], readonly B[]]

pipe(
  [1, 2, 3, 4],
  Arr.partitionMap(n => n % 2 === 0 ? Result.make.ok(n) : Result.make.err(`odd: ${n}`))
); // [["odd: 1", "odd: 3"], [2, 4]]

partitionMaybe: <A, B>(f) => (data) => readonly [readonly A[], readonly B[]]

Partitions an array by applying a function returning Maybe<B>. Elements returning None are gathered into failures (original A values); elements returning Some(b) are gathered into successes (B values).

A

B

(a) => Maybe<B>

(data) => readonly [readonly A[], readonly B[]]

const parseNumber = (s: string) => isNaN(Number(s)) ? Maybe.make.none() : Maybe.make.some(Number(s));
pipe(["1", "abc", "3"], Arr.partitionMaybe(parseNumber)); // [["abc"], [1, 3]]

prepend: <A>(value) => (data) => NonEmptyArr<A>

Prepends a value to the beginning of an array, returning a NonEmptyArr.

A

A

(data) => NonEmptyArr<A>

pipe([1, 2], Arr.prepend(0)); // [0, 1, 2]

reduce: <A, B>(initial, f) => (data) => B

Reduces an array from the left.

A

B

B

(acc, a) => B

(data) => B

pipe([1, 2, 3], Arr.reduce(0, (acc, n) => acc + n)); // 6

removeAt: (index) => <A>(data) => readonly A[]

Returns a new array with the element at index removed. Returns the original array unchanged if index is out of bounds.

number

<A>(data) => readonly A[]

pipe([1, 2, 3], Arr.removeAt(1)); // [1, 3]
pipe([1, 2, 3], Arr.removeAt(0)); // [2, 3]
pipe([1, 2, 3], Arr.removeAt(5)); // [1, 2, 3]

reverse: <A>(data) => readonly A[]

Reverses an array. Returns a new array.

A

readonly A[]

readonly A[]

Arr.reverse([1, 2, 3]); // [3, 2, 1]

scan: <A, B>(initial, f) => (data) => readonly B[]

Like reduce, but returns every intermediate accumulator as an array. The initial value is not included — the output has the same length as the input.

A

B

B

(acc, a) => B

(data) => readonly B[]

pipe([1, 2, 3], Arr.scan(0, (acc, n) => acc + n)); // [1, 3, 6]

separate: <E, A>(data) => readonly [readonly E[], readonly A[]]

Separates an array of Result values into two separate lists of errors and successes. Returns a tuple containing [errors, successes].

E

A

readonly Result<E, A>[]

readonly [readonly E[], readonly A[]]

Arr.separate([Result.make.ok(1), Result.make.err("bad"), Result.make.ok(3)]); // [["bad"], [1, 3]]

sequence: object

Maybe: <A>(data) => Maybe<readonly A[]> = ArrMaybe.sequence

Collects an array of Maybe instances into a Maybe of array. Returns None if any element is None.

A

readonly Maybe<A>[]

Maybe<readonly A[]>

Arr.sequence.Maybe([Maybe.make.some(1), Maybe.make.some(2)]); // Some([1, 2])
Arr.sequence.Maybe([Maybe.make.some(1), Maybe.make.none()]); // None

Result: <E, A>(data) => Result<E, readonly A[]> = ArrResult.sequence

Collects an array of Results into a Result of array. Returns the first Err if any element is Err.

E

A

readonly Result<E, A>[]

Result<E, readonly A[]>

Arr.sequence.Result([Result.make.ok(1), Result.make.ok(2)]); // Ok([1, 2])
Arr.sequence.Result([Result.make.ok(1), Result.make.err("bad")]); // Err("bad")

Task: TaskSequence = _sequenceTask

size: <A>(data) => number

Returns the length of an array.

A

readonly A[]

number

Arr.size([1, 2, 3]); // 3

some: <A>(predicate) => (data) => boolean

Returns true if any element satisfies the predicate.

A

(a) => boolean

(data) => boolean

pipe([1, 2, 3], Arr.some(n => n > 2)); // true

sortBy: <A>(compare) => (data) => readonly A[]

Sorts an array using a comparison function. Returns a new array. To sort with a typed Ordering<A>, prefer Arr.sortWith.

A

(a, b) => number

(data) => readonly A[]

pipe([3, 1, 2], Arr.sortBy((a, b) => a - b)); // [1, 2, 3]

sortWith: <A>(ord) => (data) => readonly A[]

Sorts an array using an Ordering<A>. Returns a new array without mutating the original. Use this over sortBy when you have a typed Ordering<A> from the Ordering module.

A

Ordering<A>

(data) => readonly A[]

pipe([3, 1, 2], Arr.sortWith(Ordering.number)); // [1, 2, 3]

type Product = { price: number };
const products: Product[] = [{ price: 20 }, { price: 10 }];
const byPrice = pipe(Ordering.number, Ordering.by((p: Product) => p.price));
pipe(products, Arr.sortWith(byPrice));

splitAt: (index) => <A>(data) => readonly [readonly A[], readonly A[]]

Splits an array at an index into a [before, after] tuple. Negative indices clamp to 0; indices beyond the array length clamp to the end.

number

<A>(data) => readonly [readonly A[], readonly A[]]

pipe([1, 2, 3, 4], Arr.splitAt(2)); // [[1, 2], [3, 4]]
pipe([1, 2, 3], Arr.splitAt(0));    // [[], [1, 2, 3]]
pipe([1, 2, 3], Arr.splitAt(10));   // [[1, 2, 3], []]

tail: <A>(data) => Maybe<readonly A[]>

Returns all elements except the first, or None if the array is empty.

A

readonly A[]

Maybe<readonly A[]>

Arr.tail([1, 2, 3]); // Some([2, 3])
Arr.tail([]); // None

take: (n) => <A>(data) => readonly A[]

Takes the first n elements from an array.

number

<A>(data) => readonly A[]

pipe([1, 2, 3, 4], Arr.take(2)); // [1, 2]

takeWhile: <A>(predicate) => (data) => readonly A[]

Takes elements from the start while the predicate holds.

A

(a) => boolean

(data) => readonly A[]

pipe([1, 2, 3, 1], Arr.takeWhile(n => n < 3)); // [1, 2]

traverse: object

Maybe: <A, B>(f) => (data) => Maybe<readonly B[]> = ArrMaybe.traverse

Maps each element to a Maybe and collects the results. Returns None if any mapping returns None.

A

B

(a) => Maybe<B>

(data) => Maybe<readonly B[]>

const parseNum = (s: string): Maybe<number> => {
  const n = Number(s);
  return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
};

pipe(["1", "2", "3"], Arr.traverse.Maybe(parseNum)); // Some([1, 2, 3])
pipe(["1", "x", "3"], Arr.traverse.Maybe(parseNum)); // None

Result: <E, A, B>(f) => (data) => Result<E, readonly B[]> = ArrResult.traverse

Maps each element to a Result and collects the results. Returns the first Err if any mapping fails.

E

A

B

(a) => Result<E, B>

(data) => Result<E, readonly B[]>

pipe(
  [1, 2, 3],
  Arr.traverse.Result((n: number) => n > 0 ? Result.make.ok(n) : Result.make.err("negative"))
); // Ok([1, 2, 3])

Task: TaskTraverse = _traverseTask

unfold: <A, S>(initial, f) => readonly A[]

Generates an array from an initial seed state until f returns None.

A

S

S

(state) => Maybe<readonly [A, S]>

readonly A[]

Arr.unfold(1, (n) => n > 3 ? Maybe.make.none() : Maybe.make.some([n, n + 1]));
// [1, 2, 3]

uniq: <A>(data) => readonly A[]

Removes duplicate elements using strict equality.

A

readonly A[]

readonly A[]

Arr.uniq([1, 2, 2, 3, 1]); // [1, 2, 3]

uniqBy: <A, B>(f) => (data) => readonly A[]

Removes duplicate elements by comparing the result of a key function.

A

B

(a) => B

(data) => readonly A[]

pipe(
  [{id: 1, name: "a"}, {id: 1, name: "b"}, {id: 2, name: "c"}],
  Arr.uniqBy(x => x.id)
); // [{id: 1, name: "a"}, {id: 2, name: "c"}]

uniqWith: <A>(eq) => (data) => readonly A[]

Removes duplicate elements using a custom equality check. Preserves the order of first occurrences. Complements uniq (reference equality) and uniqBy (key extraction).

A

Equality<A>

(data) => readonly A[]

type Point = { x: number; y: number };
const eqPoint: Equality<Point> = (a, b) => a.x === b.x && a.y === b.y;

pipe(
  [{ x: 1, y: 1 }, { x: 2, y: 2 }, { x: 1, y: 1 }],
  Arr.uniqWith(eqPoint),
); // [{ x: 1, y: 1 }, { x: 2, y: 2 }]

windowed: (windowSize, options?) => <A>(data) => readonly readonly A[][]

Produces a sliding window of size elements over an array, advancing by step (default 1). Returns an empty array if size <= 0 or size > data.length.

number

number

<A>(data) => readonly readonly A[][]

pipe([1, 2, 3, 4], Arr.windowed(2)); // [[1, 2], [2, 3], [3, 4]]
pipe([1, 2, 3, 4], Arr.windowed(2, { step: 2 })); // [[1, 2], [3, 4]]

zip: <B>(other) => <A>(data) => readonly readonly [A, B][]

Pairs up elements from two arrays. Stops at the shorter array.

B

readonly B[]

<A>(data) => readonly readonly [A, B][]

pipe([1, 2, 3], Arr.zip(["a", "b"])); // [[1, "a"], [2, "b"]]

zipWith: <A, B, C>(f) => (other) => (data) => readonly C[]

Combines elements from two arrays using a function. Stops at the shorter array.

A

B

C

(a, b) => C

(other) => (data) => readonly C[]

pipe([1, 2], Arr.zipWith((a: number, b: string) => `${a}${b}`)(["a", "b"])); // ["1a", "2b"]