;; -*-theme-*-

;; Copyright (C) 2014, 2021  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-virtual-method sequence-ref (%element)
    (((seq (:vector-as-sequence %element)) (index <integer>))
     %element pure)
    (vector-ref (field-ref seq 'contents) index))


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


  (define-param-virtual-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))))
      (let* ((result1 (cast-vector %target result))
	     (result2
	      (create (:vector-as-sequence %target) result1)))
	result2))))
