preludejs/List API
Complete API reference for preludejs/List
preludejs/List API Reference
Core
all ƒ
Returns true only when every item satisfies the predicate; curried.
function all<A>(fn: Predicate<A>): (xs: readonly A[]) => boolean
function all<A>(fn: Predicate<A>, xs: readonly A[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
all((x: number) => x > 0, [1, 2, 3]) //=> trueand ƒ
Returns true when every item in the list is truthy.
function and(xs: readonly unknown[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly unknown[] |
Example
and([1, 2, 3]) //=> trueany ƒ
Returns true when at least one item satisfies the predicate; curried.
function any<A>(fn: Predicate<A>): (xs: readonly A[]) => boolean
function any<A>(fn: Predicate<A>, xs: readonly A[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
any((x: number) => x > 2, [1, 2, 3]) //=> trueat ƒ
Returns the item at the given index; curried.
function at<A>(index: number): (xs: readonly A[]) => A | undefined
function at<A>(index: number, xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
index | number |
Example
at(1, ['a', 'b', 'c']) //=> 'b'breakList ƒ
Splits the list at the first item that satisfies the predicate; curried.
function breakList<A>(fn: Predicate<A>): (xs: readonly A[]) => [A[], A[]]
function breakList<A>(fn: Predicate<A>, xs: readonly A[]): [A[], A[]]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
breakList((x: number) => x > 2, [1, 2, 3, 4]) //=> [[1, 2], [3, 4]]compact ƒ
Removes falsy values from the list.
function compact<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
compact([0, 1, false, 2]) //=> [1, 2]concat ƒ
Concatenates a list of lists into a single list.
function concat<A>(xss: readonly (readonly A[])[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xss | readonly (readonly A[])[] |
Example
concat([[1, 2], [3], [4, 5]]) //=> [1, 2, 3, 4, 5]concatMap ƒ
Maps a list to lists and concatenates the results; curried.
function concatMap<A, B>(fn: Mapper<A, readonly B[]>): (xs: readonly A[]) => B[]
function concatMap<A, B>(fn: Mapper<A, readonly B[]>, xs: readonly A[]): B[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Mapper<A, readonly B[]> |
Example
concatMap((x: number) => [x, x], [1, 2, 3]) //=> [1, 1, 2, 2, 3, 3]countBy ƒ
Counts items grouped by the key the accessor returns; curried.
function countBy<A, B>(fn: Accessor<A, B>): (xs: readonly A[]) => Record<string, number>
function countBy<A, B>(fn: Accessor<A, B>, xs: readonly A[]): Record<string, number>Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, B> |
Example
countBy(Math.floor, [4.2, 4.4, 9.8]) //=> { 4: 2, 9: 1 }difference ƒ
Returns items present in the first list but absent from the rest.
function difference<A>(xs: readonly A[], ...yss: readonly (readonly A[])[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] | |
yss | readonly (readonly A[])[] |
Example
difference([1, 2, 3, 4], [1], [4]) //=> [2, 3]drop ƒ
Drops the first n items from the list; curried.
function drop<A>(count: number): (xs: readonly A[]) => A[]
function drop<A>(count: number, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
count | number |
Example
drop(2, [1, 2, 3, 4]) //=> [3, 4]dropWhile ƒ
Drops leading items while the predicate holds; curried.
function dropWhile<A>(fn: Predicate<A>): (xs: readonly A[]) => A[]
function dropWhile<A>(fn: Predicate<A>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
dropWhile((n: number) => n <= 3, [2, 3, 5, 1]) //=> [5, 1]each ƒ
Applies a function to each item for its side effect; curried.
function each<A>(fn: (value: A, index: number, array: readonly A[]) => unknown): (xs: readonly A[]) => void
function each<A>(fn: (value: A, index: number, array: readonly A[]) => unknown, xs: readonly A[]): voidParameters
| Parameter | Type | Description |
|---|---|---|
fn | (value: A, index: number, array: readonly A[]) => unknown |
Example
each((x: number) => console.log(x), [1, 2, 3]) //=> undefinedelem ƒ
Returns true when the value is present in the list; curried.
function elem<A>(value: A): (xs: readonly A[]) => boolean
function elem<A>(value: A, xs: readonly A[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
value | A |
Example
elem(3, [1, 2, 3]) //=> trueelemIndex ƒ
Returns the index of the first item equal to the value, or -1; curried.
function elemIndex<A>(value: A): (xs: readonly A[]) => number
function elemIndex<A>(value: A, xs: readonly A[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
value | A |
Example
elemIndex(3, [1, 2, 3]) //=> 2elemIndices ƒ
Returns the indices of all items equal to the value; curried.
function elemIndices<A>(value: A): (xs: readonly A[]) => number[]
function elemIndices<A>(value: A, xs: readonly A[]): number[]Parameters
| Parameter | Type | Description |
|---|---|---|
value | A |
Example
elemIndices(2, [2, 1, 2]) //=> [0, 2]empty ƒ
Returns true when the list has no items.
function empty<A>(xs: readonly A[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
empty([]) //=> truefilter ƒ
Keeps the items that satisfy the predicate; curried.
function filter<A>(fn: Predicate<A>): (xs: readonly A[]) => A[]
function filter<A>(fn: Predicate<A>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
filter((x: number) => x > 3, [1, 2, 3, 4, 5]) //=> [4, 5]find ƒ
Returns the first item that satisfies the predicate, or undefined; curried.
function find<A>(fn: Predicate<A>): (xs: readonly A[]) => A | undefined
function find<A>(fn: Predicate<A>, xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
find((x: number) => x > 2, [1, 2, 3, 4]) //=> 3findIndex ƒ
Returns the index of the first item that satisfies the predicate, or -1; curried.
function findIndex<A>(fn: Predicate<A>): (xs: readonly A[]) => number
function findIndex<A>(fn: Predicate<A>, xs: readonly A[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
findIndex((x: number) => x > 2, [1, 2, 3]) //=> 2findIndices ƒ
Returns the indices of all items that satisfy the predicate; curried.
function findIndices<A>(fn: Predicate<A>): (xs: readonly A[]) => number[]
function findIndices<A>(fn: Predicate<A>, xs: readonly A[]): number[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
findIndices((x: number) => x > 1, [1, 2, 3]) //=> [1, 2]first ƒ
Returns the first item of the list, or undefined. Alias of head.
function first<A>(xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
first([1, 2, 3]) //=> 1flatten ƒ
function flatten<A>(xss: readonly Nested<A>[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xss | readonly Nested<A>[] |
fold ƒ
Left-associative fold over a list with an initial accumulator. Alias of foldl.
function fold<A, B>(fn: Reducer<A, B>): (initial: A) => (xs: readonly B[]) => A
function fold<A, B>(fn: Reducer<A, B>, initial: A): (xs: readonly B[]) => A
function fold<A, B>(fn: Reducer<A, B>, initial: A, xs: readonly B[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | Reducer<A, B> |
Example
fold((acc: number, x: number) => acc + x, 0, [1, 2, 3]) //=> 6fold1 ƒ
Left fold over a non-empty list using its head as the seed. Alias of foldl1.
function fold1<A>(fn: (acc: A, value: A) => A): (xs: readonly A[]) => A
function fold1<A>(fn: (acc: A, value: A) => A, xs: readonly A[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | (acc: A, value: A) => A |
Example
fold1((acc: number, x: number) => acc + x, [1, 2, 3]) //=> 6foldl ƒ
Left-associative fold over a list with an initial accumulator; curried.
function foldl<A, B>(fn: Reducer<A, B>): (initial: A) => (xs: readonly B[]) => A
function foldl<A, B>(fn: Reducer<A, B>, initial: A): (xs: readonly B[]) => A
function foldl<A, B>(fn: Reducer<A, B>, initial: A, xs: readonly B[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | Reducer<A, B> |
Example
foldl((acc: number, x: number) => acc + x, 0, [1, 2, 3]) //=> 6foldl1 ƒ
Left fold over a non-empty list using its head as the seed; curried.
function foldl1<A>(fn: (acc: A, value: A) => A): (xs: readonly A[]) => A
function foldl1<A>(fn: (acc: A, value: A) => A, xs: readonly A[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | (acc: A, value: A) => A |
Example
foldl1((acc: number, x: number) => acc + x, [1, 2, 3]) //=> 6foldr ƒ
Right-associative fold over a list with an initial accumulator; curried.
function foldr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A): (initial: A) => (xs: readonly B[]) => A
function foldr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A, initial: A): (xs: readonly B[]) => A
function foldr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A, initial: A, xs: readonly B[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | (value: B, acc: A, index: number, array: readonly B[]) => A |
Example
foldr((x: string, acc: string) => x + acc, 'o', ['h', 'e', 'l', 'l']) //=> 'hello'foldr1 ƒ
Right fold over a non-empty list using its last item as the seed; curried.
function foldr1<A>(fn: (value: A, acc: A) => A): (xs: readonly A[]) => A
function foldr1<A>(fn: (value: A, acc: A) => A, xs: readonly A[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | (value: A, acc: A) => A |
Example
foldr1((x: number, y: number) => x - y, [1, 2, 3, 4, 9]) //=> 7groupBy ƒ
Groups items into lists keyed by the value the function returns; curried.
function groupBy<A>(fn: Accessor<A, boolean | PropertyKey>): (xs: readonly A[]) => Record<string, A[]>
function groupBy<A>(fn: Accessor<A, boolean | PropertyKey>, xs: readonly A[]): Record<string, A[]>Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, boolean | PropertyKey> |
Example
groupBy(Math.floor, [4.2, 4.4, 9.8]) //=> { 4: [4.2, 4.4], 9: [9.8] }head ƒ
Returns the first item of the list, or undefined.
function head<A>(xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
head([1, 2, 3]) //=> 1initial ƒ
Returns all items but the last, or undefined for an empty list.
function initial<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
initial([1, 2, 3, 4]) //=> [1, 2, 3]intersection ƒ
Returns items present in the first list and every other list.
function intersection<A>(xs: readonly A[], ...yss: readonly (readonly A[])[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] | |
yss | readonly (readonly A[])[] |
Example
intersection([1, 2, 3], [2, 3, 4]) //=> [2, 3]last ƒ
Returns the last item of the list, or undefined.
function last<A>(xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
last([1, 2, 3]) //=> 3length ƒ
Returns the number of items in the list.
function length<A>(xs: readonly A[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
length([1, 2, 3]) //=> 3map ƒ
Applies a function to each item, returning a new list; curried.
function map<A, B>(fn: Mapper<A, B>): (xs: readonly A[]) => B[]
function map<A, B>(fn: Mapper<A, B>, xs: readonly A[]): B[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Mapper<A, B> |
Example
map((x: number) => x + 1, [1, 2, 3]) //=> [2, 3, 4]maximum ƒ
Returns the largest item in the list, or undefined when empty.
function maximum<A>(xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
maximum([1, 2, 3, 4, 5]) //=> 5maximumBy ƒ
Returns the item with the largest value produced by the accessor; curried.
function maximumBy<A, B>(fn: Accessor<A, B>): (xs: readonly A[]) => A | undefined
function maximumBy<A, B>(fn: Accessor<A, B>, xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, B> |
Example
maximumBy((s: string) => s.length, ['a', 'ccc', 'bb']) //=> 'ccc'mean ƒ
Returns the arithmetic mean of a list of numbers.
function mean(xs: readonly number[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly number[] |
Example
mean([1, 2, 3, 4, 5]) //=> 3minimum ƒ
Returns the smallest item in the list, or undefined when empty.
function minimum<A>(xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
minimum([3, 1, 2]) //=> 1minimumBy ƒ
Returns the item with the smallest value produced by the accessor; curried.
function minimumBy<A, B>(fn: Accessor<A, B>): (xs: readonly A[]) => A | undefined
function minimumBy<A, B>(fn: Accessor<A, B>, xs: readonly A[]): A | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, B> |
Example
minimumBy((s: string) => s.length, ['was', 'a', 'test']) //=> 'a'notElem ƒ
Returns true when the value is absent from the list; curried.
function notElem<A>(value: A): (xs: readonly A[]) => boolean
function notElem<A>(value: A, xs: readonly A[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
value | A |
Example
notElem(4, [1, 2, 3]) //=> trueor ƒ
Returns true when at least one item in the list is truthy.
function or(xs: readonly unknown[]): booleanParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly unknown[] |
Example
or([0, '', 3]) //=> truepartition ƒ
Splits a list into items that pass and fail the predicate; curried.
function partition<A>(fn: Predicate<A>): (xs: readonly A[]) => [A[], A[]]
function partition<A>(fn: Predicate<A>, xs: readonly A[]): [A[], A[]]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
partition((x: number) => x > 2, [1, 2, 3, 4]) //=> [[3, 4], [1, 2]]product ƒ
Returns the product of a list of numbers.
function product(xs: readonly number[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly number[] |
Example
product([1, 2, 3]) //=> 6range ƒ
Builds an inclusive list of numbers from from to to stepping by step.
function range(to: number, from?: number, step?: number): number[]Parameters
| Parameter | Type | Description |
|---|---|---|
to | number | |
from | number | |
step | number |
Example
range(3) //=> [1, 2, 3]reduce ƒ
Left-reduces a list with an initial accumulator; curried.
function reduce<A, B>(fn: Reducer<A, B>): (initial: A) => (xs: readonly B[]) => A
function reduce<A, B>(fn: Reducer<A, B>, initial: A): (xs: readonly B[]) => A
function reduce<A, B>(fn: Reducer<A, B>, initial: A, xs: readonly B[]): AParameters
| Parameter | Type | Description |
|---|---|---|
fn | Reducer<A, B> |
Example
reduce((acc: number, x: number) => acc + x, 0, [1, 2, 3]) //=> 6reject ƒ
Keeps the items that do not satisfy the predicate; curried.
function reject<A>(fn: Predicate<A>): (xs: readonly A[]) => A[]
function reject<A>(fn: Predicate<A>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
reject((x: number) => x > 2, [1, 2, 3, 4]) //=> [1, 2]reverse ƒ
Returns a new list with the items in reverse order.
function reverse<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
reverse([1, 2, 3]) //=> [3, 2, 1]scan ƒ
Like reduce but returns the list of successive accumulator values; curried.
function scan<A, B>(fn: Reducer<A, B>): (initial: A) => (xs: readonly B[]) => A[]
function scan<A, B>(fn: Reducer<A, B>, initial: A): (xs: readonly B[]) => A[]
function scan<A, B>(fn: Reducer<A, B>, initial: A, xs: readonly B[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Reducer<A, B> |
Example
scan((a: number, b: number) => a + b, 0, [1, 2, 3]) //=> [0, 1, 3, 6]scan1 ƒ
Like scan but seeds with the list head; curried.
function scan1<A>(fn: (acc: A, value: A) => A): (xs: readonly A[]) => A[] | undefined
function scan1<A>(fn: (acc: A, value: A) => A, xs: readonly A[]): A[] | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
fn | (acc: A, value: A) => A |
Example
scan1((a: number, b: number) => a + b, [1, 2, 3]) //=> [1, 3, 6]scanl ƒ
Left scan over a list with an initial accumulator. Alias of scan.
function scanl<A, B>(fn: Reducer<A, B>): (initial: A) => (xs: readonly B[]) => A[]
function scanl<A, B>(fn: Reducer<A, B>, initial: A): (xs: readonly B[]) => A[]
function scanl<A, B>(fn: Reducer<A, B>, initial: A, xs: readonly B[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Reducer<A, B> |
Example
scanl((a: number, b: number) => a + b, 0, [1, 2, 3]) //=> [0, 1, 3, 6]scanl1 ƒ
Left scan over a list seeded with its head. Alias of scan1.
function scanl1<A>(fn: (acc: A, value: A) => A): (xs: readonly A[]) => A[] | undefined
function scanl1<A>(fn: (acc: A, value: A) => A, xs: readonly A[]): A[] | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
fn | (acc: A, value: A) => A |
Example
scanl1((a: number, b: number) => a + b, [1, 2, 3]) //=> [1, 3, 6]scanr ƒ
Right scan over a list with an initial accumulator; curried.
function scanr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A): (initial: A) => (xs: readonly B[]) => A[]
function scanr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A, initial: A): (xs: readonly B[]) => A[]
function scanr<A, B>(fn: (value: B, acc: A, index: number, array: readonly B[]) => A, initial: A, xs: readonly B[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (value: B, acc: A, index: number, array: readonly B[]) => A |
Example
scanr((a: number, b: number) => a + b, 0, [1, 2, 3]) //=> [6, 5, 3, 0]scanr1 ƒ
Right scan over a list seeded with its last item; curried.
function scanr1<A>(fn: (value: A, acc: A) => A): (xs: readonly A[]) => A[]
function scanr1<A>(fn: (value: A, acc: A) => A, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (value: A, acc: A) => A |
Example
scanr1((a: number, b: number) => a + b, [1, 2, 3]) //=> [6, 5, 3]slice ƒ
Returns the sublist between start and end; curried.
function slice<A>(start: number): (end: number) => (xs: readonly A[]) => A[]
function slice<A>(start: number, end: number): (xs: readonly A[]) => A[]
function slice<A>(start: number, end: number, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
start | number |
Example
slice(1, 3, [1, 2, 3, 4]) //=> [2, 3]sort ƒ
Returns a new list sorted with default comparison.
function sort<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
sort([1, 3, 2]) //=> [1, 2, 3]sortBy ƒ
Returns a new list sorted by the value the accessor returns; curried.
function sortBy<A, B>(fn: Accessor<A, B>): (xs: readonly A[]) => A[]
function sortBy<A, B>(fn: Accessor<A, B>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, B> |
Example
sortBy((s: string) => s.length, ['three', 'one', 'two']) //=> ['one', 'two', 'three']sortWith ƒ
Returns a new list sorted with a custom comparator; curried.
function sortWith<A>(fn: Comparer<A>): (xs: readonly A[]) => A[]
function sortWith<A>(fn: Comparer<A>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Comparer<A> |
Example
sortWith((a: number, b: number) => a - b, [3, 1, 2]) //=> [1, 2, 3]span ƒ
Splits a list into the longest prefix that satisfies the predicate and the rest; curried.
function span<A>(fn: Predicate<A>): (xs: readonly A[]) => [A[], A[]]
function span<A>(fn: Predicate<A>, xs: readonly A[]): [A[], A[]]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
span((x: number) => x < 3, [1, 2, 3, 4]) //=> [[1, 2], [3, 4]]splitAt ƒ
Splits a list into a prefix of length index and the remaining items; curried.
function splitAt<A>(count: number): (xs: readonly A[]) => [A[], A[]]
function splitAt<A>(count: number, xs: readonly A[]): [A[], A[]]Parameters
| Parameter | Type | Description |
|---|---|---|
count | number |
Example
splitAt(2, [1, 2, 3, 4]) //=> [[1, 2], [3, 4]]sum ƒ
Returns the sum of a list of numbers.
function sum(xs: readonly number[]): numberParameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly number[] |
Example
sum([1, 2, 3, 4, 5]) //=> 15tail ƒ
Returns all items but the first.
function tail<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
tail([1, 2, 3]) //=> [2, 3]take ƒ
Takes the first n items from the list; curried.
function take<A>(count: number): (xs: readonly A[]) => A[]
function take<A>(count: number, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
count | number |
Example
take(2, [1, 2, 3, 4]) //=> [1, 2]takeWhile ƒ
Takes leading items while the predicate holds; curried.
function takeWhile<A>(fn: Predicate<A>): (xs: readonly A[]) => A[]
function takeWhile<A>(fn: Predicate<A>, xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Predicate<A> |
Example
takeWhile((n: number) => n <= 3, [2, 3, 5, 1]) //=> [2, 3]unfoldr ƒ
Builds a list by repeatedly applying a generator to a seed; curried.
function unfoldr<A, B>(fn: (seed: B) => [A, B] | undefined): (seed: B) => A[]
function unfoldr<A, B>(fn: (seed: B) => [A, B] | undefined, seed: B): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (seed: B) => [A, B] | undefined |
Example
unfoldr((n: number) => (n > 0 ? [n, n - 1] : undefined), 3) //=> [3, 2, 1]union ƒ
Returns the unique items across all supplied lists.
function union<A>(xs: readonly A[], ...yss: readonly (readonly A[])[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] | |
yss | readonly (readonly A[])[] |
Example
union([1, 2, 3], [3, 4]) //=> [1, 2, 3, 4]unique ƒ
Returns the list with duplicate items removed.
function unique<A>(xs: readonly A[]): A[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] |
Example
unique([1, 2, 2, 3, 1]) //=> [1, 2, 3]uniqueBy ƒ
Returns items unique by the value the accessor returns; curried.
function uniqueBy<A, B>(fn: Accessor<A, B>): (xs: readonly A[]) => B[]
function uniqueBy<A, B>(fn: Accessor<A, B>, xs: readonly A[]): B[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | Accessor<A, B> |
Example
uniqueBy((o: { id: number }) => o.id, [{ id: 1 }, { id: 1 }, { id: 2 }]) //=> [1, 2]zip ƒ
Zips two lists into a list of pairs, truncating to the shorter; curried.
function zip<A, B>(xs: readonly A[], ys: readonly B[]): Pair<A, B>[]
function zip<A>(xs: readonly A[]): <B>(ys: readonly B[]) => Pair<A, B>[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] | |
ys | readonly B[] |
Example
zip([1, 2], [4, 5]) //=> [[1, 4], [2, 5]]zipAll ƒ
Zips two lists into pairs, padding the shorter list with undefined.
function zipAll<A, B>(xs: readonly A[], ys: readonly B[]): Pair<A | undefined, B | undefined>[]Parameters
| Parameter | Type | Description |
|---|---|---|
xs | readonly A[] | |
ys | readonly B[] |
Example
zipAll([1, 2], [4]) //=> [[1, 4], [2, undefined]]zipAllWith ƒ
Combines two lists with a function, padding the shorter with undefined; curried.
function zipAllWith<A, B, C>(fn: (left: A | undefined, right: B | undefined) => C): (xs: readonly A[], ys: readonly B[]) => C[]
function zipAllWith<A, B, C>(fn: (left: A | undefined, right: B | undefined) => C, xs: readonly A[]): (ys: readonly B[]) => C[]
function zipAllWith<A, B, C>(fn: (left: A | undefined, right: B | undefined) => C, xs: readonly A[], ys: readonly B[]): C[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (left: A | undefined, right: B | undefined) => C |
Example
zipAllWith((a, b) => [a, b], [1], [4, 5]) //=> [[1, 4], [undefined, 5]]zipWith ƒ
Combines two lists element-wise with a function, truncating to the shorter; curried.
function zipWith<A, B, C>(fn: (left: A, right: B) => C): (xs: readonly A[], ys: readonly B[]) => C[]
function zipWith<A, B, C>(fn: (left: A, right: B) => C, xs: readonly A[]): (ys: readonly B[]) => C[]
function zipWith<A, B, C>(fn: (left: A, right: B) => C, xs: readonly A[], ys: readonly B[]): C[]Parameters
| Parameter | Type | Description |
|---|---|---|
fn | (left: A, right: B) => C |
Example
zipWith((a: number, b: number) => a + b, [1, 2, 3], [3, 2, 1]) //=> [4, 4, 4]