Skip to content

Appendix B: Standard Library Reference

Complete API Documentation

This appendix documents every function and method in Wyn's standard library. Use it as a reference when coding.

String Methods

Inspection

len() -> int Returns the length of the string in characters (not bytes).

wyn
"hello".len()  // 5

is_empty() -> bool Returns true if the string has zero length.

wyn
"".is_empty()  // true

contains(substring: string) -> bool Returns true if the string contains the substring.

wyn
"hello world".contains("world")  // true

starts_with(prefix: string) -> bool Returns true if the string starts with the prefix.

wyn
"hello".starts_with("hel")  // true

ends_with(suffix: string) -> bool Returns true if the string ends with the suffix.

wyn
"hello".ends_with("lo")  // true

Transformation

trim() -> string Returns a new string with leading and trailing whitespace removed.

wyn
"  hello  ".trim()  // "hello"

upper() -> string Returns a new string with all characters converted to uppercase.

wyn
"hello".upper()  // "HELLO"

lower() -> string Returns a new string with all characters converted to lowercase.

wyn
"HELLO".lower()  // "hello"

reverse() -> string Returns a new string with characters in reverse order.

wyn
"hello".reverse()  // "olleh"

replace(old: string, new: string) -> string Returns a new string with all occurrences of old replaced with new.

wyn
"hello world".replace("world", "wyn")  // "hello wyn"
"hello hello".replace("hello", "hi")   // "hi hi"

replace_all(old: string, new: string) -> string Alias for replace - replaces all occurrences.

wyn
"hello hello".replace_all("hello", "hi")  // "hi hi"

"str" * n (string repeat operator) Returns the string repeated n times.

wyn
"ha" * 3       // "hahaha"
"-" * 40       // 40 dashes

Searching

index_of(substring: string) -> int Returns the index of the first occurrence, or -1 if not found.

wyn
"hello".index_of("l")  // 2
"hello".index_of("x")  // -1

last_index_of(substring: string) -> int Returns the index of the last occurrence, or -1 if not found.

wyn
"hello".last_index_of("l")  // 3

Splitting and Joining

split(delimiter: string) -> [string] Splits the string by delimiter and returns an array of parts.

wyn
"a,b,c".split(",")  // ["a", "b", "c"]

[string].join(separator: string) -> string Joins array elements with separator.

wyn
["a", "b", "c"].join(",")  // "a,b,c"

Substring

substring(start: int) -> string Returns substring from start to end.

wyn
"hello".substring(2)  // "llo"

substring(start: int, end: int) -> string Returns substring from start to end (exclusive).

wyn
"hello".substring(1, 4)  // "ell"

Parsing

parse_int() -> Option<int> Parses the string as an integer.

wyn
"42".parse_int()   // some(42)
"abc".parse_int()  // none

parse_float() -> Option<float> Parses the string as a float.

wyn
"3.14".parse_float()  // some(3.14)

Integer Methods

abs() -> int Returns the absolute value.

wyn
(-42).abs()  // 42

to_string() -> string Converts to string.

wyn
42.to_string()  // "42"

to_float() -> float Converts to float.

wyn
42.to_float()  // 42.0

Float Methods

abs() -> float Returns the absolute value.

wyn
(-3.14).abs()  // 3.14

round() -> float Rounds to nearest integer.

wyn
3.7.round()  // 4.0

floor() -> float Rounds down.

wyn
3.7.floor()  // 3.0

ceil() -> float Rounds up.

wyn
3.2.ceil()  // 4.0

to_string() -> string Converts to string.

wyn
3.14.to_string()  // "3.14"

to_int() -> int Converts to integer (truncates).

wyn
3.7.to_int()  // 3

Array Methods

len() -> int Returns the number of elements.

wyn
[1, 2, 3].len()  // 3

is_empty() -> bool Returns true if the array has no elements.

wyn
[].is_empty()  // true

push(item: T) -> int Appends item to the end.

wyn
var arr = [1, 2]
arr.push(3)  // arr is now [1, 2, 3]

contains(item: T) -> bool Returns true if the array contains item.

wyn
[1, 2, 3].contains(2)  // true

first() -> Option<T> Returns the first element, or none if empty.

wyn
[1, 2, 3].first()  // some(1)
[].first()         // none

last() -> Option<T> Returns the last element, or none if empty.

wyn
[1, 2, 3].last()  // some(3)

map(f: fn(T) -> U) -> [U] Applies function to each element.

wyn
[1, 2, 3].map((x) => x * 2)  // [2, 4, 6]

filter(f: fn(T) -> bool) -> [T] Keeps elements where function returns true.

wyn
[1, 2, 3, 4].filter((x) => x % 2 == 0)  // [2, 4]

reduce(f: fn(T, T) -> T, initial: T) -> T Combines elements using function.

wyn
[1, 2, 3].reduce((a, b) => a + b, 0)  // 6

HashMap Methods

has(key: K) -> bool Returns true if the key exists.

wyn
var map = {"a": 1}
map.has("a")  // true

len() -> int Returns the number of entries.

wyn
{"a": 1, "b": 2}.len()  // 2

remove(key: K) -> int Removes the entry with the given key.

wyn
var map = {"a": 1, "b": 2}
map.remove("a")

clear() -> int Removes all entries.

