Skip to main content

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

creating_lists.mux
Loading...

List Methods

MethodReturnsDescription
.size()intNumber of elements in the list
.is_empty()boolReturns true if list has no elements
.get(int index)optional<T>Safe access; returns some(value) or none if out of bounds
[int index]TDirect access; runtime error if out of bounds
.push(T item)voidAppends item to the front
.push_back(T item)voidAppends 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()stringString representation of the list

List Operations

list_operations.mux
Loading...

Iterating Lists

iterating_lists.mux
Loading...

Maps

Key-value pairs with unique keys.

Creating Maps

creating_maps.mux
Loading...

Map Methods

MethodReturnsDescription
.size()intNumber of key-value pairs
.is_empty()boolReturns true if map has no entries
.get(K key)optional<V>Safe lookup; returns some(value) or none if key not found
[K key]VDirect access; runtime error if key not found
.put(K key, V value)voidInserts or updates a key-value pair
.contains(K key)boolReturns true if key exists in map
.remove(K key)optional<V>Removes key and returns value, or none if key not found
.to_string()stringString 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

map_operations.mux
Loading...

Sets

Creating Sets

creating_sets.mux
Loading...

Set Methods

MethodReturnsDescription
.size()intNumber of elements
.is_empty()boolReturns true if set is empty
.add(T item)voidAdds an item to the set
.contains(T item)boolReturns true if item exists in set
.remove(T item)optional<T>Removes item and returns it, or none if not found
.to_string()stringString representation of the set
.to_list()list<T>Creates a list from the set

Set Operations

set_operations.mux
Loading...

The in Operator

Test for membership/containment:

in_operator.mux
Loading...

Type Requirements:

  • Both operands must have compatible element types
  • No implicit type conversions allowed

Nested Collections

Collections can be arbitrarily nested:

nested_collections.mux
Loading...

Collection Type Conversions

No automatic conversions - collections must have exact type match:

collection_conversions.mux
Loading...

Generic Collections

Collections work seamlessly with generics:

generic_collections.mux
Loading...

Collection Literals vs Constructors

collection_literals.mux
Loading...

Best Practices

  1. Use safe access methods - .get() returns optional<T> to avoid runtime errors
  2. Explicit types for empty collections - Prevents ambiguity
  3. Leverage type inference - Use auto when types are obvious from literals
  4. Use the in operator - Cleaner than calling .contains()
  5. Prefer literals over constructors - More readable
  6. Use appropriate collection type:
    • List: Ordered, indexed access, duplicates allowed
    • Map: Key-value pairs, unique keys
    • Set: Unique elements, membership testing
  7. Match on fallible operations - Handle none cases 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