Lists ([1, 2, 3])
Luma supports lists as a core data structure, offering both mutability and simplicity, along with a rich set of methods inspired by modern programming languages.
Declaration
nums: [int] = [1, 2, 3]
words: [str] = ["hello", "world"]
empty: [str] = []Supported element types: int, float, str, bool, byte, and nested lists like [[str]].
Access
Index Access
print(nums[0]) // 1
print(nums[2]) // 3Slicing
Extract a portion of a list using start..end inside brackets. Both bounds are optional:
nums: [int] = [10, 20, 30, 40, 50]
nums[1..4] // [20, 30, 40] — from index 1 up to (not including) 4
nums[2..] // [30, 40, 50] — from index 2 to the end
nums[..3] // [10, 20, 30] — from the start up to index 3Slicing also works on strings:
word: str = "hello"
word[1..] // "ello"
word[..3] // "hel"Walk
Iterate over a list using .walk(). One parameter gives values, two gives index and value:
nums: [int] = [10, 20, 30]
nums.walk(v) -> print(v)
nums.walk(i, v) -> {
print("${i}: ${v}")
}Output:
10
20
30
0: 10
1: 20
2: 30Use skip to skip an iteration, or stop to exit the walk early:
nums: [int] = [1, -2, 3, -4, 5]
nums.walk(n) -> {
if n < 0 { skip }
print(n)
}
// Output: 1, 3, 5See Walk Loops (skip & stop) for more details.
Quick Reference
| Method | Syntax | Returns | Mutates |
|---|---|---|---|
| Length | nums.len() |
int |
No |
| Add | nums.add(4) |
— | Yes |
| Bulk Add | nums.add_all([4, 5]) |
— | Yes |
| Remove by value | nums.remove(2) |
bool |
Yes |
| Bulk Remove | nums.remove_all([1, 2]) |
— | Yes |
| Sort | nums.sort() |
— | Yes |
| Filter | nums.filter(x -> x > 1) |
[T] |
No |
| Intersect | nums.intersect([1, 3]) |
[T] |
No |
| Join (strings) | words.join(", ") |
str |
No |
Methods
add
Appends a single element to the list:
nums: [int] = [1, 2]
nums.add(3)
print(nums) // [1, 2, 3]add_all
Appends all elements from another list:
nums: [int] = [1, 2]
nums.add_all([3, 4, 5])
print(nums) // [1, 2, 3, 4, 5]remove
Removes the first occurrence of a value. Returns true if found, false otherwise:
nums: [int] = [1, 2, 3]
found: bool = nums.remove(2) // true
print(nums) // [1, 3]
missing: bool = nums.remove(99) // falseremove_all
Removes all occurrences of the given values:
nums: [int] = [1, 2, 3, 4, 5]
nums.remove_all([2, 4])
print(nums) // [1, 3, 5]sort
Sorts the list in place:
nums: [int] = [3, 1, 2]
nums.sort()
print(nums) // [1, 2, 3]
words: [str] = ["banana", "apple", "cherry"]
words.sort()
print(words) // [apple, banana, cherry]filter
Returns a new list with elements that match the predicate:
nums: [int] = [1, 2, 3, 4, 5]
evens: [int] = nums.filter(x -> x % 2 == 0)
print(evens) // [2, 4]The original list is not modified.
intersect
Returns a new list with elements common to both lists:
a: [int] = [1, 2, 3, 4]
b: [int] = [3, 4, 5, 6]
common: [int] = a.intersect(b)
print(common) // [3, 4]join
Joins a [str] list into a single string with a separator:
words: [str] = ["one", "two", "three"]
result: str = words.join(", ")
print(result) // one, two, threeOnly works on [str] lists.
len
Returns the number of elements:
nums: [int] = [10, 20, 30]
print(nums.len()) // 3
print([].len()) // 0Nested Lists
Lists can contain other lists:
table: [[str]] = []
table.add(["Alice", "30", "Tallinn"])
table.add(["Bob", "25", "Riga"])
print(table[0]) // [Alice 30 Tallinn]
print(table[0][1]) // 30
print(table.len()) // 2All list methods (.add(), .len(), .sort(), etc.) work with nested lists.
Byte List Methods ([byte])
Byte lists have additional methods for encoding binary data as text and converting to/from integers.
Encoding
data: [byte] = [0x48, 0x65, 0x6C, 0x6C, 0x6F]
text: str = data.to_str() // "Hello"
hex: str = data.to_hex() // "48656c6c6f"
b64: str = data.to_base64() // "SGVsbG8="
b64url: str = data.to_base64url() // "SGVsbG8"| Method | Returns | Format |
|---|---|---|
data.to_str() |
str |
UTF-8 string — the inverse of str.to_bytes() |
data.to_hex() |
str |
Lowercase hex, 2 chars per byte |
data.to_base64() |
str |
Standard base64 with padding (RFC 4648) |
data.to_base64url() |
str |
URL-safe base64, no padding |
These always succeed. To decode back, use the corresponding methods on str — see String Utilities and Type Conversion.
// Hash a file and display as hex
hash: [byte] = crypto.sha256(content)
print(hash.to_hex())
// Encode binary for JSON transport
image: [byte] = file("photo.png").read()
print(image.to_base64())
// Generate a URL-safe token
token: str = crypto.rand(32).to_base64url()Integer Conversion
Byte lists can be interpreted as integers. The _be suffix selects big-endian byte order; without it, little-endian is used:
data: [byte] = [0x78, 0x56, 0x34, 0x12]
value: int = data.to_int16() // 2 bytes, little-endian
value: int = data.to_int24() // 3 bytes, little-endian
value: int = data.to_int32() // 4 bytes, little-endianBig-endian (for network protocols and binary file formats):
header: [byte] = [0x00, 0x10]
page_size: int = header.to_int16_be() // 4096The reverse — converting an int to bytes — is a method on int:
n: int = 0x1234
bytes: [byte] = n.to_int16() // [0x34, 0x12] little-endian
bytes = n.to_int16_be() // [0x12, 0x34] big-endianAvailable widths: 16-bit, 24-bit, 32-bit, 48-bit, and 64-bit. Big-endian IEEE 754 floats are also supported with to_float64_be(). A single byte can also be produced:
b: [byte] = 65.to_byte() // [0x41]For full details, see Bytes and Type Conversion.
Examples
nums: [int] = [1, 2]
nums.add(3) // [1, 2, 3]
nums.remove(2) // true
print(nums) // [1, 3]
// Filter
filtered: [int] = nums.filter(x -> x > 1) // [3]
// Slicing
all: [int] = [10, 20, 30, 40, 50]
slice: [int] = all[0..2] // [10, 20]
tail: [int] = all[2..] // [30, 40, 50]
head: [int] = all[..3] // [10, 20, 30]
nums.sort() // [1, 3]
common: [int] = nums.intersect([3]) // [3]
// Join string lists
tags: [str] = "a,b,c".split(",")
tags.join(" | ") // "a | b | c"Edge Cases
| Case | Behavior |
|---|---|
nums.remove(99) |
Returns false (value not found) |
nums.intersect([]) |
Returns empty list |
[].len() |
Returns 0 |
nums.filter(x -> false) |
Returns empty list |