Skip to content

Commit

Permalink
Fixed `stream-add-between'.
Browse files Browse the repository at this point in the history
Don't put an extra seperator element at the end of the stream.  Brings
it in-line with the list version (`add-between') and sequence
version (`sequence-add-between').  Includes a test.
  • Loading branch information
MartyNeal authored and elibarzilay committed May 5, 2012
1 parent 36dd749 commit 6e1ee71
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions collects/racket/stream.rkt
Expand Up @@ -152,12 +152,15 @@
(make-do-stream (lambda () (force!) empty?) (make-do-stream (lambda () (force!) empty?)
(lambda () (force!) fst) (lambda () (force!) fst)
(lambda () (force!) rst)))])) (lambda () (force!) rst)))]))

(define (stream-add-between s e) (define (stream-add-between s e)
(unless (stream? s) (unless (stream? s)
(raise-type-error 'stream-add-between "stream" s)) (raise-type-error 'stream-add-between "stream" s))
(let loop ([s s]) (if (stream-empty? s)
(cond empty-stream
[(stream-empty? s) empty-stream] (stream-cons
[else (stream-cons (stream-first s) (stream-first s)
(stream-cons e (loop (stream-rest s))))]))) (let loop ([s (stream-rest s)])
(cond [(stream-empty? s) empty-stream]
[else (stream-cons e (stream-cons (stream-first s)
(loop (stream-rest s))))])))))
3 changes: 2 additions & 1 deletion collects/tests/racket/sequence.rktl
@@ -1,4 +1,3 @@

(load-relative "loadtest.rktl") (load-relative "loadtest.rktl")


(Section 'sequence) (Section 'sequence)
Expand Down Expand Up @@ -108,6 +107,8 @@
(sequence-ref (sequence-add-between (in-naturals) #t) 2)) (sequence-ref (sequence-add-between (in-naturals) #t) 2))
(test #t 'sequence-add-between (test #t 'sequence-add-between
(sequence-ref (sequence-add-between (in-naturals) #t) 3)) (sequence-ref (sequence-add-between (in-naturals) #t) 3))
(test 3 'sequence-add-between
(sequence-length (sequence-add-between (in-range 2) #t)))


(arity-test sequence-count 2 2) (arity-test sequence-count 2 2)
(test 0 'sequence-count (sequence-count even? empty-sequence)) (test 0 'sequence-count (sequence-count even? empty-sequence))
Expand Down

0 comments on commit 6e1ee71

Please sign in to comment.