From 58b9c7a6e4bd4296852bdfbc819feab1ae637c49 Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Fri, 13 Aug 2010 15:00:42 -0600 Subject: [PATCH] Fixing seqn-count --- collects/racket/private/sequence.rkt | 8 ++++++-- collects/scribblings/reference/sequences.scrbl | 4 ++-- collects/tests/racket/for.rktl | 15 +++++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/collects/racket/private/sequence.rkt b/collects/racket/private/sequence.rkt index 972f0d2258..fdfdd3677b 100644 --- a/collects/racket/private/sequence.rkt +++ b/collects/racket/private/sequence.rkt @@ -222,11 +222,15 @@ (λ _ #t) (λ _ #t))))) - (define (seqn-count s) + (define (seqn-count f s) + (unless (procedure? f) + (error 'seqn-count "expects a procedure as the first argument, given ~e" f)) (define-values (more? next) (sequence-generate s)) (let loop ([n 0]) (if (more?) - (begin (next) (loop (add1 n))) + (if (call-with-values next f) + (loop (add1 n)) + (loop n)) n))) (provide empty-seqn diff --git a/collects/scribblings/reference/sequences.scrbl b/collects/scribblings/reference/sequences.scrbl index b0a9ba6724..b523b40be1 100644 --- a/collects/scribblings/reference/sequences.scrbl +++ b/collects/scribblings/reference/sequences.scrbl @@ -149,9 +149,9 @@ then operations on this sequence will not terminate during that infinite sub-seq sequence?]{ Returns a sequence whose elements are the elements of @scheme[s] except in between each is @scheme[e]. The new sequence is constructed lazily. } -@defproc[(seqn-count [s sequence?]) +@defproc[(seqn-count [f procedure?] [s sequence?]) exact-nonnegative-integer?]{ -Returns the number of elements in @scheme[s]. If @scheme[s] is infinite, this function does not terminate. } +Returns the number of elements in @scheme[s] for which @scheme[f] returns a true result. If @scheme[s] is infinite, this function does not terminate. } @defproc*[([(in-range [end number?]) sequence?] [(in-range [start number?] [end number?] [step number? 1]) sequence?])]{ diff --git a/collects/tests/racket/for.rktl b/collects/tests/racket/for.rktl index 675b9724ff..de8f332360 100644 --- a/collects/tests/racket/for.rktl +++ b/collects/tests/racket/for.rktl @@ -385,11 +385,14 @@ (test 1 'seqn-add-between (seqn-ref (seqn-add-between (in-naturals) #t) 2)) (test #t 'seqn-add-between (seqn-ref (seqn-add-between (in-naturals) #t) 3)) -(arity-test seqn-count 1 1) -(test 0 'seqn-count (seqn-count empty-seqn)) -(test 1 'seqn-count (seqn-count (in-range 1))) -(test 10 'seqn-count (seqn-count (in-range 10))) -(let ([r (random 100)]) - (test r 'seqn-count (seqn-count (in-range r)))) +(arity-test seqn-count 2 2) +(test 0 'seqn-count (seqn-count even? empty-seqn)) +(test 1 'seqn-count (seqn-count even? (in-range 1))) +(test 5 'seqn-count (seqn-count even? (in-range 10))) +(let* ([r (random 100)] + [a (if (even? r) + (/ r 2) + (ceiling (/ r 2)))]) + (test a 'seqn-count (seqn-count even? (in-range r)))) (report-errs)