Collections
Weir provides two collection types with literal syntax: vectors (ordered, indexed) and hash-maps (key-value).
Vectors
Section titled “Vectors”Vectors are ordered, indexed collections created with square bracket literals:
[1 2 3 4 5]["hello" "world"][] ;; empty vectorVectors are typed — all elements must have the same type. The element type is inferred from contents:
(let ((nums [1 2 3]) ;; (Vector i64) (names ["alice" "bob"])) ;; (Vector String) ...)Operations
Section titled “Operations”Returns the number of elements:
(len [1 2 3]) ;; => 3(len []) ;; => 0Gets the element at an index (zero-based):
(nth [10 20 30] 0) ;; => 10(nth [10 20 30] 2) ;; => 30append
Section titled “append”Returns a new vector with an element added at the end:
(append [1 2] 3) ;; => [1 2 3](append [] "hello") ;; => ["hello"]set-nth
Section titled “set-nth”Returns a new vector with the element at an index replaced:
(set-nth [1 2 3] 1 99) ;; => [1 99 3]Vectors are immutable — append and set-nth return new vectors, leaving the original unchanged:
(let ((v [1 2 3]) (v2 (append v 4))) (println (len v)) ;; => 3 (unchanged) (println (len v2))) ;; => 4Higher-Order Operations
Section titled “Higher-Order Operations”Vectors work with closures and higher-order patterns:
;; Map a function over positions(map .pos enemies)
;; Filter with a predicate(filter (fn (e) (> (.health e) 0)) enemies)Parallel Operations
Section titled “Parallel Operations”For bulk processing, parallel versions are available:
(par-map update-position entities)(par-for-each process-chunk chunks)See Built-in Functions — Concurrency for details.
Hash-Maps
Section titled “Hash-Maps”Hash-maps are unordered key-value collections created with curly brace literals. Keys are typically keywords:
{:name "Alice" :health 100 :pos (Vec2 0.0 0.0)}
{} ;; empty mapNested Maps
Section titled “Nested Maps”Maps can be nested:
{:player {:name "Alice" :level 5} :enemies [(spawn-goblin) (spawn-orc)]}Operations
Section titled “Operations”Returns the number of entries:
(len {:a 1 :b 2 :c 3}) ;; => 3Summary
Section titled “Summary”| Function | Signature | Description |
|---|---|---|
len | (Fn ['a] i64) | Length of vector, map, or string |
nth | (Fn [(Vector 'a) i64] 'a) | Get element at index |
append | (Fn [(Vector 'a) 'a] (Vector 'a)) | Append element |
set-nth | (Fn [(Vector 'a) i64 'a] (Vector 'a)) | Replace element at index |
par-map | (Fn [(Fn ['a] 'b) (Vector 'a)] (Vector 'b)) | Parallel map |
par-for-each | (Fn [(Fn ['a] Unit) (Vector 'a)] Unit) | Parallel for-each |
Collection Literals vs. S-Expressions
Section titled “Collection Literals vs. S-Expressions”| Syntax | Meaning |
|---|---|
(f x y) | S-expression — function call |
[x y z] | Vector literal |
{:k1 v1 :k2 v2} | Hash-map literal |
Parentheses are always code. Square brackets and curly braces are always data.