Library
The standard library
The prelude that ships with the compiler, written in the language itself and in scope in every program.
since 0.1.0-alpha.1linuxwindowswasm
Description
The standard library is called the prelude. It is a set of ordinary source files written in Leviathan, on top of a small set of native operations, and it ships inside the compiler. You do not install it and you do not import it. Every program is compiled together with it, and every name it declares is in scope from the first line.
The prelude lives in the std namespace, and std is implicitly imported: it behaves as if every
file began with uses std;. You can write Array, Map or sum(xs), and you can equally write
the qualified form std::sum(xs). Some parts are placed in their own top-level namespaces with
short names, notably math (math::pi), regex and env. Because the prelude is written in the
language, you can read its sources as examples of ordinary Leviathan code.
One part of the prelude is only present for one target. The browser interface of the WebAssembly target is loaded when you build for a WebAssembly target and is invisible to native builds.
A little of everything, with no imports
Array<string> words = ["pear", "fig", "apple"];
Map<string, int> lengths = Map();
for (string w in words) { lengths = lengths.with(w, w.length()); }
console.writeln(lengths);
Set<int> sizes = Set(lengths.values());
console.writeln(sizes.length());
console.writeln(math::max(lengths.at("fig"), lengths.at("pear")));
Regex vowels = Regex("[aeiou]");
console.writeln(vowels.count("apple"));
StringBuilder sb = StringBuilder();
for (int i in 1..3) { sb.add(i.toString()); }
console.writeln(sb.toString());
{pear: 4, fig: 3, apple: 5}
3
4
2
123
Where things are
This chapter's companion pages cover the core types.
- Collections.
Array<T>,Map<K, V>andSet<T>are pure values; seelang.collection-value-semanticsfor the shared rule.Rangeis the value ofa..b.PairandTripleare small value types, andOrderedArrayis the result of a multi-key sort. - Iteration. The
IIterableandIIteratorinterfaces and thefor-inrules are inlang.iterator-protocol, andSeqis the lazy pipeline built on them. - Text tools.
StringBuilderaccumulates a long string, andRegexwith theregexnamespace does pattern matching in time linear in the input. - Numbers. The
mathnamespace holds the constants and the transcendental functions.
The rest of the library has its own pages: the primitive types with their methods, Promise and
tasks, streams and files, networking, Console, byte buffers (Block), processes and terminals.
The generated library reference lists every type and method with its signature and examples.
Rules
- No import is needed to use the prelude;
stdis implicitly in scope. - A prelude name can be written bare (
sum) or qualified (std::sum). math,regexandenvare namespaces of their own and are written with their own prefix:math::pi,regex::isMatch(...).- Do not give your own classes the names of the built-in collections (
Array,Map,Range) if you iterate over them withfor-in: the loop recognises the built-in types by name.
Examples
Bare and qualified names
console.writeln(sum([1, 2, 3]));
console.writeln(std::sum([1, 2, 3]));
console.writeln(std::min([5, 3]));
Pair<int, int> p = Pair::Of(1, 2);
console.writeln(p.toString());
console.writeln(math::max(1, 2));
6
6
3
(1, 2)
2
See also
- Array — An ordered sequence of values of one type,
Array<T>, with value semantics. - Map — An associative collection from keys of type
Kto values of typeV, with value semantics. - Set — A collection of distinct values of type
T, with value semantics. - Range — A run of consecutive integers written
a..b, including both ends. - Pair — A pair of two values, possibly of different types.
- Triple — A group of three values, possibly of different types.
- StringBuilder — A mutable accumulator for building a string from many pieces.
- OrderedArray — An array that has been sorted by one or more keys, produced by
Array.orderByandthenBy. - Seq — A lazy sequence: a pipeline of steps that does no work until its result is requested.
- Regex — A compiled regular expression.
- math — Mathematical constants and functions on
float. - Promise — A value that arrives later.
- File — An open file.
- Console — The program's standard output as an object.
- Block — A fixed-length, mutable buffer of bytes.
- Collections are values — Array, Map and Set never change in place; every changing method returns a new collection, and the idiom for change is to rebind.
- The iterator protocol — IIterable and IIterator — Two interfaces that make any type usable in a
for-inloop, and the rules for which loop path the compiler picks.