Zyn0.1.0

Standard library

Every function Zyn has built in, generated from the list the type checker itself uses, so nothing is missing. They're written as Zyn signatures; a few things in them aren't syntax you write yourself:

  • fn Iterable<T>.map<T, R>(...) is a function called on a value: list.map { ... }. Iterable stands for List, MutableList and Range.
  • vararg items: T takes any number of arguments.
  • <N: Number> means Int or Float. <T: Comparable> means a type that can be compared with <: Int, Float, String, Char or Bool.
  • Several signatures under one name are overloads: the one that fits the arguments is used.

Functions called on a value also work in a pipe and as plain calls: list.sorted(), list |> sorted and sorted(list) are the same.

#Functions

Called on their own: println(x), sqrt(2.0). Also the constant PI.

fn panic(message: String): Nothing
Stops the program with an error. For bugs, not for failures you expect: use Result for those.
fn assert(condition: Bool): Unit
fn assert(condition: Bool, message: String): Unit
Stops the program (or fails the test) if condition is false.
fn assertEq<T>(actual: T, expected: T): Unit
Stops the program (or fails the test) if actual isn't equal to expected, and shows both.
fn print(value: Any?): Unit
Prints a value, without a line break.
fn println(): Unit
fn println(value: Any?): Unit
Prints a value and a line break (or only the line break).
fn args(): List<String>
The program's command-line arguments. See Command line.
fn listOf<T>(vararg items: T): List<T>
A read-only list of the items. The same as [a, b, c].
fn mutableListOf<T>(vararg items: T): MutableList<T>
A list that can change. When it starts empty, give the item type: mutableListOf<String>().
fn mutableMapOf<K, V>(): MutableMap<K, V>
A map that can change. Its types are learned from how it's used, or given: mutableMapOf<String, Int>().
fn sqrt<N: Number>(x: N): Float
The square root.
fn pow<A: Number, B: Number>(base: A, exponent: B): Float
base to the power of exponent.
fn abs<N: Number>(x: N): N
The number without its sign.
fn floor<N: Number>(x: N): Int
Rounds down to a whole number.
fn ceil<N: Number>(x: N): Int
Rounds up to a whole number.
fn round<N: Number>(x: N): Int
Rounds to the nearest whole number.
fn minOf<T: Comparable>(a: T, b: T): T
The smaller of two values.
fn maxOf<T: Comparable>(a: T, b: T): T
The larger of two values.
let PI: Float
π, 3.141592653589793.

#Lists and ranges

Iterable means any of List, MutableList and Range. Every one of these also works in a pipe (list |> filter { it > 2 }) and as a plain call (filter(list) { it > 2 }).

