array

Checks if the given array is present and it is not empty (contains at least one element).

Type signature

<T>(xs?: T[]) => boolean

Examples

any([]);
// ⇒ false
Try in REPL
any([1, 2, 3]);
// ⇒ true
Try in REPL

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 sourceJavaScript source

Checks if the given arguments are all `Arrays`.

Type signature

<T>(...xs: T[]) => boolean

Examples

are([2, 3]);
// ⇒ true
Try in REPL
are([1, 2, 3], []);
// ⇒ true
Try in REPL
are([1, 2, 3], 8, [1, 3], "test");
// ⇒ false
Try in REPL

Questions

  • How to check if all the given values are arrays?

TypeScript sourceJavaScript 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[][]

Examples

chunk(2)(["a", "b", "c", "d"]);
// ⇒ [['a', 'b'], ['c', 'd']]
Try in REPL
chunk(3)(["a", "b", "c", "d"]);
// ⇒ [['a', 'b', 'c'], ['d']]
Try in REPL

Questions

  • How to split an array into chunks?
  • How to split an array into chunks of the same size?

TypeScript sourceJavaScript 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],
);
// ⇒ [1, 3, 5, 6]
Try in REPL

Questions

  • How to find elements which are present in the first array and not in the second?

TypeScript sourceJavaScript source

Checks if two arrays are not equal.

Type signature

<T>(xs?: T[], ys?: T[]) => boolean

Examples

differs([1, 2, 3], [1, 2]);
// ⇒ true
Try in REPL
differs([1, 2, 3], [1, 2, 3]);
// ⇒ false
Try in REPL

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 sourceJavaScript source

Lists all the duplicated values in the given array.

Type signature

<T>(xs: T[]) => T[]

Examples

duplicates([
  1, 2, 3, 4, 3, 4, 3, 6,
]);
// ⇒ [3, 4, 3]
Try in REPL

Questions

  • How to find duplicates in an array?

TypeScript sourceJavaScript source

Empty array.

Type signature

unknown[]

Examples

empty;
// ⇒ []
Try in REPL

Questions

  • How to get an empty array?

TypeScript sourceJavaScript source

Takes exactly the given count of elements.

Type signature

(count: number) => <T>(xs: T[]) => T[]

Examples

exact(5)([1, 2, 3]);
// ⇒ [1, 2, 3, undefined, undefined]
Try in REPL
exact(2)([1, 2, 3]);
// ⇒ [1, 2]
Try in REPL

Questions

  • How to get exactly N elements out of an array?

TypeScript sourceJavaScript source

Filters out the given value.

Type signature

<T>(y: T) => (xs: T[]) => T[]

Examples

except(2)([1, 2, 3, 4, 5]);
// ⇒ [1, 3, 4, 5]
Try in REPL
except(2)([1, 2, 2, 4, 2]);
// ⇒ [1, 4]
Try in REPL

Questions

  • How to get all the values of an array except the given one?

TypeScript sourceJavaScript 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;
// ⇒ true
ys;
// ⇒ [1, 3, 5, 7]
Try in REPL

Questions

  • How to filter an array in place?

TypeScript sourceJavaScript 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]);
// ⇒ 3
Try in REPL
find((x) => x > 2)([
  1, 2, -3, -5, -7,
]);
// ⇒ undefined
Try in REPL

Questions

  • How to find an element of an array by a given predicate?

TypeScript sourceJavaScript source

Returns the first element or undefined when there are no elements in the given array.

Type signature

<T>([x]: T[]) => T | undefined

Examples

first([1, 2, 3]);
// ⇒ 1
Try in REPL
first([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the first element of an array?

TypeScript sourceJavaScript 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",
]);
// ⇒ ["t", "e", "s", "t", "1", "2", "3"]
Try in REPL

Questions

  • How to flat map an array?
  • How to map and then flatten an array?

TypeScript sourceJavaScript source

Flattens the nested arrays by a single level.

Type signature

<T>(xs: T[]) => T[]

Examples

flatten([1, [2, 3], 4, [5, 6]]);
// ⇒ [1, 2, 3, 4, 5, 6]
Try in REPL
flatten([
  1,
  [2, [3, 6]],
  4,
  [5, 6],
]);
// ⇒ [1, 2, [3, 6], 4, 5, 6]
Try in REPL

Questions

  • How to flatten an array?

TypeScript sourceJavaScript source

Inserts the given item to the array at a specific index.

Type signature

(index: number) => <T>(item: T) => ([...xs]: T[]) => T[]

Examples

insert(0)("d")(["a", "b", "c"]);
// ⇒ ['d', 'a', 'b', 'c']
Try in REPL
insert(1)("d")(["a", "b", "c"]);
// ⇒ ['a', 'd', 'b', 'c']
Try in REPL

Questions

  • How to insert an element to an array at a given position?

TypeScript sourceJavaScript 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],
);
// ⇒ [2, 3, 5]
Try in REPL

Questions

  • How to find common elements present in both arrays?

TypeScript sourceJavaScript source

Checks if the given argument is an array.

Type signature

(value?: unknown) => boolean

