#lang scheme
(require scheme/generator)
(define (sequence-ref s i)
(define-values (more? get) (sequence-generate s))
(let loop ([n i])
(if (more?)
(if (zero? n)
(get)
(begin
(get)
(loop (sub1 n))))
(error 'sequence-ref "Sequence does not contain ~e elements" i))))
(define (sequence-count s)
(define-values (more? get) (sequence-generate s))
(let loop ([i 0])
(if (more?)
(begin (get) (loop (add1 i)))
i)))
(define (sequence->list s)
(for/list ([e s]) e))
(define (sequence-map f s)
(make-do-sequence
(lambda ()
(define-values (more? get) (sequence-generate s))
(values
(lambda (pos) (f (get)))
(lambda (pos) pos)
0
(lambda (pos) (more?))
(lambda (val) #t)
(lambda (pos val) #t)))))
(define (sequenceof c)
sequence?)
(provide (all-defined-out))