[plt-dev] n-valued list and vector sequences

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Fri Feb 5 14:37:43 EST 2010

I've found these functions very useful and I'd like to add them to the
core. I'm not sure what a good naming convention is. Perhaps just
in-list and in-vector with an additional argument. What do you think?

#lang scheme
(require tests/eli-tester)

(define (in-list* l n)
  (make-do-sequence
   (lambda ()
     (values (lambda (l) (apply values (take l n)))
             (lambda (l) (drop l n))
             l
             (lambda (l) (>= (length l) n))
             (lambda _ #t)
             (lambda _ #t)))))

(define l (list 1 2 3 4))

(test
 (for/list ([i (in-list l)]) i) => '(1 2 3 4)
 (for/list ([i (in-list* l 1)]) i) => '(1 2 3 4)
 (for/list ([(i j) (in-list* l 2)]) (cons i j)) => '((1 . 2) (3 . 4))
 (for/list ([(i j k) (in-list* l 3)]) (list i j k)) => '((1 2 3)))

(define (in-vector* v n)
  (make-do-sequence
   (λ ()
     (values (λ (i) (vector->values v i (+ i n)))
             (λ (i) (+ i n))
             0
             (λ (i) (>= (vector-length v) (+ i n)))
             (λ _ #t)
             (λ _ #t)))))

(define v (vector 1 2 3 4))

(test
 (for/list ([i (in-vector v)]) i) => '(1 2 3 4)
 (for/list ([i (in-vector* v 1)]) i) => '(1 2 3 4)
 (for/list ([(i j) (in-vector* v 2)]) (cons i j)) => '((1 . 2) (3 . 4))
 (for/list ([(i j k) (in-vector* v 3)]) (list i j k)) => '((1 2 3)))

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the dev mailing list.