Threadon / Std lib

Std lib

the small standard library

What's in it

The standard library is a single Threadon file, stdlib/std.th, written in the language itself. It has six helper functions and one class used for looping:

Six Int32 helpers:

FunctionReturnsExample
abs(x)Int32, the absolute value of xabs(-5) → 5
max(a, b)Int32, the larger of the twomax(3, 7) → 7
min(a, b)Int32, the smaller of the twomin(3, 7) → 3
clamp(x, lo, hi)Int32, x forced between lo and hiclamp(10, 0, 5) → 5
is_even(n)Boolis_even(4) → True
is_odd(n)Boolis_odd(4) → False

range and Iterable

The one Int64 iterable maker, range, powers for loops:

CallYields
range(n)0, 1, 2, …, n-1
range(a, b)a, a+1, a+2, …, b-1
range(a, b, step)a, then plus step each time, stopping before b; a negative step counts down
def main() -> Int32
    for i in range(3):
        print(i)
    return 0
0 1 2

All three forms return an Iterable, a class with three methods — done(), value(), and advance() — that a for loop calls to walk through the sequence. Because iteration lives in an ordinary class, you can loop over your own classes too if you give them those methods. See Flow.

It's loaded automatically

The std lib is imported automatically into the main file of a program. You never write import std yourself; the functions are just available by their plain name.

def main() -> Int32
    n: Int32 = 127
    print(clamp(n, 0, 100))
    print(min(n, 50))
    return 0
100 50

Because it's a plain Threadon file, it follows every rule you've learned: functions before use, return types on all paths, NoneType with a bare return, and so on.

Inside modules

One catch: the automatic import only happens for the main file. If you're writing a module and want a std function, import it yourself. Both of these work inside a module:

from std import abs, clamp
import std   # then call std.abs(-5)

The built-ins print and input are not part of std.th; they're always available, in modules and main files alike.

You can shadow it

The std lib is found on the module search path like anything else. If you put a file named std.th somewhere earlier on the search path, it wins over the real one. The automatic import looks up std through the normal search, so a local std.th is picked up and its functions are what you get. Same for a folder: a std module on the path shadows the library. See Imports for how the search path works.