Builtins Reference

Every built-in function, organized by category.

Arithmetic

The standard math operations. These work on both integers and floats, though mixing the two will coerce to float.

+

Add two or more numbers.

-

Subtract. With one argument, negates.

*

Multiply two or more numbers.

/

Divide. Integer division for ints, float division for floats.

%

Modulo (remainder after division).

Comparison

Equality checks work on any type using structural comparison. Ordering comparisons work on numbers and strings.

=

Structural equality. Works on all types.

!=

Structural inequality.

<

Less than. Numbers and strings.

>

Greater than. Numbers and strings.

<=

Less than or equal.

>=

Greater than or equal.

Logic

One important caveat here: and and or are eager builtins, not short-circuiting special forms. Both arguments get evaluated before the logical operation happens. If you need short-circuit behavior, use nested if expressions instead.

not

Logical negation. Returns true if the argument is false.

and

Logical AND. Eager; evaluates both arguments.

or

Logical OR. Eager; evaluates both arguments.

String

Loon has a solid set of string operations. str pulls double duty as both a converter (turn anything into a string) and a concatenator (join multiple values into one string). contains? also works on both strings and collections, so you do not need separate functions.

str

Convert to string, or concatenate multiple values into a string.

len

Return the length of a string or collection.

split

Split a string by a delimiter: [split "," "a,b,c"]

join

Join a collection into a string with a separator: [join ", " items]

trim

Remove leading and trailing whitespace.

replace

Replace occurrences in a string: [replace "old" "new" s]

uppercase

Convert a string to uppercase.

lowercase

Convert a string to lowercase.

starts-with?

Return true if a string starts with the given prefix.

ends-with?

Return true if a string ends with the given suffix.

char-at

Return the character at a given index: [char-at 0 s]

substring

Extract a substring: [substring start end s]

index-of

Return the index of a substring, or -1 if not found.

contains?

Return true if a string contains a substring, or a collection contains an element.

Collections

This is the big one. Loon's collection functions follow a consistent pattern: the collection is usually the last argument, which makes them work nicely with pipe. You will find the usual functional favorites here like map, filter, and fold, along with plenty of convenience functions for sorting, grouping, and slicing.

map

Apply a function to each element: [map f coll]

filter

Keep elements where the predicate returns true: [filter f coll]

fold

Reduce a collection: [fold coll init f] or [fold init f coll]

conj

Add an element to a collection: [conj coll elem]

nth

Get element at index: [nth idx coll]

get

Get a value by key, with optional default: [get m :key default]

assoc

Associate a key with a value in a map: [assoc m :key val]

update

Update a value in a map by applying a function: [update m :key f]

keys

Return the keys of a map as a vector.

values

Return the values of a map as a vector.

entries

Return key-value pairs of a map as a vector of vectors.

merge

Merge two or more maps. Later values win.

remove

Remove elements matching a predicate, or a key from a map.

sort

Sort a collection in natural order.

sort-by

Sort by a key function: [sort-by f coll]

reverse

Reverse a collection.

take

Take the first n elements: [take n coll]

drop

Drop the first n elements: [drop n coll]

each

Execute a side-effecting function for each element. Returns None.

find

Return the first element matching a predicate, or None.

any?

Return true if any element matches the predicate.

all?

Return true if all elements match the predicate.

zip

Combine two collections element-wise into pairs.

flatten

Flatten a nested collection by one level.

chunk

Split a collection into groups of n: [chunk n coll]

flat-map

Map then flatten: [flat-map f coll]

group-by

Group elements by a key function: [group-by f coll]

cons

Prepend an element to a list: [cons elem coll]

collect

Collect a lazy sequence into a vector.

into-map

Convert a collection of key-value pairs into a map.

range

Generate a range of integers: [range end] or [range start end]

push!

Mutably append to a vector. Use sparingly.

empty?

Return true if a collection or string is empty.

sum

Sum all numbers in a collection.

min

Return the smallest value in a collection.

max

Return the largest value in a collection.

Type Checks

Quick type predicates. These return a boolean and are useful for writing generic code that needs to branch on the shape of its input.

map?

Return true if the value is a map.

vec?

Return true if the value is a vector.

Conversion

Convert values between types. name is particularly handy for turning keywords into plain strings, which comes up a lot when you are building output from map keys.

name

Convert a keyword to its string name: [name :foo] "foo"

int

Convert a value to an integer.

float

Convert a value to a float.

str

Convert any value to its string representation.

IO

The basics for printing output. For more advanced IO like reading files or making network requests, see the Standard Effects reference.

println

Print a value followed by a newline.

print

Print a value without a trailing newline.

Physics

Dimensional analysis builtins. unit creates dimensional values, magnitude extracts the raw number, and scalar wraps a float as a dimensionless Scalar.

unit : Number -> Keyword -> Dim

Create a dimensional value: [unit 5.0 :m] or use literal syntax 5.0m. Supports 30+ units with SI prefixes.

magnitude : Dim -> Float

Extract the raw float from a dimensional value. This is the explicit exit from the physics type world.

scalar : Float -> Scalar

Wrap a float as a dimensionless Scalar. This is the explicit entry into the physics type world.

Physics Constants

Fundamental physical constants, namespaced under Const. These return dimensional values with the correct types.

Const.c

Speed of light: 299,792,458 m/s (Velocity)

Const.G

Gravitational constant: 6.674e-11 m³/(kg·s²)

Const.h

Planck constant: 6.626e-34 J·s (Energy × Time)

Const.k-B

Boltzmann constant: 1.381e-23 J/K

Const.e-charge

Elementary charge: 1.602e-19 C (Charge)