Rename {take,drop}-while' ->
{take,drop}f'.
Also, improve the tests a little.
This commit is contained in:
parent
7d1ad25d6c
commit
2cdfe18beb
|
@ -15,8 +15,8 @@
|
|||
split-at
|
||||
drop-right
|
||||
take-right
|
||||
take-while
|
||||
drop-while
|
||||
takef
|
||||
dropf
|
||||
split-at-right
|
||||
|
||||
append*
|
||||
|
@ -153,11 +153,11 @@
|
|||
(cons (car list) (loop (cdr list) (cdr lead)))
|
||||
'())))
|
||||
|
||||
(define (take-while pred list)
|
||||
(define (takef pred list)
|
||||
(unless (procedure? pred)
|
||||
(raise-argument-error 'take-while "procedure?" 0 pred list))
|
||||
(raise-argument-error 'takef "procedure?" 0 pred list))
|
||||
(unless (list? list)
|
||||
(raise-argument-error 'take-while "list?" 1 pred list))
|
||||
(raise-argument-error 'takef "list?" 1 pred list))
|
||||
(let loop ([list list])
|
||||
(if (null? list)
|
||||
'()
|
||||
|
@ -166,11 +166,11 @@
|
|||
(cons x (loop (cdr list)))
|
||||
'())))))
|
||||
|
||||
(define (drop-while pred list)
|
||||
(define (dropf pred list)
|
||||
(unless (procedure? pred)
|
||||
(raise-argument-error 'drop-while "procedure?" 0 pred list))
|
||||
(raise-argument-error 'dropf "procedure?" 0 pred list))
|
||||
(unless (list? list)
|
||||
(raise-argument-error 'drop-while "list?" 1 pred list))
|
||||
(raise-argument-error 'dropf "list?" 1 pred list))
|
||||
(let loop ([list list])
|
||||
(cond [(null? list) '()]
|
||||
[(pred (car list)) (loop (cdr list))]
|
||||
|
|
|
@ -821,7 +821,7 @@ Returns the same result as
|
|||
|
||||
except that it can be faster.}
|
||||
|
||||
@defproc[(take-while [pred procedure?] [lst list?])
|
||||
@defproc[(takef [pred procedure?] [lst list?])
|
||||
list?]{
|
||||
Returns a fresh list whose elements are taken successively from
|
||||
@racket[lst] as long as they satisfy @racket[pred]. The returned
|
||||
|
@ -829,18 +829,18 @@ list includes up to, but not including, the first element in
|
|||
@racket[lst] for which @racket[pred] returns @racket[#f].
|
||||
|
||||
@mz-examples[#:eval list-eval
|
||||
(take-while even? '(2 4 5 8))
|
||||
(take-while odd? '(2 4 6 8))
|
||||
(dropf even? '(2 4 5 8))
|
||||
(dropf odd? '(2 4 6 8))
|
||||
]}
|
||||
|
||||
@defproc[(drop-while [pred procedure?] [lst list?])
|
||||
@defproc[(dropf [pred procedure?] [lst list?])
|
||||
list?]{
|
||||
Returns a fresh list with elements successively removed from
|
||||
@racket[lst] from the front as long as they satisfy @racket[pred].
|
||||
|
||||
@mz-examples[#:eval list-eval
|
||||
(drop-while even? '(2 4 5 8))
|
||||
(drop-while odd? '(2 4 6 8))
|
||||
(dropf even? '(2 4 5 8))
|
||||
(dropf odd? '(2 4 6 8))
|
||||
]}
|
||||
|
||||
@defproc[(take-right [lst any/c] [pos exact-nonnegative-integer?]) any/c]{
|
||||
|
|
|
@ -212,22 +212,23 @@
|
|||
(err/rt-test (fun '(1) 2) exn:application:mismatch?)
|
||||
(err/rt-test (fun '(1 2 . 3) 3) exn:application:mismatch?)))
|
||||
|
||||
;; ---------- take/drop-while ----------
|
||||
;; ---------- takef/dropf ----------
|
||||
|
||||
(let ()
|
||||
(define list-1 '(2 4 6 8 1 3 5))
|
||||
(err/rt-test (take-while 5 '()) exn:application:mismatch?)
|
||||
(err/rt-test (drop-while 5 '()) exn:application:mismatch?)
|
||||
(err/rt-test (take-while even? 1) exn:application:mismatch?)
|
||||
(err/rt-test (drop-while even? 1) exn:application:mismatch?)
|
||||
(test '(2 4 6 8) take-while even? list-1)
|
||||
(test '(1 3 5) drop-while even? list-1)
|
||||
(test '() take-while odd? list-1)
|
||||
(test list-1 drop-while odd? list-1)
|
||||
(test list-1 take-while number? list-1)
|
||||
(test '() drop-while number? list-1)
|
||||
(test '() take-while list? '())
|
||||
(test '() drop-while list? '()))
|
||||
(err/rt-test (takef 5 '()) exn:application:mismatch?)
|
||||
(err/rt-test (dropf 5 '()) exn:application:mismatch?)
|
||||
(err/rt-test (takef even? 1) exn:application:mismatch?)
|
||||
(err/rt-test (dropf even? 1) exn:application:mismatch?)
|
||||
(define (t pred take-l drop-l)
|
||||
(define l (append take-l drop-l))
|
||||
(test take-l takef pred l)
|
||||
(test drop-l dropf pred l))
|
||||
(t even? '() '())
|
||||
(t even? '(2 4) '(5 7))
|
||||
(t even? '(2 4 6 8) '())
|
||||
(t even? '() '(1 3 5))
|
||||
(t symbol? '(a b c) '(1 2 3 x y z)))
|
||||
|
||||
;; ---------- append* ----------
|
||||
(let ()
|
||||
|
|
Loading…
Reference in New Issue
Block a user