aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.private.vector.scm
blob: 742700189be732b20c83018f1c2d2aae3df038a2 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(module (algebraic-structures private vector) (op unit fold reduce map1)
  (import (except scheme
                  vector-fill! vector->list list->vector)
          (only (chicken base) add1 assert)
          (srfi 133))

  (define (op 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)))