Skip to content

List

zipWith()

Applies a binary function to corresponding elements of two lists and returns a new list.

  • Type
  • Details

The first parameter is a binary function , followed by two lists of equal length .

Returns a new list of the same length as the input lists, where .

  • Example
js
const f = (a, b) => a + b;
zipWith(f, [1, 2, 3], [4, 5, 6]); //[5, 7, 9]
const f = (a, b) => a + b;
zipWith(f, [1, 2, 3], [4, 5, 6]); //[5, 7, 9]

zip()

Pairs elements from two lists into a new list of tuples.

  • Type
  • Details

Takes two lists of equal length.

Returns a new list of the same length as the input lists, where .

This is equivalent to zipWith((a, b) => [a, b]).

  • Example
js
zip([1, 2, 3], [4, 5, 6]); //[[1, 4], [2, 5], [3, 6]]
zip([1, 2, 3], [4, 5, 6]); //[[1, 4], [2, 5], [3, 6]]

join()

Joins elements in a list into a string, separated by a given delimiter.

  • Type
  • Details

The first parameter is a string that serves as the delimiter. The second parameter is a list. The function returns a string that joins all the elements in the list, separated by the delimiter.

  • Example
js
join('|', [1, 2, 3]); //"1|2|3"
join('|', [1, 2, 3]); //"1|2|3"

slice()

Slices a section of a list.

  • Type
  • Details

Takes two integers representing the start and end indices, and returns a new list containing elements within the specified range.

  • Example
js
slice(0, 3, [1, 2, 3, 4]); //[1, 2, 3]
slice(0, 3, [1, 2, 3, 4]); //[1, 2, 3]

take()

Takes the first elements from a list.

  • Type
  • Details

Takes an integer and a list as parameters. Returns a new list containing the first elements from the given list.

  • Example
js
take(3, [1, 2, 3, 4]); //[1, 2, 3]
take(3, [1, 2, 3, 4]); //[1, 2, 3]

takeWhile()

Takes elements from the beginning of a list as long as they satisfy a given predicate.

  • Type
  • Details

The first parameter is a predicate function , and the second parameter is a list. Elements are taken from the beginning of the list as long as they satisfy .

  • Example
js
takeWhile((v) => v < 3, [1, 1, 4, 5, 1, 4]); //[1, 1]
takeWhile((v) => v < 3, [1, 1, 4, 5, 1, 4]); //[1, 1]

drop()

Drops the first elements from a list.

  • Type
  • Details

Takes an integer and a list as parameters. Returns a new list with the first elements removed.

  • Example
js
drop(3, [1, 2, 3, 4]); //[4]
drop(3, [1, 2, 3, 4]); //[4]

dropWhile()

Drops elements from the beginning of a list as long as they satisfy a given predicate.

  • Type
  • Details

The first parameter is a predicate function , and the second parameter is a list. Elements are dropped from the beginning of the list as long as they satisfy .

  • Example
js
dropWhile((v) => v < 3, [1, 4, 5, 1, 4]); //[4, 5, 1, 4]
dropWhile((v) => v < 3, [1, 4, 5, 1, 4]); //[4, 5, 1, 4]

every()

Checks if all elements in a list satisfy a given predicate.

  • Type
  • Details

Takes a predicate function and a list as parameters. Returns true if all elements in the list satisfy , and false otherwise.

  • Example
js
every((v) => v < 3, [1, 1, 4, 5, 1, 4]); //false
every((v) => v < 3, [1, 1, 4, 5, 1, 4]); //false

some()

Checks if any element in a list satisfies a given predicate.

  • Type
  • Details

Takes a predicate function and a list as parameters. Returns true if any element in the list satisfies , and false otherwise.

  • Example
js
some((v) => v < 3, [1, 4, 5, 1, 4]); //true
some((v) => v < 3, [1, 4, 5, 1, 4]); //true

concat()

Concatenates two lists.

  • Type
  • Details

Takes two lists and returns a new list that is a concatenation of the two.

  • Example
js
concat([1, 2, 3], [4, 5, 6]); //[1, 2, 3, 4, 5, 6]
concat([1, 2, 3], [4, 5, 6]); //[1, 2, 3, 4, 5, 6]

concatr()

Concatenates two lists in reverse order.

  • Type
  • Details

Takes two lists and returns a new list that is a reverse concatenation of the two.

  • Example
js
concatr([1, 2, 3], [4, 5, 6]); //[4, 5, 6

, 1, 2, 3]
concatr([1, 2, 3], [4, 5, 6]); //[4, 5, 6

, 1, 2, 3]

Returns the first element of a list.

  • Type
  • Details

Takes a list and returns its first element.

  • Example
js
head([1, 2, 3]); //1
head([1, 2, 3]); //1

tail()

Returns the last element of a list.

  • Type
  • Details

Takes a list and returns its last element.

  • Example
js
tail([1, 2, 3]); //3
tail([1, 2, 3]); //3

dropHead()

Drops the first element of a list.

  • Type
  • Details

Takes a list and returns a new list with the first element removed.

  • Example
js
dropHead([1, 2, 3]); //[2, 3]
dropHead([1, 2, 3]); //[2, 3]

dropTail()

Drops the last element of a list.

  • Type
  • Details

Takes a list and returns a new list with the last element removed.

  • Example
js
dropTail([1, 2, 3]); //[1, 2]
dropTail([1, 2, 3]); //[1, 2]

includes()

Checks if a given element is present in a list.

  • Type
  • Details

Takes an element and a list, and returns true if the element is present in the list, and false otherwise.

  • Example
js
includes(4, [1, 2, 3, 4]); //true
includes(4, [1, 2, 3, 4]); //true

reverse()

Reverses a list.

  • Type
  • Details

Takes a list and returns a new list with the elements in reverse order.

  • Example
js
reverse([1, 2, 3]); //[3, 2, 1]
reverse([1, 2, 3]); //[3, 2, 1]

countWith()

Counts the number of elements in a list that satisfy a given predicate.

  • Type
  • Details

Takes a predicate function and a list, and returns the number of elements that satisfy .

  • Example
js
countWith((v) => v === 5, [1, 2, 3, 4, 5, 5, 5]); //3
countWith((v) => v === 5, [1, 2, 3, 4, 5, 5, 5]); //3

count()

Counts the occurrences of a specific element in a list.

  • Type
  • Details

Takes an element and a list, and returns the number of occurrences of that element in the list.

  • Example
js
count(5, [1, 2, 3, 4, 5, 5, 5]); //3
count(5, [1, 2, 3, 4, 5, 5, 5]); //3

pairList()

Pairs the first element with the rest of the elements in the list.

  • Type
  • Details

Takes a list and returns a pair consisting of its first element and the rest of the elements.

  • Example
js
const [x, xs] = pairList([1, 2, 3, 4, 5]); // x=1, xs=[2, 3, 4, 5]
const [x, xs] = pairList([1, 2, 3, 4, 5]); // x=1, xs=[2, 3, 4, 5]

PureEval released under the GPL-3.0 License.