Built-in library
Every Swan program starts with these names already bound:
| Name | Contents |
|---|---|
| Root names | TRUE, FALSE, type, parse, bind, label |
bool | Converting and combining truth values |
numb | Converting values to numbers |
text | Measuring, searching, slicing, splitting text |
list | Measuring, searching and slicing lists |
nspace | Inspecting namespaces |
math | Mathematical constants and functions |
time | Timestamps and calendar dates |
promise | Creating and resolving promises |
undef | Creating and inspecting undefined values |
In a template, the engine also binds texl, which imports other
documents, and the host can bind its own names (see
Names and data).
Reading the reference
Each entry starts with a signature that shows how to call the function and what it returns:
text.find needle haystack -> Numb
This reads: apply text.find to needle, then apply the result to
haystack, and you get a number. The description says what types the
arguments must have.
Curried functions
Functions with two parameters are curried: they take the first argument and return a function that takes the second. You can call them in one go, or keep the intermediate function:
text.find "l" "hello" # => 2
find_l = text.find "l"
find_l "world" # => 3
The first argument is the one you are most likely to fix in advance, such as
the separator of text.split or the exponent of math.pow. Note that
text.find ("l", "hello") passes a single tuple, not two arguments, and does
not work.
Tuples
Unless an entry says otherwise, functions are applied to each item of a tuple
argument and return a tuple of results. A Nothing argument gives Nothing.
text.upper ("a", "b") # => ("A", "B")
text.find ("l", "o") "hello" # => (2, 4)
text.upper () # => ()
Functions that reduce a tuple to one value, such as bool.all and math.max,
say so in their entry.
Errors
A function given an argument of the wrong type returns Undefined. The
undefined value names the function in its op member.
text.size 42 # => Undefined
(undef.inspect (text.size 42)).op # => "text.size"