tur/gadt-vec
GVec: a singly-linked list of ints with a phantom type parameter.
GVec is a phantom-typed singly-linked list of ints: both constructors return (GVec int), so the parameter never says anything about the length, and gvec-head-or takes an explicit default. A genuinely length-indexed vector -- VNil : (Vec Zero), VCons : (Vec (Succ n)) over phantom-parameter opaques -- gets an arm-less head on a scrutinee annotated (Vec (Succ n)), and a rejected call on (VNil) itself, whose index is known; the recipe is the "Length-Indexed Vectors" section of docs/guides/gadts-cookbook.md.
Since: Phase G4
GVec
(defdata GVec [a] (GVNil : (GVec int)) (GVCons int (GVec int) : (GVec int)))
gvec-nil
(gvec-nil : GVec)
construct an empty GVec.
A (GVec int) value representing the empty list.
(gvec-nil) ; => GVNil
Since: Phase G4
gvec-cons
(gvec-cons [x : int v : GVec] : GVec)
prepend an integer element to a GVec.
| x | the integer element to prepend | |
| v | the existing (GVec int) to prepend onto |
A new (GVec int) with x at the front.
(gvec-cons 1 (gvec-nil)) ; => GVCons(1, GVNil)
Since: Phase G4
gvec-len
(gvec-len [v : GVec] : int)
compute the length of a GVec.
| v | the (GVec int) to measure |
The number of elements in v as an int.
(gvec-len (gvec-cons 1 (gvec-cons 2 (gvec-nil)))) ; => 2
Since: Phase G4
gvec-sum
(gvec-sum [v : GVec] : int)
sum all elements of a GVec.
| v | the (GVec int) to sum |
The integer sum of all elements; 0 for an empty GVec.
(gvec-sum (gvec-cons 1 (gvec-cons 2 (gvec-cons 3 (gvec-nil))))) ; => 6
Since: Phase G4
gvec-head-or
(gvec-head-or [dflt : int v : GVec] : int)
return the first element or a default if empty.
| dflt | the int value to return when v is empty | |
| v | the (GVec int) to inspect |
The head element of v if non-empty, otherwise dflt.
(gvec-head-or 99 (gvec-nil)) ; => 99
(gvec-head-or 99 (gvec-cons 5 (gvec-nil))) ; => 5
Since: Phase G4
gvec-tail
(gvec-tail [v : GVec] : GVec)
return all elements after the first.
| v | the (GVec int) to take the tail of |
The tail (GVec int) of v; GVNil if v is empty.
(gvec-tail (gvec-cons 1 (gvec-cons 2 (gvec-nil)))) ; => GVCons(2, GVNil)
(gvec-tail (gvec-nil)) ; => GVNil
Since: Phase G4
gvmap
(gvmap [f : ptr<void> v] : GVec)
apply an int->int function to every element of a GVec.
| f | a curried int->int function (e.g. a partial application) | |
| v | the (GVec int) to transform |
A new (GVec int) with f applied to each element.
(defn mul [a : int b : int] : int (* a b))
(gvec-sum (gvmap (mul 2) (gvec-cons 1 (gvec-cons 2 (gvec-nil))))) ; => 6
Since: Phase G4
gvzip-with
(gvzip-with [f : (fn [int int] int) v1 v2] : GVec)
combine two GVecs element-wise using a binary int->int->int function.
| f | a two-argument function (int, int) -> int | |
| v1 | the first (GVec int) | |
| v2 | the second (GVec int) |
A (GVec int) of combined values; stops at the shorter input.
(defn add [a : int b : int] : int (+ a b))
(gvec-sum (gvzip-with add
(gvec-cons 1 (gvec-cons 2 (gvec-nil)))
(gvec-cons 10 (gvec-cons 20 (gvec-nil))))) ; => 33
Since: Phase G4
Internal definitions
__gvec-call-fn1-- apply a ptr<void> closure to one int argument.