Skip to content

Lists ​

Functions over lists. None of them mutate their input: sort and reverse return new lists. Functions that take a lambda accept an implicit . lambda or an arrow lambda, and call it with the item and its index.

Every function can be called as f(x, ...) or as a method, x.f(...). In the signatures, T and U stand for any type, T[] is a list, ? marks an optional parameter, and (T, number) => boolean is a lambda parameter.

The examples use this context:

ts
const context = { items: [{ name: "Pen", price: 2, qty: 3 }, { name: "Book", price: 12, qty: 1 }, { name: "Bag", price: 30, qty: 1 }] }
FunctionDescription
mapTransforms each item.
filterKeeps the items for which the lambda is true.
findThe first item for which the lambda is true, or null.
findIndexThe position of the first item for which the lambda is true, or -1.
someWhether the lambda is true for at least one item.
everyWhether the lambda is true for every item.
noneWhether the lambda is false for every item.
countThe number of items, or of items for which the lambda is true.
flatMapTransforms each item and flattens list results one level.
reduceFolds a list into one value: reduce(list, (acc, item) => ..., initial). Name both parameters; "." is not allowed, since the first one is the accumulator.
sortSorts numbers, text, timestamps, or durations; pass "desc" to reverse.
sortBySorts by a key; pass "desc" to reverse. Nulls sort first ("asc") or last ("desc").
groupByGroups items into a map of lists by a key. Map keys are text, so a key is its text form: 1 and "1", or true and "true", share a group. A key must be a string, number, or boolean (null is an error). Keys are listed integer-like first, ascending, then in first-seen order.
reverseThe items in reverse order.
uniqueThe items without duplicates (by value), in first-seen order.
flatFlattens nested lists one level.
firstThe first item, or null.
lastThe last item, or null.
joinJoins items into text with a separator (default ",").
sliceA section of text or a list; negative positions count from the end.
atThe item or character at a position; negative positions count from the end.
includesWhether text contains a substring, or a list contains a value.
indexOfPosition of the first match, or -1.
lastIndexOfPosition of the last match, or -1.
isEmptyWhether a list, text, or map has no items; null is empty.
minThe smallest value (nulls are skipped); null for an empty list.
maxThe largest value (nulls are skipped); null for an empty list.
sumThe sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.
avgThe mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list.

map ​

Transforms each item.

  • map(T[], (T, number) => U): U[]
bonsai
items.map(.name) // => ["Pen", "Book", "Bag"]
items.map((it, i) => `${i + 1}. ${it.name}`) // => ["1. Pen", "2. Book", "3. Bag"]

filter ​

Keeps the items for which the lambda is true.

  • filter(T[], (T, number) => boolean): T[]
bonsai
items.filter(.price > 5).map(.name) // => ["Book", "Bag"]

find ​

The first item for which the lambda is true, or null.

  • find(T[], (T, number) => boolean): T | null
bonsai
items.find(.price > 10) // => { name: "Book", price: 12, qty: 1 }
items.find(.price > 100) // => null

findIndex ​

The position of the first item for which the lambda is true, or -1.

  • findIndex(T[], (T, number) => boolean): number
bonsai
items.findIndex(.name == "Bag") // => 2

some ​

Whether the lambda is true for at least one item.

  • some(T[], (T, number) => boolean): boolean
bonsai
items.some(.price > 20) // => true

every ​

Whether the lambda is true for every item.

  • every(T[], (T, number) => boolean): boolean
bonsai
items.every(.qty >= 1) // => true

none ​

Whether the lambda is false for every item.

  • none(T[], (T, number) => boolean): boolean
bonsai
items.none(.price < 0) // => true

count ​

The number of items, or of items for which the lambda is true.

  • count(T[]): number
  • count(T[], (T, number) => boolean): number
bonsai
items.count() // => 3
items.count(.qty > 1) // => 1

flatMap ​

Transforms each item and flattens list results one level.

  • flatMap(T[], (T, number) => U[] | U): U[]
bonsai
[1, 2].flatMap([., . * 10]) // => [1, 10, 2, 20]

reduce ​

Folds a list into one value: reduce(list, (acc, item) => ..., initial). Name both parameters; "." is not allowed, since the first one is the accumulator.

  • reduce(T[], (U, T) => U, U): U
bonsai
items.reduce((total, it) => total + it.price * it.qty, 0) // => 48