Examples

is([1, 2, 3]);
// ⇒ true
Try in REPL
is({ a: 5 });
// ⇒ false
Try in REPL

Questions

  • How to check if a value is an array?

TypeScript sourceJavaScript source

Returns the last element or undefined when there are no elements in the given array.

Type signature

<T>(xs: T[]) => T | undefined

Examples

last([1, 2, 3]);
// ⇒ 3
Try in REPL
last([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the last element of an array?

TypeScript sourceJavaScript source

Returns the number of elements in the given array.

Type signature

<T>(xs: T[]) => number

Examples

length([true, 1]);
// ⇒ 2
Try in REPL
length([1, 2, 3]);
// ⇒ 3
Try in REPL
length([]);
// ⇒ 0
Try in REPL

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 sourceJavaScript source

Checks if lengths of the given arrays differ.

Type signature

<T1, T2>(a: T1[], b: T2[]) => boolean

Examples

lengthDiffers([1, 2, 3], [1, 2]);
// ⇒ true
Try in REPL
lengthDiffers([6, 7], [1, 2]);
// ⇒ false
Try in REPL

Questions

  • How to check if array lengths differ?
  • How to check if the given arrays have different lengths?

TypeScript sourceJavaScript source

Maps the given array with the given functions.

Type signature

<T>(...fs: ((x: T) => T)[]) => (xs: T[]) => T[]

Examples

map((x) => x * x)([1, 2, 3]);
// ⇒ [1, 4, 9]
Try in REPL
map(
  (x) => x * x,
  (x) => x + 1,
)([1, 2, 3]);
// ⇒ [2, 5, 10]
Try in REPL

Questions

  • How to map an array?

TypeScript sourceJavaScript source

Returns the middle element or the right one when the number of elements is even.

Type signature

<T>(xs: T[]) => T | undefined

Examples

midpoint([1, 2, 3, 4, 5]);
// ⇒ 3
Try in REPL
midpoint([1, 2, 3, 4]);
// ⇒ 3
Try in REPL

Questions

  • How to get the element in the middle of an array?
  • How to get the middle element of an array?

TypeScript sourceJavaScript 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,
]);
// ⇒ [-5, 32]
Try in REPL
minMax([1]);
// ⇒ [1, 1]
Try in REPL
minMax([]);
// ⇒ [undefined, undefined]
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 sourceJavaScript source

Checks if the given array contains more than one element.

Type signature

<T>(xs: T[]) => boolean

Examples

multiple([1, 2, 3]);
// ⇒ true
Try in REPL
multiple([1, 2]);
// ⇒ true
Try in REPL
multiple([1]);
// ⇒ false
Try in REPL
multiple([]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array contains multiple elements?
  • How to check whether multiple values exist within an array?

TypeScript sourceJavaScript source

Checks if the given array is empty.

Type signature

<T>(xs?: T[]) => boolean

Examples

none([]);
// ⇒ true
Try in REPL
none([1, 2, 3]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array is empty?

TypeScript sourceJavaScript 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,
]);
// ⇒ [[2, 4], [1, 3, 5]])
Try in REPL

Questions

  • How to partition an array based on a condition?
  • How to divide an array by a filter function?

TypeScript sourceJavaScript source

Returns the given array without the last element.

Type signature

<T>(xs: T[]) => T[]

Examples

pop([1, 2, 3, 4]); // ⇒ [1, 2, 3]
Try in REPL
pop([]); // ⇒ []
Try in REPL

Questions

  • How to get an array without the last element?
  • How to remove the last element from an array?

TypeScript sourceJavaScript source

Generates an array of numbers from 0 to n - 1.

Type signature

(n: number) => number[]

Examples

range(3);
// ⇒ [0, 1, 2]
Try in REPL

Questions

  • How to create an array of all integers from 0 to N exclusive?

TypeScript sourceJavaScript source

Removes an element at the given index from the given array.

Type signature

(index: number) => <T>(xs: T[]) => T[]

Examples

removeAt(3)([1, 2, 3, 4, 5, 6]);
// ⇒ [1, 2, 3, 5, 6]
Try in REPL

Questions

  • How to remove an item from an array at a particular index?

TypeScript sourceJavaScript source

Repeats the given element by the given count of times.

Type signature

(count: number) => <T>(value: T) => T[]

Examples

repeat(3)("test");
// ⇒ ["test", "test", "test"]
Try in REPL

Questions

  • How to repeat a value N times?

TypeScript sourceJavaScript source

Reverses the given array without mutating it (in contrast to Array.reverse).

Type signature

<T>(xs: T[]) => T[]

Examples

reverse([1, 2, 3, 4, 5]);
// ⇒ [5, 4, 3, 2, 1]
Try in REPL

Questions

  • How to reverse an array without mutating it?

TypeScript sourceJavaScript source

Reverses the given array when enabled.

Type signature

(enabled: boolean) => <T>(xs: T[]) => T[]

Examples

reverseIf(true)([1, 2, 3, 4, 5]);
// ⇒ [5, 4, 3, 2, 1]
Try in REPL
reverseIf(false)([1, 2, 3, 4, 5]);
// ⇒ [1, 2, 3, 4, 5]
Try in REPL

