Permalink
Browse files

Enumerator of Vectors.

  • Loading branch information...
1 parent b0aa508 commit 6c0d8ac1cbcd8566717ac679c5f81847bf6653a2 @RayRacine committed Mar 26, 2013
Showing with 23 additions and 6 deletions.
  1. +23 −6 io/iteratee/enumerators.rkt
@@ -18,14 +18,17 @@
#lang typed/racket/base
-(provide
- enumerator/eos
- enumerator/list
- enumerator/text-input-port
- enumerator/select-from-n-lists)
+(provide:
+ [enumerator/eos (All (D A) (Enumerator D A))]
+ [enumerator/list (All (D A) (Listof D) -> (Enumerator D A))]
+ [enumerator/vector (All (D A) (Vectorof D) -> (Enumerator D A))]
+ [enumerator/text-input-port (All (A) (Input-Port -> (Enumerator String A)))]
+ [enumerator/select-from-n-lists (All (D A) (Listof (Listof D)) (D D -> Boolean) -> (Enumerator D A))])
(require
racket/match
+ (only-in racket/unsafe/ops
+ unsafe-vector-ref)
(only-in "iteratee.rkt"
Done Continue Iteratee Enumerator))
@@ -53,6 +56,20 @@
[(cons _ (Done _ _)) iter]
[(cons (list-rest x xs) (Continue k)) (loop xs (k x))]))))
+(: enumerator/vector (All (D A) (Vectorof D) -> (Enumerator D A)))
+(define (enumerator/vector vect)
+ (define: len : Index (vector-length vect))
+ (λ: ((iter : (Iteratee D A)))
+ (let: loop : (Iteratee D A)
+ ((idx : Natural 0)
+ (iter : (Iteratee D A) iter))
+ (if (>= idx len)
+ iter
+ (match iter
+ [(Done _ _) iter]
+ [(Continue k) (loop (add1 idx)
+ (k (unsafe-vector-ref vect idx)))])))))
+
(: enumerator/text-input-port (All (A) (Input-Port -> (Enumerator String A))))
(define (enumerator/text-input-port inp)
(λ: ((iter : (Iteratee String A)))
@@ -113,4 +130,4 @@
(let ((selected (next-selected)))
(if selected
(loop (k selected))
- (k 'EOS)))]))))
+ (k 'EOS)))]))))

0 comments on commit 6c0d8ac

Please sign in to comment.