PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-29/SWIG/Examples/guile/std_vector/runme.scm

#
Lisp | 54 lines | 33 code | 13 blank | 8 comment | 0 complexity | 4d1c2a1d1dd95ea01f1ecbd3bc9820e1 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1;; run with mzscheme -r example.scm
 2
 3(use-modules (example))
 4
 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)
 9      (if (< i size)
10          (begin
11            (proc v i)
12            (with-vector-item v (+ i 1)))))
13    (with-vector-item v 0)))
14
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))
19
20(define (print-DoubleVector v)
21  (with-DoubleVector v (lambda (v i) (display (DoubleVector-ref v i)) 
22                                     (display " ")))
23  (newline))
24
25
26; Call average with a Scheme list...
27
28(display (average '(1 2 3 4)))
29(newline)
30
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))))
34(display (average v))
35(newline)
36(delete-IntVector v)
37
38; half will return a Scheme vector.
39; Call it with a Scheme vector...
40
41(display (half #(1 1.5 2 2.5 3)))
42(newline)
43
44; ... or a wrapped std::vector<double>
45(define v (new-DoubleVector))
46(map (lambda (i) (DoubleVector-push! v i)) '(1 2 3 4))
47(display (half v))
48(newline)
49
50; now halve a wrapped std::vector<double> in place
51(halve-in-place v)
52(print-DoubleVector v)
53(delete-DoubleVector v)
54