let size: Int
How many items the list has.
let indices: Range
0..<size: the indexes of the items.
fn Iterable<T>.map<T, R>(transform: (T) -> R): List<R>
A list of transform's result for each item.
fn Iterable<T>.mapIndexed<T, R>(transform: (Int, T) -> R): List<R>
Like map, but transform also gets each item's index.
fn Iterable<T>.flatMap<T, R>(transform: (T) -> List<R>): List<R>
Calls transform for each item and joins the lists it returns into one.
fn Iterable<T>.filter<T>(predicate: (T) -> Bool): List<T>
The items for which predicate is true.
fn Iterable<T>.forEach<T>(action: (T) -> Unit): Unit
Calls action for each item.
fn Iterable<T>.count<T>(): Int
fn Iterable<T>.count<T>(predicate: (T) -> Bool): Int
How many items there are, or how many match predicate.
fn Iterable<T>.any<T>(): Bool
fn Iterable<T>.any<T>(predicate: (T) -> Bool): Bool
True if there's at least one item, or one that matches predicate.
fn Iterable<T>.all<T>(predicate: (T) -> Bool): Bool
True if every item matches predicate (also when there are none).
fn Iterable<T>.none<T>(): Bool
fn Iterable<T>.none<T>(predicate: (T) -> Bool): Bool
True if there are no items, or none that match predicate.
fn Iterable<T>.find<T>(predicate: (T) -> Bool): T?
The first item that matches predicate, or null.
fn Iterable<T>.first<T>(): T
fn Iterable<T>.first<T>(predicate: (T) -> Bool): T
The first item, or the first that matches predicate. Stops the program if there's none.
fn Iterable<T>.last<T>(): T
fn Iterable<T>.last<T>(predicate: (T) -> Bool): T
The last item, or the last that matches predicate. Stops the program if there's none.
fn Iterable<T>.firstOrNull<T>(): T?
The first item, or null if there are none.
fn Iterable<T>.lastOrNull<T>(): T?
The last item, or null if there are none.
fn List<T>.indexOf<T>(item: T): Int
The index of the first item equal to item, or -1.
fn List<T>.contains<T>(item: T): Bool
True if an item is equal to item.
fn Range.contains(value: Int): Bool
True if value is in the range.
fn Iterable<T>.isEmpty<T>(): Bool
True if there are no items.
fn Iterable<T>.isNotEmpty<T>(): Bool
True if there's at least one item.
fn Iterable<N>.sum<N: Number>(): N
The sum of the numbers.
fn Iterable<T>.sumOf<T, N: Number>(selector: (T) -> N): N
The sum of selector's result for each item.
fn Iterable<N>.average<N: Number>(): Float
The average of the numbers.
fn Iterable<T>.min<T: Comparable>(): T
The smallest item. Stops the program if there are none.
fn Iterable<T>.max<T: Comparable>(): T
The largest item. Stops the program if there are none.
fn Iterable<T>.minBy<T, C: Comparable>(selector: (T) -> C): T
The item for which selector gives the smallest value.
fn Iterable<T>.maxBy<T, C: Comparable>(selector: (T) -> C): T
The item for which selector gives the largest value.
fn Iterable<T>.minOrNull<T: Comparable>(): T?
The smallest item, or null if there are none.
fn Iterable<T>.maxOrNull<T: Comparable>(): T?
The largest item, or null if there are none.
fn Iterable<T>.minByOrNull<T, C: Comparable>(selector: (T) -> C): T?
Like minBy, or null if there are no items.
fn Iterable<T>.maxByOrNull<T, C: Comparable>(selector: (T) -> C): T?
Like maxBy, or null if there are no items.
fn Iterable<T>.sorted<T: Comparable>(): List<T>
The items from smallest to largest.
fn Iterable<T>.sortedDescending<T: Comparable>(): List<T>
The items from largest to smallest.
fn Iterable<T>.sortedBy<T, C: Comparable>(selector: (T) -> C): List<T>
The items sorted by selector's result, smallest first.
fn Iterable<T>.sortedByDescending<T, C: Comparable>(selector: (T) -> C): List<T>
The items sorted by selector's result, largest first.
fn List<T>.reversed<T>(): List<T>
The items in the opposite order.
fn Range.reversed(): Range
The range backwards: (1..3).reversed() is 3, 2, 1.
fn Range.step(step: Int): Range
Every step-th number of the range: (0..10).step(5) is 0, 5, 10.
fn Iterable<T>.take<T>(count: Int): List<T>
The first count items (or all, if there are fewer).
fn Iterable<T>.drop<T>(count: Int): List<T>
All items but the first count.
fn Iterable<T>.chunked<T>(size: Int): List<List<T>>
The items in lists of size (the last one may be shorter).
fn Iterable<T>.distinct<T>(): List<T>
The items without repeats, in their first order.
fn List<List<T>>.flatten<T>(): List<T>
A list of lists joined into one list.
fn Iterable<T>.joinToString<T>(): String
fn Iterable<T>.joinToString<T>(separator: String): String
fn Iterable<T>.joinToString<T>(separator: String, transform: (T) -> Any?): String
The items as one string, with separator between them (", " if not given). transform turns each item into text.
fn Iterable<T>.fold<T, R>(initial: R, operation: (R, T) -> R): R
Starts with initial and combines it with each item in turn: [1, 2, 3].fold(0) { sum, x -> sum + x } is 6.
fn Iterable<T>.reduce<T>(operation: (T, T) -> T): T
Like fold, starting with the first item. Stops the program if there are none.
fn Iterable<T>.groupBy<T, K>(key: (T) -> K): Map<K, List<T>>
A map from each key to the items that have it.
fn Iterable<T>.toList<T>(): List<T>
The items in a read-only list.
fn Iterable<T>.toMutableList<T>(): MutableList<T>
The items in a new list that can change.
fn String.toList(): List<Char>
The string's characters.
fn String.toMutableList(): MutableList<Char>
The string's characters, in a list that can change.

#Mutable lists

Only for lists made with mutableListOf() or .toMutableList(). Items are also changed with list[i] = value.

fn MutableList<T>.add<T>(item: T): Unit
Adds item at the end.
fn MutableList<T>.addAll<T>(items: List<T>): Unit
Adds every item of items at the end.
fn MutableList<T>.insert<T>(index: Int, item: T): Unit
Puts item at index, moving the items after it along.
fn MutableList<T>.removeAt<T>(index: Int): T
Removes the item at index and returns it.
fn MutableList<T>.remove<T>(item: T): Bool
Removes the first item equal to item. True if there was one.
fn MutableList<T>.clear<T>(): Unit
Removes every item.

#Maps

Look values up with map[key], which gives null for a missing key. Lambdas get the key and the value: map.filter { key, value -> value > 1 }.

