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).
"hello".len() // 5is_empty() -> bool Returns true if the string has zero length.
"".is_empty() // truecontains(substring: string) -> bool Returns true if the string contains the substring.
"hello world".contains("world") // truestarts_with(prefix: string) -> bool Returns true if the string starts with the prefix.
"hello".starts_with("hel") // trueends_with(suffix: string) -> bool Returns true if the string ends with the suffix.
"hello".ends_with("lo") // trueTransformation
trim() -> string Returns a new string with leading and trailing whitespace removed.
" hello ".trim() // "hello"upper() -> string Returns a new string with all characters converted to uppercase.
"hello".upper() // "HELLO"lower() -> string Returns a new string with all characters converted to lowercase.
"HELLO".lower() // "hello"reverse() -> string Returns a new string with characters in reverse order.
"hello".reverse() // "olleh"replace(old: string, new: string) -> string Returns a new string with all occurrences of old replaced with new.
"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.
"hello hello".replace_all("hello", "hi") // "hi hi""str" * n (string repeat operator) Returns the string repeated n times.
"ha" * 3 // "hahaha"
"-" * 40 // 40 dashesSearching
index_of(substring: string) -> int Returns the index of the first occurrence, or -1 if not found.
"hello".index_of("l") // 2
"hello".index_of("x") // -1last_index_of(substring: string) -> int Returns the index of the last occurrence, or -1 if not found.
"hello".last_index_of("l") // 3Splitting and Joining
split(delimiter: string) -> [string] Splits the string by delimiter and returns an array of parts.
"a,b,c".split(",") // ["a", "b", "c"][string].join(separator: string) -> string Joins array elements with separator.
["a", "b", "c"].join(",") // "a,b,c"Substring
substring(start: int) -> string Returns substring from start to end.
"hello".substring(2) // "llo"substring(start: int, end: int) -> string Returns substring from start to end (exclusive).
"hello".substring(1, 4) // "ell"Parsing
parse_int() -> Option<int> Parses the string as an integer.
"42".parse_int() // some(42)
"abc".parse_int() // noneparse_float() -> Option<float> Parses the string as a float.
"3.14".parse_float() // some(3.14)Integer Methods
abs() -> int Returns the absolute value.
(-42).abs() // 42to_string() -> string Converts to string.
42.to_string() // "42"to_float() -> float Converts to float.
42.to_float() // 42.0Float Methods
abs() -> float Returns the absolute value.
(-3.14).abs() // 3.14round() -> float Rounds to nearest integer.
3.7.round() // 4.0floor() -> float Rounds down.
3.7.floor() // 3.0ceil() -> float Rounds up.
3.2.ceil() // 4.0to_string() -> string Converts to string.
3.14.to_string() // "3.14"to_int() -> int Converts to integer (truncates).
3.7.to_int() // 3Array Methods
len() -> int Returns the number of elements.
[1, 2, 3].len() // 3is_empty() -> bool Returns true if the array has no elements.
[].is_empty() // truepush(item: T) -> int Appends item to the end.
var arr = [1, 2]
arr.push(3) // arr is now [1, 2, 3]contains(item: T) -> bool Returns true if the array contains item.
[1, 2, 3].contains(2) // truefirst() -> Option<T> Returns the first element, or none if empty.
[1, 2, 3].first() // some(1)
[].first() // nonelast() -> Option<T> Returns the last element, or none if empty.
[1, 2, 3].last() // some(3)map(f: fn(T) -> U) -> [U] Applies function to each element.
[1, 2, 3].map((x) => x * 2) // [2, 4, 6]filter(f: fn(T) -> bool) -> [T] Keeps elements where function returns true.
[1, 2, 3, 4].filter((x) => x % 2 == 0) // [2, 4]reduce(f: fn(T, T) -> T, initial: T) -> T Combines elements using function.
[1, 2, 3].reduce((a, b) => a + b, 0) // 6HashMap Methods
has(key: K) -> bool Returns true if the key exists.
var map = {"a": 1}
map.has("a") // truelen() -> int Returns the number of entries.
{"a": 1, "b": 2}.len() // 2remove(key: K) -> int Removes the entry with the given key.
var map = {"a": 1, "b": 2}
map.remove("a")clear() -> int Removes all entries.
var map = {"a": 1}
map.clear()HashSet Methods
contains(item: T) -> bool Returns true if the set contains item.
{:"a", "b"}.contains("a") // trueinsert(item: T) -> int Adds item to the set.
var set = {:"a"}
set.insert("b")remove(item: T) -> int Removes item from the set.
var set = {:"a", "b"}
set.remove("a")len() -> int Returns the number of elements.
{:"a", "b", "c"}.len() // 3clear() -> int Removes all elements.
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).
var contents = File.read("data.txt")File.write(path: string, contents: string) -> int Writes string to file, overwriting if exists.
File.write("output.txt", "hello")File.append(path: string, contents: string) -> int Appends string to file.
File.append("log.txt", "new entry\n")File.exists(path: string) -> bool Returns true if file exists.
File.exists("data.txt")File.file_size(path: string) -> int Returns file size in bytes.
File.file_size("data.txt")File.list_dir(path: string) -> [string] Lists files in directory.
var files = File.list_dir(".")File.mkdir(path: string) -> int Creates directory.
File.mkdir("new_folder")File.rmdir(path: string) -> int Removes directory.
File.rmdir("old_folder")HTTP Functions
Http.get(url: string) -> string Performs HTTP GET request and returns the response body (empty on failure).
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.
var resp = Http.post("https://api.example.com/users", json_data)JSON Functions
Json.parse(text: string) -> Json Parses JSON string.
var json = Json.parse("{\"name\": \"Alice\"}")
var name = json.get("name")Json.stringify(value) -> string Converts value to JSON string.
var json_str = Json.stringify({"name": "Alice"})Time Functions
time_now() -> int Returns current Unix timestamp.
var now = time_now()time_format(timestamp: int, format: string) -> string Formats timestamp as string.
time_format(now, "%Y-%m-%d %H:%M:%S")Time.now() -> int Namespaced form of time_now(); Time.now_millis() gives milliseconds.
var now = Time.now()Time.sleep(milliseconds: int) Sleeps for specified milliseconds (also available as sleep(ms)).
Time.sleep(1000) // Sleep 1 secondSystem Functions
System.args() -> [string] Returns command-line arguments.
var args = System.args()System.env(name: string) -> string Gets environment variable (empty string if unset).
var home = System.env("HOME")System.set_env(name: string, value: string) Sets environment variable.
System.set_env("MY_VAR", "value")System.load_env(path: string) Loads variables from a .env file.
System.load_env(".env")System.exec(command: string) -> string Executes shell command and returns its output.
var out = System.exec("ls -la")Math Functions
All under the Math namespace (use Math:: or Math.):
Math::sqrt(x: float) -> float Square root.
Math::sqrt(16.0) // 4.0Math::pow(base: float, exp: float) -> float Power.
Math::pow(2.0, 8.0) // 256.0Math::abs(x: float) -> float Absolute value.
Math::abs(-42.0) // 42.0Math::min(a: float, b: float) -> float Minimum of two values.
Math::min(5.0, 3.0) // 3.0Math::max(a: float, b: float) -> float Maximum of two values.
Math::max(5.0, 3.0) // 5.0Math::sin(x: float) -> float Sine.
Math::sin(3.14159 / 2) // 1.0Math::cos(x: float) -> float Cosine.
Math::cos(0.0) // 1.0Math::tan(x: float) -> float Tangent.
Math::tan(3.14159 / 4) // 1.0Also 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.
Math::random()random_int(min: int, max: int) -> int Random integer between min (inclusive) and max (exclusive).
random_int(0, 100)random_float() -> float Random float between 0.0 and 1.0.
random_float()Concurrency Functions
channel(capacity: int) Creates a buffered channel. The element type is inferred from the first send.
var ch = channel(10)Channel methods:
send(value) Sends value through channel (blocks if full).
ch.send(42)recv() Receives value from channel (blocks if empty).
var value = ch.recv()close() Closes the channel.
ch.close()select { } Waits on multiple channels and runs the arm whose channel is ready.
select {
v = a.recv() => println("from a")
v = b.recv() => println("from b")
}Print Function
print(value: T) -> int Prints value to stdout. Works with any type.
print(42)
print("hello")
print([1, 2, 3])This covers the complete standard library. For usage examples, see Chapter 14.