-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewindable-streams.rkt
67 lines (55 loc) · 1.98 KB
/
rewindable-streams.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#lang racket/base
(require racket/contract)
(provide make-rstream list->rstream rstream-forever
rstream-next
rstream-snapshot rstream-rewind!)
(module+ test
(require rackunit))
(struct rstream (contents))
(define the-streams (box (make-weak-hasheq)))
(define/contract (make-rstream contents)
(-> (-> exact-nonnegative-integer? any/c) rstream?)
(define s (rstream contents))
(hash-set! (unbox the-streams) s 0)
s)
(define (rstream-next s)
(define pos (hash-ref (unbox the-streams) s))
(hash-set! (unbox the-streams) s (add1 pos) )
((rstream-contents s) pos))
(define (rstream-snapshot)
(hash-copy (unbox the-streams)))
(define (rstream-rewind! snapshot)
(for ([(s pos) (in-hash snapshot)])
(hash-set! (unbox the-streams) s pos)))
(define (rstream-forever x)
(make-rstream (lambda (i) x)))
(define (list->rstream xs (default #f))
(define l (length xs))
(make-rstream (lambda (i)
(if (< i l)
(list-ref xs i)
default))))
(module+ test
(define nums (list 1 2 3 4 5))
(define num-stream (list->rstream nums))
(check-equal? (rstream-next num-stream) 1)
(check-equal? (rstream-next num-stream) 2)
(check-equal? (rstream-next num-stream) 3)
(define s (rstream-snapshot))
(check-equal? (rstream-next num-stream) 4)
(check-equal? (rstream-next num-stream) 5)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f)
(rstream-rewind! s)
(check-equal? (rstream-next num-stream) 4)
(check-equal? (rstream-next num-stream) 5)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f)
(rstream-rewind! s)
(check-equal? (rstream-next num-stream) 4)
(check-equal? (rstream-next num-stream) 5)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f)
(check-equal? (rstream-next num-stream) #f))