Array

This module provides a collection of utility functions for working with arrays. It extends functionality from the Effect-TS library.

Array Creation

range(start: number, end: number): Array<number>

Returns an array containing a range of integers, including both endpoints.

range(1, 3) // returns [1, 2, 3] 

replicate<T>(value: T, number: number): Array<T>

Returns an array containing a value repeated the specified number of times.

replicate('a', 3) // returns ['a', 'a', 'a'] 

Array Modification

prepend<T>(array: Array<T>, value: T): Array<T>

Adds an element to the front of an array.

prepend([1, 2], 0) // returns [0, 1, 2] 

prependAll<T>(array: Array<T>, values: Array<T>): Array<T>

Adds multiple elements to the front of an array.

prependAll([3, 4], [1, 2]) // returns [1, 2, 3, 4] 

append<T>(array: Array<T>, value: T): Array<T>

Adds an element to the end of an array.

append([1, 2], 3) // returns [1, 2, 3] 

appendAll<T>(array: Array<T>, values: Array<T>): Array<T>

Adds multiple elements to the end of an array.

appendAll([1, 2], [3, 4]) // returns [1, 2, 3, 4] 

Array Properties

isEmptyArray<T>(array: Array<T>): boolean

Checks if an array is empty.

isEmptyArray([]) // returns true 
isEmptyArray([1]) // returns false 

isNonEmptyArray<T>(array: Array<T>): boolean

Checks if an array is non-empty.

isNonEmptyArray([1]) // returns true 
isNonEmptyArray([]) // returns false 

length<T>(array: Array<T>): number

Gets the length of an array.

length([1, 2, 3]) // returns 3 

Array Access

get<T>(array: Array<T>, index: number): T

Gets the element at a specific index in an array.

get([1, 2, 3], 1) // returns 2 

head<T>(array: Array<T>): T

Gets the first element of an array.

head([1, 2, 3]) // returns 1 

tail<T>(array: Array<T>): Array<T>

Gets the array without the first element.

tail([1, 2, 3]) // returns [2, 3] 

Array Slicing

take<T>(array: Array<T>, n: number): Array<T>

Takes the first n elements of an array.

take([1, 2, 3, 4], 2) // returns [1, 2] 

takeRight<T>(array: Array<T>, n: number): Array<T>

Takes the last n elements of an array.

takeRight([1, 2, 3, 4], 2) // returns [3, 4] 

takeWhile<T>(array: Array<T>, predicate: (a: T) => boolean): Array<T>

Takes elements from an array while a predicate is true.

takeWhile([1, 2, 3, 4], x => x < 3) // returns [1, 2] 

drop<T>(array: Array<T>, n: number): Array<T>

Drops the first n elements of an array.

drop([1, 2, 3, 4], 2) // returns [3, 4] 

dropRight<T>(array: Array<T>, n: number): Array<T>

Drops the last n elements of an array.

dropRight([1, 2, 3, 4], 2) // returns [1, 2] 

dropWhile<T>(array: Array<T>, predicate: (a: T) => boolean): Array<T>

Drops elements from an array while a predicate is true.

dropWhile([1, 2, 3, 4], x => x < 3) // returns [3, 4] 

Array Search

findFirstIndex<T>(array: Array<T>, predicate: (a: T) => boolean): number

Finds the index of the first element that satisfies a predicate.

findFirstIndex([1, 2, 3], x => x > 1) // returns 1 

findLastIndex<T>(array: Array<T>, predicate: (a: T) => boolean): number

Finds the index of the last element that satisfies a predicate.

findLastIndex([1, 2, 3], x => x < 3) // returns 1 

findFirst<T>(array: Array<T>, predicate: (a: T) => boolean): T

Finds the first element that satisfies a predicate.

findFirst([1, 2, 3], x => x > 1) // returns 2 

findLast<T>(array: Array<T>, predicate: (a: T) => boolean): T

Finds the last element that satisfies a predicate.

findLast([1, 2, 3], x => x < 3) // returns 2 

Array Manipulation

insertAt<T>(array: Array<T>, index: number, value: T): Array<T>

Inserts an element at a specific index in an array.

insertAt([1, 3], 1, 2) // returns [1, 2, 3] 

replace<T>(array: Array<T>, index: number, value: T): Array<T>

Replaces an element at a specific index in an array.

replace([1, 2, 3], 1, 4) // returns [1, 4, 3] 

remove<T>(array: Array<T>, index: number): Array<T>

Removes an element at a specific index in an array.

remove([1, 2, 3], 1) // returns [1, 3] 

reverse<T>(array: Array<T>): Array<T>

Reverses the elements of an array.

reverse([1, 2, 3]) // returns [3, 2, 1] 

Array Transformation

map<T, U>(array: Array<T>, f: (a: T) => U): Array<U>

Maps a function over the elements of an array.

map([1, 2, 3], x => x * 2) // returns [2, 4, 6] 

flatMap<T, U>(array: Array<T>, f: (a: T) => Array<U>): Array<U>

Flat maps a function over the elements of an array.

flatMap([1, 2], x => [x, x]) // returns [1, 1, 2, 2] 

flatten<T>(array: Array<Array<T>>): Array<T>

Flattens an array of arrays.

flatten([[1], [2, 3]]) // returns [1, 2, 3] 

Array Operations

zip<T, U>(array: Array<T>, that: Array<U>): Array<[T, U]>

Zips two arrays together.

zip([1, 2], ['a', 'b']) // returns [[1, 'a'], [2, 'b']] 

unzip<T, U>(array: Array<[T, U]>): [Array<T>, Array<U>]

Unzips an array of pairs into two arrays.

unzip([[1, 'a'], [2, 'b']]) // returns [[1, 2], ['a', 'b']] 

dedupe<T>(array: Array<T>): Array<T>

Removes duplicates from an array.

dedupe([1, 1, 2, 2, 3]) // returns [1, 2, 3] 

join(array: Array<any>, separator: string): string

Joins array elements into a string with a separator.

join(['a', 'b', 'c'], ',') // returns "a,b,c"