;; -*-theme-*-

;; Copyright (C) 2014  Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.


(define-body (examples vector-as-sequence)


  (define-param-method sequence-ref (%element)
  		       (((seq (:vector-as-sequence %element)) (index <integer>))
			%element pure)
    (vector-ref (field-ref seq 'contents) index))


  (define-param-method sequence-length (%element)
		       (((seq (:vector-as-sequence %element))) <integer> pure)
    (vector-length (field-ref seq 'contents)))


  (define-param-method sequence-map (%source %target)
    (((proc (:procedure (%source) %target pure)) 
      (seq (:vector-as-sequence %source)))
     (:vector-as-sequence %target) force-pure)
    (let* ((source-vec (field-ref seq 'contents))
	   (len (vector-length source-vec))
	   (result (make-mutable-vector (:maybe %target) len null)))
      (do ((i <integer> 0 (+ i 1))) ((>= i len))
	(mutable-vector-set! result i (proc (vector-ref source-vec i))))
      (make (:vector-as-sequence %target)
	    (cast-vector %target result)))))