wyn
var map = {"a": 1}
map.clear()

HashSet Methods

contains(item: T) -> bool Returns true if the set contains item.

wyn
{:"a", "b"}.contains("a")  // true

insert(item: T) -> int Adds item to the set.

wyn
var set = {:"a"}
set.insert("b")

remove(item: T) -> int Removes item from the set.

wyn
var set = {:"a", "b"}
set.remove("a")

len() -> int Returns the number of elements.

wyn
{:"a", "b", "c"}.len()  // 3

clear() -> int Removes all elements.

wyn
var set = {:"a", "b"}
set.clear()

File I/O Functions

File.read(path: string) -> string Reads entire file as string (empty string if unreadable - check File.exists first).

wyn
var contents = File.read("data.txt")

File.write(path: string, contents: string) -> int Writes string to file, overwriting if exists.

wyn
File.write("output.txt", "hello")

File.append(path: string, contents: string) -> int Appends string to file.

wyn
File.append("log.txt", "new entry\n")

File.exists(path: string) -> bool Returns true if file exists.

wyn
File.exists("data.txt")

File.file_size(path: string) -> int Returns file size in bytes.

wyn
File.file_size("data.txt")

File.list_dir(path: string) -> [string] Lists files in directory.

wyn
var files = File.list_dir(".")

File.mkdir(path: string) -> int Creates directory.

wyn
File.mkdir("new_folder")

File.rmdir(path: string) -> int Removes directory.

wyn
File.rmdir("old_folder")

HTTP Functions

Http.get(url: string) -> string Performs HTTP GET request and returns the response body (empty on failure).

wyn
var body = Http.get("https://api.example.com/data")

Http.post(url: string, body: string) -> string Performs HTTP POST request and returns the response body.

wyn
var resp = Http.post("https://api.example.com/users", json_data)

JSON Functions

Json.parse(text: string) -> Json Parses JSON string.

wyn
var json = Json.parse("{\"name\": \"Alice\"}")
var name = json.get("name")

Json.stringify(value) -> string Converts value to JSON string.

wyn
var json_str = Json.stringify({"name": "Alice"})

Time Functions

time_now() -> int Returns current Unix timestamp.

wyn
var now = time_now()

time_format(timestamp: int, format: string) -> string Formats timestamp as string.

wyn
time_format(now, "%Y-%m-%d %H:%M:%S")

Time.now() -> int Namespaced form of time_now(); Time.now_millis() gives milliseconds.

wyn
var now = Time.now()

Time.sleep(milliseconds: int) Sleeps for specified milliseconds (also available as sleep(ms)).

wyn
Time.sleep(1000)  // Sleep 1 second

System Functions

System.args() -> [string] Returns command-line arguments.

wyn
var args = System.args()

System.env(name: string) -> string Gets environment variable (empty string if unset).

wyn
var home = System.env("HOME")

System.set_env(name: string, value: string) Sets environment variable.

wyn
System.set_env("MY_VAR", "value")

System.load_env(path: string) Loads variables from a .env file.

wyn
System.load_env(".env")

System.exec(command: string) -> string Executes shell command and returns its output.

wyn
var out = System.exec("ls -la")

Math Functions

All under the Math namespace (use Math:: or Math.):

Math::sqrt(x: float) -> float Square root.

wyn
Math::sqrt(16.0)  // 4.0

Math::pow(base: float, exp: float) -> float Power.

wyn
Math::pow(2.0, 8.0)  // 256.0

Math::abs(x: float) -> float Absolute value.

wyn
Math::abs(-42.0)  // 42.0

Math::min(a: float, b: float) -> float Minimum of two values.

wyn
Math::min(5.0, 3.0)  // 3.0

Math::max(a: float, b: float) -> float Maximum of two values.

wyn
Math::max(5.0, 3.0)  // 5.0

Math::sin(x: float) -> float Sine.

wyn
Math::sin(3.14159 / 2)  // 1.0

Math::cos(x: float) -> float Cosine.

wyn
Math::cos(0.0)  // 1.0

Math::tan(x: float) -> float Tangent.

wyn
Math::tan(3.14159 / 4)  // 1.0

Also available: Math::floor, Math::ceil, Math::round, Math::round_to, Math::atan2, Math::pi(), Math::e().

Math::random() -> float Random float between 0.0 and 1.0.

wyn
Math::random()

random_int(min: int, max: int) -> int Random integer between min (inclusive) and max (exclusive).

wyn
random_int(0, 100)

random_float() -> float Random float between 0.0 and 1.0.

wyn
random_float()

Concurrency Functions

channel(capacity: int) Creates a buffered channel. The element type is inferred from the first send.

wyn
var ch = channel(10)

Channel methods:

send(value) Sends value through channel (blocks if full).

wyn
ch.send(42)

recv() Receives value from channel (blocks if empty).

wyn
var value = ch.recv()

close() Closes the channel.

wyn
ch.close()

select { } Waits on multiple channels and runs the arm whose channel is ready.

wyn
select {
    v = a.recv() => println("from a")
    v = b.recv() => println("from b")
}

print(value: T) -> int Prints value to stdout. Works with any type.

wyn
print(42)
print("hello")
print([1, 2, 3])

This covers the complete standard library. For usage examples, see Chapter 14.

MIT License - v1.19.1