Collections
Mux provides three main collection types: lists, maps, and sets, with Python-style literal syntax.
Lists
Ordered, mutable collections of elements of the same type.
Creating Lists
List Methods
| Method | Returns | Description |
|---|---|---|
.size() | int | Number of elements in the list |
.is_empty() | bool | Returns true if list has no elements |
.get(int index) | optional<T> | Safe access; returns some(value) or none if out of bounds |
[int index] | T | Direct access; runtime error if out of bounds |
.push(T item) | void | Appends item to the front |
.push_back(T item) | void | Appends item to the end |
.pop() | optional<T> | Removes and returns first item, or none if empty |
.pop_back() | optional<T> | Removes and returns last item, or none if empty |
.to_string() | string | String representation of the list |
List Operations
Iterating Lists
Maps
Key-value pairs with unique keys.
Creating Maps
Map Methods
| Method | Returns | Description |
|---|---|---|
.size() | int | Number of key-value pairs |
.is_empty() | bool | Returns true if map has no entries |
.get(K key) | optional<V> | Safe lookup; returns some(value) or none if key not found |
[K key] | V | Direct access; runtime error if key not found |
.put(K key, V value) | void | Inserts or updates a key-value pair |
.contains(K key) | bool | Returns true if key exists in map |
.remove(K key) | optional<V> | Removes key and returns value, or none if key not found |
.to_string() | string | String representation of the map |
.get_keys() | list<K> | List of keys from the map |
.get_values() | list<V> | List of values from the map |
.get_pairs() | list<tuple<K, V>> | List of key-value pairs |
Map Operations
Sets
Creating Sets
Set Methods
| Method | Returns | Description |
|---|---|---|
.size() | int | Number of elements |
.is_empty() | bool | Returns true if set is empty |
.add(T item) | void | Adds an item to the set |
.contains(T item) | bool | Returns true if item exists in set |
.remove(T item) | optional<T> | Removes item and returns it, or none if not found |
.to_string() | string | String representation of the set |
.to_list() | list<T> | Creates a list from the set |
Set Operations
The in Operator
Test for membership/containment:
Type Requirements:
- Both operands must have compatible element types
- No implicit type conversions allowed
Nested Collections
Collections can be arbitrarily nested:
Collection Type Conversions
No automatic conversions - collections must have exact type match:
Generic Collections
Collections work seamlessly with generics:
Collection Literals vs Constructors
Best Practices
- Use safe access methods -
.get()returnsoptional<T>to avoid runtime errors - Explicit types for empty collections - Prevents ambiguity
- Leverage type inference - Use
autowhen types are obvious from literals - Use the
inoperator - Cleaner than calling.contains() - Prefer literals over constructors - More readable
- Use appropriate collection type:
- List: Ordered, indexed access, duplicates allowed
- Map: Key-value pairs, unique keys
- Set: Unique elements, membership testing
- Match on fallible operations - Handle
nonecases explicitly
See Also
- Types - Type system and conversions
- Generics - Generic functions with collections
- Error Handling - optional<T> for safe access
- Memory - Reference counting for collections