1 ;; run with mzscheme -r example.scm
3 (load-extension "example.so")
5 ; repeatedly invoke a procedure with v and an index as arguments
6 (define (with-vector v proc size-proc)
7 (let ((size (size-proc v)))
8 (define (with-vector-item v i)
12 (with-vector-item v (+ i 1)))))
13 (with-vector-item v 0)))
15 (define (with-intvector v proc)
16 (with-vector v proc intvector-length))
17 (define (with-doublevector v proc)
18 (with-vector v proc doublevector-length))
20 (define (print-doublevector v)
21 (with-doublevector v (lambda (v i) (display (doublevector-ref v i))
26 ; Call average with a Scheme list...
28 (display (average '(1 2 3 4)))
31 ; ... or a wrapped std::vector<int>
32 (define v (new-intvector 4))
33 (with-intvector v (lambda (v i) (intvector-set! v i (+ i 1))))
38 ; half will return a Scheme vector.
39 ; Call it with a Scheme vector...
41 (display (half #(1 1.5 2 2.5 3)))
44 ; ... or a wrapped std::vector<double>
45 (define v (new-doublevector))
46 (map (lambda (i) (doublevector-push! v i)) '(1 2 3 4))
50 ; now halve a wrapped std::vector<double> in place
52 (print-doublevector v)
53 (delete-doublevector v)