Checks if the given array is present and it is not empty (contains at least one element).
Questions
- How to check if an array is empty?
- How to check if an array is empty or null or undefined?
- How to check if an array is empty or not?
- How to check if an array is empty or doesn't exist?
TypeScript source • JavaScript source
Checks if the given arguments are all `Arrays`.
Type signature
<T>(...xs: T[]) => boolean
Questions
- How to check if all the given values are arrays?
TypeScript source • JavaScript source
Splits the given array into an array of chunks of up to the given length.
Type signature
(count: number) => <T>(xs: T[]) => T[] | T[][]
Questions
- How to split an array into chunks?
- How to split an array into chunks of the same size?
TypeScript source • JavaScript source
Computes a set difference between the two given arrays.
Type signature
<T>(xs: T[], ys: T[]) => T[]
Examples
difference(
[1, 2, 3, 4, 5, 6],
[2, 4],
);
Try in REPL
Questions
- How to find elements which are present in the first array and not in the second?
TypeScript source • JavaScript source
Checks if two arrays are not equal.
Type signature
<T>(xs?: T[], ys?: T[]) => boolean
Questions
- How to check if two arrays differ?
- How to check if two arrays are not equal?
- How to check if two arrays are equal or not?
TypeScript source • JavaScript source
Lists all the duplicated values in the given array.
Examples
duplicates([
1, 2, 3, 4, 3, 4, 3, 6,
]);
Try in REPL
Questions
- How to find duplicates in an array?
TypeScript source • JavaScript source
Empty array.
Questions
- How to get an empty array?
TypeScript source • JavaScript source
Takes exactly the given count of elements.
Type signature
(count: number) => <T>(xs: T[]) => T[]
Questions
- How to get exactly N elements out of an array?
TypeScript source • JavaScript source
Filters out the given value.
Type signature
<T>(y: T) => (xs: T[]) => T[]
Questions
- How to get all the values of an array except the given one?
TypeScript source • JavaScript source
Filters the given array with the given predicate just like Array.filter but does it in-place thus mutates the original array.
Type signature
<T>(f: (value: T, index: number, context: T[]) => boolean) => (xs: T[]) => T[]
Examples
const xs = [1, 2, 3, 4, 5, 6, 7];
const odd = (x) => x % 2 === 1;
const ys = filterInPlace(odd)(xs);
ys === xs;
ys;
Try in REPL
Questions
- How to filter an array in place?
TypeScript source • JavaScript source
Finds an element by a predicate function within the given array, otherwise, it returns the given fallback value or undefined when fallback is not present.
Type signature
<T>(predicate: (value: T, index: number, context: T[]) => boolean, fallback?: T) => (xs: T[]) => T
Examples
find((x) => x > 2)([1, 2, 3, 5, 7]);
Try in REPL
find((x) => x > 2)([
1, 2, -3, -5, -7,
]);
Try in REPL
Questions
- How to find an element of an array by a given predicate?
TypeScript source • JavaScript source
Returns the first element or undefined when there are no elements in the given array.
Type signature
<T>([x]: T[]) => T | undefined
Questions
- How to get the first element of an array?
TypeScript source • JavaScript source
Maps and flattens the result.
Type signature
<T, TResult>(f: (value: T, index: number, context: T[]) => TResult[]) => (xs: T[]) => TResult[]
Examples
flatMap((text) => [...text])([
"test",
"123",
]);
Try in REPL
Questions
- How to flat map an array?
- How to map and then flatten an array?
TypeScript source • JavaScript source
Flattens the nested arrays by a single level.
Examples
flatten([1, [2, 3], 4, [5, 6]]);
Try in REPL
flatten([
1,
[2, [3, 6]],
4,
[5, 6],
]);
Try in REPL
TypeScript source • JavaScript source
Inserts the given item to the array at a specific index.
Type signature
(index: number) => <T>(item: T) => ([...xs]: T[]) => T[]
Questions
- How to insert an element to an array at a given position?
TypeScript source • JavaScript source
Finds common elements between both arrays.
Type signature
<T>(xs: T[], ys: T[]) => T[]
Examples
intersection(
[1, 2, 3, 4, 5],
[5, 5, 3, 2],
);
Try in REPL
Questions
- How to find common elements present in both arrays?
TypeScript source • JavaScript source
Checks if the given argument is an array.
Type signature
(value?: unknown) => boolean
Questions
- How to check if a value is an array?
TypeScript source • JavaScript source
Returns the last element or undefined when there are no elements in the given array.
Type signature
<T>(xs: T[]) => T | undefined
Questions
- How to get the last element of an array?
TypeScript source • JavaScript source
Returns the number of elements in the given array.
Questions
- How to check an array's length?
- How to compute an array's length?
- How to check the size of an array?
- How to check the number of elements in an array?
TypeScript source • JavaScript source
Checks if lengths of the given arrays differ.
Type signature
<T1, T2>(a: T1[], b: T2[]) => boolean
Questions
- How to check if array lengths differ?
- How to check if the given arrays have different lengths?
TypeScript source • JavaScript source
Maps the given array with the given functions.
Type signature
<T>(...fs: ((x: T) => T)[]) => (xs: T[]) => T[]
TypeScript source • JavaScript source
Returns the middle element or the right one when the number of elements is even.
Type signature
<T>(xs: T[]) => T | undefined
Questions
- How to get the element in the middle of an array?
- How to get the middle element of an array?
TypeScript source • JavaScript source
Computes minimum and maximum values of the given array in a single run.
Type signature
(xs: number[]) => number[]
Examples
minMax([
10, 5, 3, -5, -4, 23, 32, 8, 1, 0,
]);
Try in REPL
Questions
- How to find the minimum and maximum values of an array?
- How to get the min/max element of an array?
TypeScript source • JavaScript source
Checks if the given array contains more than one element.
Questions
- How to check if an array contains multiple elements?
- How to check whether multiple values exist within an array?
TypeScript source • JavaScript source
Checks if the given array is empty.
Questions
- How to check if an array is empty?
TypeScript source • JavaScript source
Partitions the given array to the ones that pass the given predicate function and the ones that do not. By [convention of the Haskell's Data.Either](http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Either.html), values that pass the predicate are placed at the right.
Type signature
<T>(predicate: (x: T) => boolean) => (xs: T[]) => readonly [T[], T[]]
Examples
partition((x) => x % 2 === 1)([
1, 2, 3, 4, 5,
]);
Try in REPL
Questions
- How to partition an array based on a condition?
- How to divide an array by a filter function?
TypeScript source • JavaScript source
Returns the given array without the last element.
Questions
- How to get an array without the last element?
- How to remove the last element from an array?
TypeScript source • JavaScript source
Generates an array of numbers from 0 to n - 1.
Questions
- How to create an array of all integers from 0 to N exclusive?
TypeScript source • JavaScript source
Removes an element at the given index from the given array.
Type signature
(index: number) => <T>(xs: T[]) => T[]
Questions
- How to remove an item from an array at a particular index?
TypeScript source • JavaScript source
Repeats the given element by the given count of times.
Type signature
(count: number) => <T>(value: T) => T[]
Questions
- How to repeat a value N times?
TypeScript source • JavaScript source
Reverses the given array without mutating it (in contrast to Array.reverse).
Questions
- How to reverse an array without mutating it?
TypeScript source • JavaScript source
Reverses the given array when enabled.
Type signature
(enabled: boolean) => <T>(xs: T[]) => T[]
Questions
- How to reverse an array without mutating it only when a condition is satisfied?
TypeScript source • JavaScript source
Returns the second element or undefined when there are less than two elements in the given array.
Type signature
<T>(xs: T[]) => T | undefined
Questions
- How to get the second element of an array?
TypeScript source • JavaScript source
Returns the second to last element or undefined when there are less than two elements in the given array.
Type signature
<T>(xs: T[]) => T | undefined
Questions
- How to get the second to last element of an array?
TypeScript source • JavaScript source
Shifts the given array to the left and circulates the elements back by modulo of the array's length.
Type signature
(count: number) => <T>(xs: T[]) => T[]
Examples
shift(1)([1, 2, 3, 4, 5]);
Try in REPL
shift(2)([1, 2, 3, 4, 5]);
Try in REPL
shift(3)([1, 2, 3, 4, 5]);
Try in REPL
TypeScript source • JavaScript source
Shuffles the given array in random order with Math.random as the default.
Type signature
<T>(xs: T[], random?: () => number) => T[]
Examples
let i = 0;
const random = () =>
[
0.013606630487694282,
0.21052486239086554,
0.28299838254636556,
0.696161009199874,
0.32165320593537117,
][i++];
shuffle([1, 2, 3, 4, 5], random);
Try in REPL
TypeScript source • JavaScript source
Shuffles the given array in-place in random order with Math.random as the default.
Type signature
<T>(xs: T[], random?: () => number) => T[]
Examples
let i = 0;
const random = () =>
[
0.013606630487694282,
0.21052486239086554,
0.28299838254636556,
0.696161009199874,
0.32165320593537117,
][i++];
shuffleInPlace(
[1, 2, 3, 4, 5],
random,
);
Try in REPL
Questions
- How to shuffle an array in place?
TypeScript source • JavaScript source
Checks if the given array contains exactly one element.
Questions
- How to check if an array contains only one element?
TypeScript source • JavaScript source
Skips the given count of elements from the given array.
Type signature
(count: number) => <T>(xs: T[]) => T[]
Questions
- How to skip the first few elements of an array?
TypeScript source • JavaScript source
Returns a new array composed of tuples of the given sliding window length of consecutive elements.
Type signature
(count: number) => <T>(xs: T[]) => T[][]
Examples
slidingWindow(2)([1, 2, 3, 4]);
Try in REPL
slidingWindow(3)([1, 2, 3, 4, 5]);
Try in REPL
slidingWindow(1)([
1, 2, 3, 4, 5, 6,
]);
Try in REPL
Questions
- How to iterate an array pairwise?
TypeScript source • JavaScript source
Sorts the given array without mutating it.
Type signature
<T>(f?: (a: T, b: T) => number) => (xs: T[]) => T[]
Examples
sort((a, b) => a - b)([
13, 79, 20, 69, 44, 67, 18, 95,
26, 55,
]);
Try in REPL
Questions
- How to sort an array without mutating it?
TypeScript source • JavaScript source
Sums the given array of numbers.
Questions
- How to sum elements of an array?
TypeScript source • JavaScript source
Takes up to a given count of elements.
Type signature
(count: number) => <T>(xs: T[]) => T[]
Questions
- How to get the first N number of elements from an array?
TypeScript source • JavaScript source
Returns unique elements of the given array.
Questions
- How to find all unique values in an array?
TypeScript source • JavaScript source
Filters out duplicated values based on the result of the given key selector.
Type signature
<T, TResult>(f: (x: T) => TResult) => (xs: T[]) => T[]
Examples
uniqueBy(({ id }) => id)([
{ id: 1, value: "a" },
{ id: 2, value: "b" },
{ id: 1, value: "c" },
]);
Try in REPL
Questions
- How to find all unique values in an array by some predicate?
TypeScript source • JavaScript source
Zips the given arrays together into pairs.
Type signature
(xs: unknown[], ys: unknown[]) => [unknown, unknown][]
TypeScript source • JavaScript source
Zips the given arrays together into pairs.
Type signature
<T>(...xs: T[][]) => T[][]
Examples
zipN([1, 2, 3], [4, 5, 6]);
Try in REPL
zipN(
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
);
Try in REPL
zipN([1, 2], [4, 5, 6], [7, 8, 9]);
Try in REPL
Questions
- How to zip multiple arrays?
TypeScript source • JavaScript source
Zips the given arrays together with the given function.
Type signature
<T1, T2>(f?: (x: T1, y: T2) => [T1, T2]) => (xs: T1[], ys: T2[]) => [T1, T2][]
Examples
zipWith((x, y) => x * x + y)(
[1, 2, 3],
[4, 5, 6],
);
Try in REPL
Questions
- How to zip two arrays with a given function?
TypeScript source • JavaScript source