Lists

Basic Operations

FunctionDescription
consConstruct new list by prepending item h to list t
snoc(x, l)Append element x to the end of list l
(l ‖ r)Prepend element x to list xs. Right-associative cons operator
headReturn the head item of list xs, panic if empty
(↑ x)Return first element of list xs. Tight-binding prefix operator
nil?true if list xs is empty, false otherwise
non-nil?true if list xs is non-empty, false otherwise
null?true if x is null, false otherwise
(x ✓)true if x is not null, false otherwise. Postfix not-null check operator
coalesce(xs)Return the first non-null element from list xs, or null if all are null
head-or(d, xs)Return the head item of list xs or default d if empty
tailReturn list xs without the head item. [] causes error
tail-or(d, xs)Return list xs without the head item or d for empty list
nilIdentical to [], the empty list
firstReturn first item of list xs - error if the list is empty
second(xs)Return second item of list - error if there is none
second-or(d, xs)Return second item of list - default d if there is none

List Construction

FunctionDescription
repeat(i)Return infinite list of instances of item i
iterate(f, i)Return list of i with subsequent repeated applications of f to i
ints-fromReturn infinite list of integers from n upwards, alias iota for backwards compatibility
range(b, e)Return list of ints from b to e (not including e)
cycle(l)Create infinite list by cycling elements of list l

Transformations

FunctionDescription
take(n, l)Return initial segment of integer n elements from list l
drop(n, l)Return result of dropping integer n elements from list l
take-while(p?, l)Initial elements of list l while p? is true
take-until(p?)Initial elements of list l while p? is false
drop-while(p?, l)Skip initial elements of list l while p? is true
drop-until(p?)Skip initial elements of list l while p? is false
map(f, l)Map function f over list l
map2(f, l1, l2)Map function f over lists l1 and l2, until the shorter is exhausted
cross(f, xs, ys)Apply f to every combination of elements from xs and ys (cartesian product)
filter(p?, l)Return list of elements of list l that satisfy predicate p?
remove(p?, l)Return list of elements of list l that do not satisfy predicate p?
reverse(l)Reverse list l

Combining Lists

FunctionDescription
zip-withMap function f over lists l1 and l2, until the shorter is exhausted
zipList of pairs of elements l1 and l2, until the shorter is exhausted
append(l1, l2)Concatenate two lists l1 and l2
prependConcatenate two lists with l1 after l2
concat(ls)Concatenate all lists in ls together
mapcat(f)Map items in l with f and concatenate the resulting lists
zip-apply(fs, vs)Apply fns in list fs to corresponding values in list vs, until shorter is exhausted

Splitting Lists

FunctionDescription
split-at(n, l)Split list into two at nth item and return pair
split-after(p?, l)Split list where p? becomes false and return pair
split-when(p?, l)Split list where p? becomes true and return pair
window(n, step, l)List of lists of sliding windows over list l of size n and offset step
partition(n)List of lists of non-overlapping segments of list l of size n
discriminate(pred, xs)Return pair of xs for which pred(_) is true and xs for which pred(_) is false

Folds and Scans

FunctionDescription
foldl(op, i, l)Left fold operator op over list l starting from value i
foldr(op, i, l)Right fold operator op over list l ending with value i
scanl(op, i, l)Left scan operator op over list l starting from value i
scanr(op, i, l)Right scan operator op over list l ending with value i

Predicates

FunctionDescription
all-true?(l)True if and only if all items in list l are true
all(p?, l)True if and only if all items in list l satisfy predicate p?
any-true?(l)True if and only if any items in list l are true
any(p?, l)True if and only if any items in list l satisfy predicate p?

Sorting

FunctionDescription
group-by(k, xs)Group xs by key function returning block of key to subgroups, maintains order
qsort(lt, xs)Sort xs using 'less-than' function lt
sort-numsSort list of numbers ascending (Rust-level intrinsic)
sort-strs(xs)Sort list of strings or symbols ascending
sort-zdts(xs)Sort list of zoned date-times ascending
sort-by(key-fn, cmp, xs)Sort list xs by key extracted with key-fn using comparator cmp
sort-by-num(key-fn)Sort list xs ascending by numeric key extracted with key-fn
sort-by-str(key-fn)Sort list xs ascending by string key extracted with key-fn
sort-by-zdt(key-fn)Sort list xs ascending by zoned date-time key extracted with key-fn

Sorting Examples

nums: [3, 1, 4, 1, 5] sort-nums          # [1, 1, 3, 4, 5]
words: ["banana", "apple", "cherry"] sort-strs  # ["apple", "banana", "cherry"]

people: [{name: "Zara" age: 30}, {name: "Alice" age: 25}]
by-name: people sort-by-str(.name)       # sorted by name
by-age: people sort-by-num(.age)         # sorted by age

Other

FunctionDescription
nth(n, l)Return nth item of list if it exists, otherwise error
(l !! r)Return nth item of list l if it exists, otherwise error. For arrays, n must be a coordinate list (e.g. [row, col]) and !! delegates to arr.get
count(l)Return count of items in list l
lastReturn last element of list l
over-sliding-pairs(f, l)Apply binary fn f to each overlapping pair in l to form new list
differencesCalculate difference between each overlapping pair in list of numbers l

Other

FunctionDescription
rotate(n, l)Rotate list l left by n positions
transpose(rows)Transpose a matrix (list of lists)
update-nth(n, f, l)Apply f to element at index n in list l.
Panics if n is out of range
update-first(p?, f, l)Apply f to the first element matching p? in list l.
Returns l unchanged if no element matches
reduce(op, l)Left fold with no initial value; uses first element as seed. Panics on empty list
tails(l)Return list of successive tails of l: [l, tail(l), tail(tail(l)), ...]
iota(n)Return infinite list of integers from n upwards
The natural numbers: [0, 1, 2, ...]
butlast(l)Return all elements of list l except the last
unzip(pairs)List of pairs to pair of lists. Inverse of zip
interleave(a, b)Alternate elements from lists a and b. When one is exhausted, the remainder of the other is appended
window-all(n, step, l)Like window but includes the final short chunk even if smaller than n
partition-all(n)List of lists of non-overlapping segments of l, including any final short chunk
group-consecutive-by(f, xs)Group consecutive elements where f returns equal values into sublists
group-consecutiveGroup consecutive equal elements into sublists
uniq(xs)Remove consecutive duplicates from a list
nub-by(key, xs)Remove duplicates from xs, keeping the first occurrence of each distinct key(x) value. Unlike uniq, works on non-consecutive duplicates
split-on(pred, xs)Split list xs into sublists, breaking on
elements that satisfy pred. Matching elements are discarded
running-maxRunning maximum over a number list.
[3, 1, 4, 1][3, 3, 4, 4]. (Rust-level intrinsic)
running-minRunning minimum over a number list.
[3, 1, 4, 1][3, 1, 1, 1]. (Rust-level intrinsic)
running-sumCumulative sum over a number list.
[3, 1, 4, 1][3, 4, 8, 9]. (Rust-level intrinsic)