let size: Int
How many entries the map has.
let keys: List<K>
The keys, in the order they were added.
let values: List<V>
The values, in the order their keys were added.
fn Map<K, V>.containsKey<K, V>(key: K): Bool
True if the map has key.
fn Map<K, V>.contains<K, V>(key: K): Bool
True if the map has key (the same as containsKey).
fn Map<K, V>.getOrDefault<K, V>(key: K, default: V): V
The value for key, or default if there's none. map[key] ?? default does the same.
fn Map<K, V>.isEmpty<K, V>(): Bool
True if the map has no entries.
fn Map<K, V>.isNotEmpty<K, V>(): Bool
True if the map has at least one entry.
fn Map<K, V>.toMutableMap<K, V>(): MutableMap<K, V>
The entries in a new map that can change.
fn Map<K, V>.forEach<K, V>(action: (K, V) -> Unit): Unit
Calls action with each key and value.
fn Map<K, V>.map<K, V, R>(transform: (K, V) -> R): List<R>
A list of transform's result for each key and value.
fn Map<K, V>.filter<K, V>(predicate: (K, V) -> Bool): Map<K, V>
The entries for which predicate is true.
fn Map<K, V>.count<K, V>(): Int
fn Map<K, V>.count<K, V>(predicate: (K, V) -> Bool): Int
How many entries there are, or how many match predicate.
fn Map<K, V>.any<K, V>(predicate: (K, V) -> Bool): Bool
True if an entry matches predicate.
fn Map<K, V>.all<K, V>(predicate: (K, V) -> Bool): Bool
True if every entry matches predicate.

#Mutable maps

Only for maps made with mutableMapOf() or .toMutableMap(). Set values with map[key] = value.

fn MutableMap<K, V>.remove<K, V>(key: K): V?
Removes key and returns its value, or null if it wasn't there.
fn MutableMap<K, V>.clear<K, V>(): Unit
Removes every entry.

#Strings

Strings never change: these functions return new ones.

let length: Int
How many characters the string has.
let indices: Range
0..<length: the indexes of the characters.
fn String.uppercase(): String
The string in capital letters.
fn String.lowercase(): String
The string in small letters.
fn String.trim(): String
The string without spaces (and other whitespace) at the start and end.
fn String.isEmpty(): Bool
True if the string has no characters.
fn String.isNotEmpty(): Bool
True if the string has at least one character.
fn String.isBlank(): Bool
True if the string is empty or only whitespace.
fn String.contains(text: String): Bool
fn String.contains(char: Char): Bool
True if text (or char) is in the string.
fn String.startsWith(prefix: String): Bool
True if the string starts with prefix.
fn String.endsWith(suffix: String): Bool
True if the string ends with suffix.
fn String.indexOf(text: String): Int
Where text first appears in the string, or -1.
fn String.split(separator: String): List<String>
The parts of the string between each separator.
fn String.lines(): List<String>
The string's lines.
fn String.replace(old: String, new: String): String
The string with every old replaced by new.
fn String.reversed(): String
The characters in the opposite order.
fn String.repeat(times: Int): String
The string times times over: "ab".repeat(3) is "ababab".
fn String.substring(start: Int): String
fn String.substring(start: Int, end: Int): String
The characters from start up to (not including) end, or to the end of the string.
fn String.toIntOrNull(): Int?
The string as a whole number, or null if it isn't one.
fn String.toFloatOrNull(): Float?
The string as a number, or null if it isn't one.
fn String.toInt(): Result<Int, String>
The string as a whole number, as a Result: Err with a message if it isn't one.
fn String.toFloat(): Result<Float, String>
The string as a number, as a Result: Err with a message if it isn't one.

#Chars

let code: Int
The character's Unicode number: 'A'.code is 65.
fn Char.isDigit(): Bool
True for '0' to '9' (and other digits).
fn Char.isLetter(): Bool
True for letters.
fn Char.isWhitespace(): Bool
True for spaces, tabs and line breaks.
fn Char.uppercase(): Char
The character as a capital letter.
fn Char.lowercase(): Char
The character as a small letter.

#Numbers and conversions

Int is a 64-bit whole number, Float a 64-bit floating point number. They don't convert by themselves: use these.

fn Int.repeat(action: (Int) -> Unit): Unit
Calls action this many times, with 0, 1, 2, ...
fn Int.toFloat(): Float
The number as a Float.
fn Float.toInt(): Int
The number as an Int, without its fraction (rounds toward zero).
fn Int.toString(): String
The number as text.
fn Float.toString(): String
The number as text.
fn Bool.toString(): String
"true" or "false".
fn Char.toString(): String
The character as a string.

#Result

Result<T, E> is Ok(value) or Err(error); it's written in Zyn, in the prelude every program gets. See Errors.

fn Result<T, E>.isOk<T, E>(): Bool
True for an Ok.
fn Result<T, E>.isErr<T, E>(): Bool
True for an Err.
fn Result<T, E>.getOrElse<T, E>(default: T): T
The value of an Ok, or default for an Err.
fn Result<T, E>.getOrNull<T, E>(): T?
The value of an Ok, or null for an Err.
fn Result<T, E>.errorOrNull<T, E>(): E?
The error of an Err, or null for an Ok.
fn Result<T, E>.map<T, E, R>(transform: (T) -> R): Result<R, E>
Changes the value of an Ok; an Err stays as it is.
fn Result<T, E>.mapErr<T, E, F>(transform: (E) -> F): Result<T, F>
Changes the error of an Err; an Ok stays as it is.
fn Result<T, E>.andThen<T, E, R>(next: (T) -> Result<R, E>): Result<R, E>
Runs the next step (which can fail too) if this is an Ok.
fn Result<T, E>.unwrap<T, E>(): T
The value of an Ok. For an Err, it stops the program.