tur/gadt-vec
length-indexed GVec GADT with phantom natural-number type parameter.
Since: Phase G4
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 :ptr<void> 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.