Lists, Maps, Sets
Collections in Luma allow you to group values together in structured ways. Luma supports Lists, Maps, and Sets.
Lists
A list is an ordered collection of elements of the same type. Lists are declared with square brackets [...].
nums: [int] = [1, 2, 3, 4, 5]
names: [str] = ["Ada", "Bjarne", "Guido"]Indexing
Access elements by index (zero-based):
first: int = nums[0]Methods
// walk
nums.walk(n) -> print(n)
// walk with index
nums.walk(i, n) -> print("${i}: ${n}")
// filter
even: [int] = nums.filter(x -> x % 2 == 0)
// intersection
a: [int] = [1, 2, 3]
b: [int] = [2, 3, 4]
common: [int] = a.intersect(b) // [2, 3]For the full list of methods (add, remove, sort, filter, join, and more), see Lists.
Maps
A map is a collection of key-value pairs. Keys and values each have their own type. Maps are declared with curly braces {...} and key-value pairs separated by :.
ages: {str: int} = {"Alice": 30, "Bob": 25}Accessing values
Use square brackets with a key to look up or assign values:
print(ages["Alice"]) // 30
ages["Charlie"] = 40Iterating maps
Maps can be walked, retrieving keys and values:
ages.walk(k, v) -> print("${k} is ${v} years old")For full details, see Maps.
Sets
A set is an unordered collection of unique values. Sets are declared with curly braces {...} containing values (no key-value pairs).
numbers: {int} = {1, 2, 3, 4, 5}
names: {str} = {"Alice", "Bob", "Charlie"}
scores: {float} = {9.5, 8.0, 7.5}Supported element types: int, float, str.
Sets automatically prevent duplicates. Adding a value that already exists has no effect.
Quick Reference
| Method | Syntax | Returns | Mutates |
|---|---|---|---|
| Contains | s.contains(v) |
bool |
No |
| Add | s.add(v) |
— | Yes |
| Remove | s.remove(v) |
— | Yes |
| Size | s.size() |
int |
No |
| Union | s.union(other) |
{T} |
No |
| Intersect | s.intersect(other) |
{T} |
No |
| Difference | s.difference(other) |
{T} |
No |
| To List | s.to_list() |
[T] |
No |
Checking membership
numbers: {int} = {1, 2, 3, 4, 5}
print(numbers.contains(3)) // true
print(numbers.contains(99)) // falseAdding and removing
numbers.add(6) // add an element
numbers.remove(1) // remove an elementSize
print(numbers.size()) // 5Set operations
Union, intersection, and difference all return new sets:
a: {int} = {1, 2, 3, 4, 5}
b: {int} = {3, 4, 5, 6, 7}
// Union — all elements from both sets
united: {int} = a.union(b) // {1, 2, 3, 4, 5, 6, 7}
// Intersection — elements in both sets
common: {int} = a.intersect(b) // {3, 4, 5}
// Difference — elements in a but not in b
diff: {int} = a.difference(b) // {1, 2}Walking a set
Iterate over set elements with .walk():
names: {str} = {"Alice", "Bob", "Charlie"}
names.walk(n) -> print(n)Sets are unordered — iteration order is not guaranteed.
Converting to a list
member_list: [str] = names.to_list()Empty sets
An empty {} defaults to an empty map. Use a type annotation to create an empty set:
empty: {int} = {}Quick Reference
| Type | Example | Notes |
|---|---|---|
| List | [1, 2, 3] |
Ordered, indexed, all elements same type |
| Map | {"Alice": 30, "Bob": 25} |
Key-value pairs, types must match declaration |
| Set | {1, 2, 3} |
Unordered, unique values only |