PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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. (use-modules (example))
  3. ; repeatedly invoke a procedure with v and an index as arguments
  4. (define (with-vector v proc size-proc)
  5. (let ((size (size-proc v)))
  6. (define (with-vector-item v i)
  7. (if (< i size)
  8. (begin
  9. (proc v i)
  10. (with-vector-item v (+ i 1)))))
  11. (with-vector-item v 0)))
  12. (define (with-IntVector v proc)
  13. (with-vector v proc IntVector-length))
  14. (define (with-DoubleVector v proc)
  15. (with-vector v proc DoubleVector-length))
  16. (define (print-DoubleVector v)
  17. (with-DoubleVector v (lambda (v i) (display (DoubleVector-ref v i))
  18. (display " ")))
  19. (newline))
  20. ; Call average with a Scheme list...
  21. (display (average '(1 2 3 4)))
  22. (newline)
  23. ; ... or a wrapped std::vector<int>
  24. (define v (new-IntVector 4))
  25. (with-IntVector v (lambda (v i) (IntVector-set! v i (+ i 1))))
  26. (display (average v))
  27. (newline)
  28. (delete-IntVector v)
  29. ; half will return a Scheme vector.
  30. ; Call it with a Scheme vector...
  31. (display (half #(1 1.5 2 2.5 3)))
  32. (newline)
  33. ; ... or a wrapped std::vector<double>
  34. (define v (new-DoubleVector))
  35. (map (lambda (i) (DoubleVector-push! v i)) '(1 2 3 4))
  36. (display (half v))
  37. (newline)
  38. ; now halve a wrapped std::vector<double> in place
  39. (halve-in-place v)
  40. (print-DoubleVector v)
  41. (delete-DoubleVector v)