Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

data.lists

ranges, map/filter/reduce, building lists

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

operators

# a..b
1..4
# → [1, 2, 3]
# a..=b
1..=4
# → [1, 2, 3, 4]
# list + list
[1] + [2, 3]
# → [1, 2, 3]

Functions

functiondescription
range(n: int) / range(a: int, b: int)integers in [0, n) or [a, b); same as a..b
push(xs: list, v: any)append v in place and return the list
pop(xs: list)remove and return the last item in place, or nil if empty
shift(xs: list)remove and return the first item in place, or nil if empty
unshift(xs: list, v: any)insert v at the front in place and return the list
map(xs: list, f: fn)apply f to every item
filter(xs: list, f: fn) / filter(m: map, f: fn)keep items where f is truthy; for maps, f gets (key, value)
reduce(xs: list, init: any, f: fn)fold with f(acc, item)
flatten(xs: list)unpack nested lists one level
zip(a: list, b: list)pair up items, stopping at the shorter list
enumerate(xs: list)[index, item] pairs
group_by(xs: list, f: fn)map from each key f gives to the items with that key
count_by(xs: list, f: fn)map from each key f gives to how many items have it
chunks(xs: list, n: int)split into lists of n items; the last may be shorter
windows(xs: list, n: int)every run of n neighboring items
step(xs: list, n: int)every nth item, starting with the first

range

range(n: int) / range(a: int, b: int): integers in [0, n) or [a, b); same as a..b

range(4)
# → [0, 1, 2, 3]
range(2, 5)
# → [2, 3, 4]

See also: map

push

push(xs: list, v: any): append v in place and return the list

[1, 2].push(3)
# → [1, 2, 3]

See also: pop, unshift

pop

pop(xs: list): remove and return the last item in place, or nil if empty

xs = [1, 2, 3]; [xs.pop, xs]
# → [3, [1, 2]]

See also: push, shift

shift

shift(xs: list): remove and return the first item in place, or nil if empty

xs = [1, 2, 3]; [xs.shift, xs]
# → [1, [2, 3]]

See also: unshift, pop

unshift

unshift(xs: list, v: any): insert v at the front in place and return the list

[2, 3].unshift(1)
# → [1, 2, 3]

See also: shift, push

map

map(xs: list, f: fn): apply f to every item

[1, 2, 3].map(|x| x * 10)
# → [10, 20, 30]

See also: filter, reduce

filter

filter(xs: list, f: fn) / filter(m: map, f: fn): keep items where f is truthy; for maps, f gets (key, value)

(1..10).filter(|x| x % 3 == 0)
# → [3, 6, 9]
{a: 1, b: 5}.filter(|k, v| v > 2)
# → {b: 5}

See also: map, reduce, filter_keys

reduce

reduce(xs: list, init: any, f: fn): fold with f(acc, item)

[1, 2, 3].reduce(10, |acc, x| acc + x)
# → 16

See also: sum, map

flatten

flatten(xs: list): unpack nested lists one level

[[1, 2], [3], 4].flatten
# → [1, 2, 3, 4]

See also: chunks

zip

zip(a: list, b: list): pair up items, stopping at the shorter list

zip([1, 2, 3], ["a", "b"])
# → [[1, "a"], [2, "b"]]

See also: enumerate

enumerate

enumerate(xs: list): [index, item] pairs

["a", "b"].enumerate
# → [[0, "a"], [1, "b"]]
["a", "b"].enumerate.map(|[i, x]| "{i}:{x}")
# → ["0:a", "1:b"]

See also: zip

group_by

group_by(xs: list, f: fn): map from each key f gives to the items with that key

["apple", "avocado", "banana"].group_by(|w| w[0])
# → {a: ["apple", "avocado"], b: ["banana"]}
(1..=6).group_by(|x| x % 2 == 0)
# → {false: [1, 3, 5], true: [2, 4, 6]}

See also: count_by

count_by

count_by(xs: list, f: fn): map from each key f gives to how many items have it

"mississippi".chars.count_by(|c| c)
# → {m: 1, i: 4, s: 4, p: 2}

See also: group_by, count

chunks

chunks(xs: list, n: int): split into lists of n items; the last may be shorter

(1..=7).chunks(3)
# → [[1, 2, 3], [4, 5, 6], [7]]

See also: windows, flatten

windows

windows(xs: list, n: int): every run of n neighboring items

[1, 2, 3, 4].windows(2)
# → [[1, 2], [2, 3], [3, 4]]
[1, 4, 9, 16].windows(2).map(|[a, b]| b - a)
# → [3, 5, 7]

See also: chunks

step

step(xs: list, n: int): every nth item, starting with the first

(0..=20).step(5)
# → [0, 5, 10, 15, 20]
(1..10).step(2)
# → [1, 3, 5, 7, 9]

See also: range

More examples

lists

# stepped range
(1..=10).step(3)
# → [1, 4, 7, 10]
# primes under 100
(1..100).filter(is_prime).len
# → 25
# pipe into a function
(1..=10).map(|x| x ** 2) |> sum
# → 385
# fold
[1, 2, 3].reduce(10, |acc, x| acc + x)
# → 16