tur/vec
typed Vec[A]: parameterized growable array.
Since: Phase TC1-A
Vec
(defstruct Vec [A])
parameterized growable array with element type A.
Since: Phase TC1
vec-new
(vec-new)
allocate a new empty Vec[A].
A new empty Vec[A] with len=0 and cap=0.
(vec-new) ; => empty Vec
Since: Phase TC1
vec-len
(vec-len [v :int])
return the number of elements in a Vec[A].
| v | Vec[A] pointer |
Element count.
(vec-len my-vec) ; => 3
Since: Phase TC1
vec-get
(vec-get [v :int i :int])
return the element at index i (bounds-checked).
| v | Vec[A] pointer | |
| i | zero-based index |
The element at index i (typed A, stored as int64_t).
(vec-get my-vec 0) ; => first element
Since: Phase TC1
vec-push!
(vec-push! [v :int val :int])
append an element to the Vec[A].
| v | Vec[A] pointer | |
| val | element of type A to append |
(vec-push! my-vec 42)
Since: Phase TC1
vec-pop!
(vec-pop! [v :int])
remove and return the last element of a Vec[A].
| v | Vec[A] pointer |
The last element of type A (as int64_t), or 0 if empty.
(vec-pop! my-vec) ; => last element
Since: Phase TC1
vec-set!
(vec-set! [v :int i :int val :int])
overwrite the element at index i.
| v | Vec[A] pointer | |
| i | zero-based index | |
| val | new element value of type A |
(vec-set! my-vec 0 99)
Since: Phase TC1
vec-free
(vec-free [v :int])
free a Vec[A] and its data buffer.
| v | Vec[A] pointer to free |
(vec-free my-vec)
Since: Phase TC1
vec-push-each__
(vec-push-each__ [v & xs])
internal helper for the vec-of macro.
vec-of
(vec-of [& xs])
variadic Vec[A] literal.
| & xs -- zero or more elements, all of type A. |
A fresh Vec[A] populated with xs in order. Carrier type is :int (the same handle returned by vec-new), so it composes with all of vec-len, vec-get, vec-push!, vec-eq?, etc. Note: all elements must unify to a single type. Mixing types -- e.g. (vec-of 1 "x" 3.14) -- is a type error (TUR-E0001 on the offending element). For heterogeneous fixed-arity collections, use tupleN.
(vec-of) ; => empty Vec (vec-of 1 2 3) ; => Vec containing 1, 2, 3
Since: Phase 2 cons-in-docs cleanup
vec-eq?
(vec-eq? [v1 :int v2 :int cmp-fn])
compare two Vec[A] element-wise using a comparator.
| v1 | first Vec[A] | |
| v2 | second Vec[A] | |
| cmp-fn | comparator fn [a :int b :int] :bool |
true if both vecs have the same length and all element pairs satisfy cmp-fn.
(vec-eq? va vb (fn [a b] (= a b))) ; => true
Since: Phase TC1
Eq[Vec]
(definstance Eq [Vec])
element-wise equality using the element type's Eq instance.