Questions

  • How to reverse an array without mutating it only when a condition is satisfied?

TypeScript sourceJavaScript 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

Examples

second([1, 2, 3, 4, 5]);
// ⇒ 2
Try in REPL
second([1]);
// ⇒ undefined
Try in REPL
second([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the second element of an array?

TypeScript sourceJavaScript 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

Examples

secondToLast([1, 2, 3, 4, 5]);
// ⇒ 4
Try in REPL
secondToLast([1]);
// ⇒ undefined
Try in REPL
secondToLast([]);
// ⇒ undefined
Try in REPL

Questions

  • How to get the second to last element of an array?

TypeScript sourceJavaScript 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]);
// ⇒ [2, 3, 4, 5, 1]
Try in REPL
shift(2)([1, 2, 3, 4, 5]);
// ⇒ [3, 4, 5, 1, 2]
Try in REPL
shift(3)([1, 2, 3, 4, 5]);
// ⇒ [4, 5, 1, 2, 3]
Try in REPL

Questions

  • How to shift an array?

TypeScript sourceJavaScript 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); // => [3, 5, 4, 2, 1]
Try in REPL

Questions

  • How to shuffle an array?

TypeScript sourceJavaScript 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,
); // => [3, 5, 4, 2, 1]
Try in REPL

Questions

  • How to shuffle an array in place?

TypeScript sourceJavaScript source

Checks if the given array contains exactly one element.

Type signature

<T>(xs: T[]) => boolean

Examples

single([1]);
// ⇒ true
Try in REPL
single([1, 2, 3]);
// ⇒ false
Try in REPL
single([]);
// ⇒ false
Try in REPL

Questions

  • How to check if an array contains only one element?

TypeScript sourceJavaScript source

Skips the given count of elements from the given array.

Type signature

(count: number) => <T>(xs: T[]) => T[]

Examples

skip(2)([1, 2, 3, 4, 5]);
// ⇒ [3, 4, 5]
Try in REPL

Questions

  • How to skip the first few elements of an array?

TypeScript sourceJavaScript 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]);
// ⇒ [[1, 2], [2, 3], [3, 4]]
Try in REPL
slidingWindow(3)([1, 2, 3, 4, 5]);
// ⇒ [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
Try in REPL
slidingWindow(1)([
  1, 2, 3, 4, 5, 6,
]);
// ⇒ [[1], [2], [3], [4], [5], [6]]
Try in REPL

Questions

  • How to iterate an array pairwise?

TypeScript sourceJavaScript 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,
]);
// ⇒ [13, 18, 20, 26, 44, 55, 67, 69, 79, 95]
Try in REPL

Questions

  • How to sort an array without mutating it?

TypeScript sourceJavaScript source

Sums the given array of numbers.

Type signature

(xs: number[]) => number

Examples

sum([1, 2, 3, 4, 5]);
// ⇒ 15
Try in REPL

Questions

  • How to sum elements of an array?

TypeScript sourceJavaScript source

Takes up to a given count of elements.

Type signature

(count: number) => <T>(xs: T[]) => T[]

Examples

take(2)([1, 2, 3, 4, 5]);
// ⇒ [1, 2]
Try in REPL
take(10)([1, 2, 3, 4, 5]);
// ⇒ [1, 2, 3, 4, 5]
Try in REPL

Questions

  • How to get the first N number of elements from an array?

TypeScript sourceJavaScript source

Returns unique elements of the given array.

Type signature

<T>(xs: T[]) => T[]

Examples

unique([1, 2, 3, 4, 3, 4, 3, 6]);
// ⇒ [1, 2, 3, 4, 6]
Try in REPL

Questions

  • How to find all unique values in an array?

TypeScript sourceJavaScript 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" },
]);
// ⇒ [{ id: 1, value: 'c' }, { id: 2, value: 'b' }]
Try in REPL

Questions

  • How to find all unique values in an array by some predicate?

TypeScript sourceJavaScript source

Zips the given arrays together into pairs.

Type signature

(xs: unknown[], ys: unknown[]) => [unknown, unknown][]

Examples

zip([1, 2, 3], [4, 5, 6]);
// ⇒ [[1, 4],[2, 5],[3, 6]]
Try in REPL

Questions

  • How to zip two arrays?

TypeScript sourceJavaScript source

Zips the given arrays together into pairs.

Type signature

<T>(...xs: T[][]) => T[][]

Examples

zipN([1, 2, 3], [4, 5, 6]);
// ⇒ [[1, 4], [2, 5], [3, 6]]
Try in REPL
zipN(
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
);
// ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Try in REPL
zipN([1, 2], [4, 5, 6], [7, 8, 9]);
// ⇒ [[1, 4, 7],[2, 5, 8]]
Try in REPL

Questions

  • How to zip multiple arrays?

TypeScript sourceJavaScript 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],
);
// ⇒ [5, 9, 15]
Try in REPL

Questions

  • How to zip two arrays with a given function?

TypeScript sourceJavaScript source