aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.private.vector.scm
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2024-06-16 15:14:13 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2024-06-16 15:14:13 +0900
commitc2f4dde340185a4b42beacd46355f94ae41e25e4 (patch)
treebf269e4fa29a24422ddd34670efe03bd3f79d0f9 /algebraic-structures.private.vector.scm
parentded31df310122b0252c69bff90b915c3e5b0ad20 (diff)
Add vector implementations
Diffstat (limited to 'algebraic-structures.private.vector.scm')
-rw-r--r--algebraic-structures.private.vector.scm27
1 files changed, 27 insertions, 0 deletions
diff --git a/algebraic-structures.private.vector.scm b/algebraic-structures.private.vector.scm
new file mode 100644
index 0000000..c9deb22
--- /dev/null
+++ b/algebraic-structures.private.vector.scm
@@ -0,0 +1,27 @@
+(module (algebraic-structures private vector) (<> unit fold reduce map1)
+ (import (except scheme
+ vector-fill! vector->list list->vector)
+ (only (chicken base) add1 assert)
+ (srfi 133))
+
+ (define (<> xs ys) (vector-append xs ys))
+
+ (define unit #())
+
+ (define (fold f z v)
+ (vector-fold (lambda (x y) (f y x))
+ z
+ v))
+
+ (define (reduce f v)
+ (assert (not (zero? (vector-length v))))
+ (let ((len (vector-length v)))
+ (let loop ((i 1)
+ (acc (vector-ref v 0)))
+ (if (= i len)
+ acc
+ (loop (add1 i)
+ (f (vector-ref v i) acc))))))
+
+ (define (map1 f v)
+ (vector-map f v)))