sort ​

Sorts numbers, text, timestamps, or durations; pass "desc" to reverse.

  • sort(T[], ("asc" | "desc")?): T[]
bonsai
[3, 1, 2].sort() // => [1, 2, 3]
["b", "a"].sort("desc") // => ["b", "a"]

sortBy ​

Sorts by a key; pass "desc" to reverse. Nulls sort first ("asc") or last ("desc").

  • sortBy(T[], (T, number) => K, ("asc" | "desc")?): T[]
bonsai
items.sortBy(.price, "desc").map(.name) // => ["Bag", "Book", "Pen"]

groupBy ​

Groups items into a map of lists by a key. Map keys are text, so a key is its text form: 1 and "1", or true and "true", share a group. A key must be a string, number, or boolean (null is an error). Keys are listed integer-like first, ascending, then in first-seen order.

  • groupBy(T[], (T, number) => string | number | boolean): { [key: string]: T[] }
bonsai
items.groupBy(.qty > 1 ? "bulk" : "single").keys() // => ["bulk", "single"]
[1, 2, 3, 4].groupBy(. % 2 == 0 ? "even" : "odd") // => { odd: [1, 3], even: [2, 4] }
[1, "1", 2].groupBy(.) // => { "1": [1, "1"], "2": [2] }
["b", "10", "a", "2"].groupBy(.).keys() // => ["2", "10", "b", "a"]

reverse ​

The items in reverse order.

  • reverse(T[]): T[]
bonsai
[1, 2, 3].reverse() // => [3, 2, 1]

unique ​

The items without duplicates (by value), in first-seen order.

  • unique(T[]): T[]
bonsai
[1, 2, 1, 3, 2].unique() // => [1, 2, 3]
[{ a: 1 }, { a: 1 }].unique() // => [{ a: 1 }]

flat ​

Flattens nested lists one level.

  • flat(any[]): any[]
bonsai
[[1, 2], [3], 4].flat() // => [1, 2, 3, 4]

first ​

The first item, or null.

  • first(T[]): T | null
bonsai
items.first().name // => "Pen"
[].first() // => null

last ​

The last item, or null.

  • last(T[]): T | null
bonsai
[1, 2, 3].last() // => 3

join ​

Joins items into text with a separator (default ",").

  • join((string | number | boolean | null | timestamp | duration)[], string?): string
bonsai
items.map(.name).join(", ") // => "Pen, Book, Bag"
[1, 2, 3].join() // => "1,2,3"

slice ​

A section of text or a list; negative positions count from the end.

  • slice(T[], number, number?): T[]
bonsai
[1, 2, 3, 4].slice(1, 3) // => [2, 3]
[1, 2, 3, 4].slice(-2) // => [3, 4]

at ​

The item or character at a position; negative positions count from the end.

  • at(T[], number): T | null
bonsai
[1, 2, 3].at(-1) // => 3
[1, 2, 3].at(5) // => null

includes ​

Whether text contains a substring, or a list contains a value.

  • includes(T[], any): boolean
bonsai
[1, 2, 3].includes(2) // => true
[{ id: 1 }].includes({ id: 1 }) // => true

indexOf ​

Position of the first match, or -1.

  • indexOf(T[], any): number
bonsai
["a", "b", "c"].indexOf("b") // => 1

lastIndexOf ​

Position of the last match, or -1.

  • lastIndexOf(T[], any): number
bonsai
[1, 2, 1].lastIndexOf(1) // => 2

isEmpty ​

Whether a list, text, or map has no items; null is empty.

  • isEmpty(any[] | string | { [key: string]: any } | null): boolean
bonsai
[].isEmpty() // => true
isEmpty(null) // => true

min ​

The smallest value (nulls are skipped); null for an empty list.

  • min(T[]): T | null
bonsai
items.map(.price).min() // => 2
[].min() // => null

max ​

The largest value (nulls are skipped); null for an empty list.

  • max(T[]): T | null
bonsai
items.map(.price).max() // => 30

sum ​

The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.

  • sum((number | null)[]): number
  • sum((duration | null)[]): duration
bonsai
items.map(.price * .qty).sum() // => 48
[1, null, 2].sum() // => 3

avg ​

The mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list.

  • avg((number | null)[]): number | null
  • avg((duration | null)[]): duration | null
bonsai
items.map(.price).avg() // => 14.666666666666666
[].avg() // => null