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

Functional collections

The central functional transforms are data-last.

(|> [1 2 3 4 5]
    (map (lambda (x) (* x x)))
    (select (lambda (x) (> x 10)))
    sum)

Transform and traverse

FunctionRole
map / map/ibuild a new vector; /i receives the index
pairwisebuild overlapping adjacent {left right} tuples
map/adjacenttransform each neighboring pair directly
filter, select, filter/iretain items satisfying a predicate
each / each/ivisit items and return the original vector
for / for/ivisit items for effects
reduce / reduce/ifold with an explicit initial accumulator
map/until, reduce/until, for/untilstop according to a predicate
(reduce (lambda (total x) (+ total x)) 0 [10 20 30])
; 60

(map/i (lambda (x index) {index x}) ["a" "b"])

pairwise is data-last and returns one fewer tuple than its input. It is empty for inputs shorter than two elements.

(|> [1 4 9 16]
    pairwise
    (map (lambda ({left right}) (- right left))))
; [3 5 7]

Construct and order

range, range/inclusive, and related helpers construct numeric sequences. sort returns a sorted copy; Vector/sort! sorts a vector in place. Use a comparison predicate that expresses the desired ordering.

(sort (lambda (left right) (< left right)) [5 1 4 2])

Ord T packages a reusable comparison for one value type, and sort/ord uses it. You do not need a sig to define an order in ordinary code: the constructor and its comparison infer together.

(sort/ord Ord/Int [5 1 4 2])
; [1 2 4 5]

(record Point y Int x Int)
(let Point/row-major
  (Ord
    (lambda (a b)
      (or (< (Point/y a) (Point/y b))
          (and (= (Point/y a) (Point/y b))
               (< (Point/x a) (Point/x b)))))))

(sort/ord Point/row-major [(Point 1 0) (Point 0 4)])
; [Point{ y 0 x 4 } Point{ y 1 x 0 }]

The ordered collection is simply [Point]; there is no need for a separate Points wrapper. Ord/Int and Ord/Dec are the standard ascending orders.

For allocation-sensitive code, prefer explicit loops and a reserved output vector when the result length is known. Pipeline fusion may optimize common map/filter/reduce shapes, but clear source invariants remain the first goal.

Numeric aggregates

The library has aggregate and statistical helpers including sum, product, avg, mean, median, minimum, and maximum, with integer and decimal variants where relevant. Query the exact expected element type rather than relying on a name alone:

que --lib types 'sum*'
que --lib types 'avg*'