Data Structures
Arrays (array)
Arrays are ordered, mutable sequences of values.
Literals
Inline with brackets:
Spreading
Use ... to splice an iterable into an array literal:
See the Array API for methods.
Dictionaries (dict)
Dictionaries are ordered, mutable key-value mappings. They preserve insertion order and are actually multi-maps: a single key can have multiple values.
Literals
Inline with braces:
Symbol Keys vs String Keys
A bare key: in a dict literal creates a symbol key:
To interpret the key as an expression instead, prefix it with $:
Constants, quoted strings, parenthesized expressions, and other literals are automatically treated as expressions. Values are always treated as full, whitespace-insensitive expressions.
Non-Pair Elements
Dict literals can contain values without explicit keys. These receive incrementing integer keys starting at 0:
Ordering
Dictionaries preserve insertion order. Iteration yields entries in the order they were inserted.
Multi-Map Semantics
Dicts are multi-maps: a key can map to multiple values. This matters in specific cases:
-
Construction: Duplicate keys in a literal or spread preserve all values:
-
Plain indexing (
d[key]) returns only the last (most recently inserted) value for a key. - Plain assignment (
d[key] = value) replaces all values for a key. - Methods like
insert,get, andpoprespect the multi-map nature ofdict
See the Dict API for details.
Spreading
Spread an iterable of key/value pairs (e.g. another dict iterator) into a dict literal:
Each yielded item must unpack into exactly two values:
Spreading of dicts preserves duplicate keys and order.
Records (record)
Records are similar to dicts but only support symbol and integer keys. They allow direct field access with dot syntax:
Records support the same ordering and multi-map semantics as dicts where applicable. They are iterable, unpackable, and support index/assignment for their key types.
See the Record API for details.
Sets (set)
Sets are ordered, mutable collections with unique membership semantics.
Unlike arrays and dicts, sets do not have a dedicated literal syntax. Construct
them with the set type object from any iterable:
Iteration preserves insertion order. Adding an existing value is a no-op and does not move it to the end.
See the Set API for methods such as add, contains,
union, and diff.
Tuples (tuple)
Tuples are immutable, ordered sequences of values.
Like sets, tuples do not have a dedicated literal syntax. Construct them with
the tuple type object from an iterable:
Some APIs produce tuples, such as key/value pair iteration:
Note that mutable collections may be used as dict keys, so tuple usage
is not mandatory.
See the Tuple API for details.