Strings, Arrays & Maps
Strings, arrays, and maps are built into the VM. Their methods are available on every value without an import.
Strings
Section titled “Strings”Strings are immutable byte strings. size(), indexing, iteration, reverse(), and split("") are byte-based, not character-based.
| Method | Returns | Description |
|---|---|---|
.size() |
int |
Byte count |
.to_num() |
int | float | none |
Parse a number; int, float, or none |
.slice(start: int, end: int) |
string |
Substring; negative start clamps to 0 |
.slice(range: range) |
string |
Substring from a range with an exclusive end |
.is_digit() |
bool |
All bytes are ASCII digits |
.is_alpha() |
bool |
All bytes are ASCII letters |
.is_alnum() |
bool |
All bytes are ASCII letters or digits |
.is_space() |
bool |
All bytes are ASCII whitespace |
.is_upper() |
bool |
Every letter is uppercase; non-letters are ignored; an empty string is false |
.is_lower() |
bool |
Every letter is lowercase; non-letters are ignored; an empty string is false |
.join(arr: array) |
string |
Join values with this string as the delimiter |
.split(delimiter: string?) |
array |
Split by a delimiter; an empty delimiter splits into bytes. With no argument, split on runs of whitespace |
.count(sub: string) |
int |
Count non-overlapping occurrences of sub |
.lower() |
string |
Lowercase copy |
.upper() |
string |
Uppercase copy |
.reverse() |
string |
Byte-reversed copy |
.to_bytes() |
bytes |
Copy the bytes into bytes |
.trim() |
string |
Strip leading and trailing whitespace |
.ltrim() |
string |
Strip leading whitespace |
.rtrim() |
string |
Strip trailing whitespace |
.contains(sub: string) |
bool |
True if sub occurs |
.index_of(sub: string, start: int?) |
int? |
Index of the first occurrence at or after start, or none |
.last_index_of(sub: string) |
int? |
Index of the last occurrence, or none |
.repeat(count: int) |
string |
Repeat the string count times; a negative count raises ArgumentError |
.ord() |
int |
First byte as an int; an empty string raises ArgumentError |
.starts_with(prefix: string) |
bool |
Prefix test |
.ends_with(suffix: string) |
bool |
Suffix test |
.replace(old: string, new: string) |
string |
Replace all non-overlapping occurrences |
let s = " Hello, Pyle "
print(s.trim()) # Hello, Pyleprint(s.trim().upper()) # HELLO, PYLEprint("a,b,c".split(",")) # ["a", "b", "c"]print("a b c".split()) # ["a", "b", "c"]print("-".join([1, 2, 3])) # 1-2-3print("abc".slice(1, 3)) # bcprint("abc".slice(1..3)) # bcprint("hello".contains("ell"))print("hello".replace("l", "L"))print("banana".count("an")) # 2print("hello".reverse()) # olleh
let upper = "H".ord() # 72let c = chr(65) # "A"Indexing returns a one-byte string:
print("hello"[0]) # hSlice clamping is shared with arrays: a negative start becomes 0, end is clamped to the length, and start >= end yields an empty result. "hello".slice(1, -1) is "".
There is no * repetition operator and no implicit concatenation. Use +, and repeat(n) for repetition.
Arrays
Section titled “Arrays”Arrays are heterogeneous and resizable.
| Method | Returns | Description |
|---|---|---|
.append(value...) |
none |
Add one or more values to the end |
.size() |
int |
Element count |
.pop(count: int?) |
any |
Without count, remove and return the last element; with it, return the last count as a new array |
.reverse() |
none |
Reverse in place |
.slice(start: int, end: int) |
array |
New sub-array |
.slice(range: range) |
array |
New sub-array from a range |
.clear() |
none |
Remove all elements |
.reserve(capacity: int) |
none |
Pre-allocate capacity; negative raises ArgumentError |
.resize(new_size: int, default_value) |
none |
Resize and fill new slots; negative raises ArgumentError |
.copy() |
array |
Shallow copy |
.to_tuple() |
tuple |
Copy the elements into a tuple (empty array gives the empty tuple) |
.map(callback: function) |
array |
New array with the callback applied |
.filter(callback: function) |
array |
New array with elements where the callback is truthy |
.find(callback: function) |
any |
First matching element, or none |
.count(callback: function) |
int |
Number of elements where the callback is truthy |
.any(callback: function) |
bool |
True if the callback is truthy for any element |
.all(callback: function) |
bool |
True if the callback is truthy for every element |
.reduce(callback: function, initial?) |
any |
Fold left with callback(accumulator, element) |
.contains(value) |
bool |
True if any element equals value |
.index_of(value, start: int?) |
int? |
Index of the first equal element, or none |
.last_index_of(value) |
int? |
Index of the last equal element, or none |
.insert(index: int, value) |
none |
Insert before index; out of range raises IndexError |
.remove(index: int) |
any |
Remove and return the element at index |
.extend(other: array) |
none |
Append another array’s elements in place |
.sort(comparator: function?) |
none |
Sort in place with std::sort semantics |
.unique() |
array |
New array with duplicates removed |
.flatten(depth: int?) |
array |
Flatten nested arrays; no argument flattens fully |
.first() |
any |
First element; empty raises IndexError |
.last() |
any |
Last element; empty raises IndexError |
.swap(i: int, j: int) |
none |
Swap two elements |
.sum() |
int | float |
Sum of the numbers; preserves int when all input is int |
.min() |
int | float |
Smallest number |
.max() |
int | float |
Largest number |
let nums = [1, 2, 3]nums.append(4, 5)nums[0] = 10
print(nums) # [10, 2, 3, 4, 5]print(nums.pop()) # 5print(nums.pop(2)) # [3, 4], nums is now [10, 2]print(nums.slice(0, 2)) # [10, 2]
let scaled = nums.map(fn(n) => n * 2)let evens = nums.filter(fn(n) => n % 2 == 0)let first = nums.find(fn(n) => n > 1)let total = nums.reduce(fn(acc, n) => acc + n, 0)
let sorted = [3, 1, 2]sorted.sort() # [1, 2, 3]sorted.sort(fn(a, b) => a > b) # [3, 2, 1]
print([1, 2, 2, 3, 1].unique()) # [1, 2, 3]print([[1], [2, 3]].flatten()) # [1, 2, 3]print([1, 2, 3].sum()) # 6map, filter, find, count, any, and all callbacks take one argument, the element. reduce takes two: the accumulator and the element.
The sort comparator returns true when its first argument belongs before its second, and must be a strict ordering (use <, not <=). With no comparator, sort compares numbers and strings; other element types raise TypeError until you pass one.
There are no negative indices. Use arr[arr.size() - 1]. Out-of-bounds access, pop() on an empty array, and first()/last() on an empty array raise IndexError.
Maps store key-value pairs. Keys can be strings, ints, floats, bools, and other hashable values. Arrays, maps, struct instances, and BytesBuilder cannot be keys.
| Method | Returns | Description |
|---|---|---|
.size() |
int |
Entry count |
.remove(key) |
any |
Remove and return the value, or none |
.keys() |
array |
Array of keys |
.values() |
array |
Array of values |
.has(key) |
bool |
Key existence check |
.clear() |
none |
Remove all entries |
let person = {name: "Ada", age: 36}
print(person.name) # Adaprint(person["age"]) # 36
person["age"] = 37person.city = "London" # dot assignment writes the data key
print(person.has("city")) # trueprint(person.keys())print(person.values())print(person.remove("city"))Map literals use {key: value}. A bare key is auto-quoted as a string. Computed keys are evaluated:
let n = 5let m = {answer: 42, 5 + 5: 10}Missing keys behave differently by access form:
Dot assignment writes a data key, so map.size = 10 does not shadow the size() method.
Equality
Section titled “Equality”- Strings compare by content.
- Arrays and maps compare by identity, so
[] == []is false. - Ints and floats compare numerically across types.
true == 1is false.
contains, index_of, and last_index_of use the same rule: strings match by content, other reference types by identity.
See Types & Values.
Iteration
Section titled “Iteration”for value in [10, 20] { print(value)}
for byte in "hi" { print(byte) # one-byte strings}
for key in person.keys() { printf("{} = {}", key, person[key])}Maps are not directly iterable. Iterate keys() or values().