check-duplicate -> check-duplicates
For consistency with remove-duplicates.
This commit is contained in:
parent
7700b3d736
commit
1083a31965
|
@ -1114,7 +1114,7 @@ traversal.
|
||||||
(flatten 'a)]}
|
(flatten 'a)]}
|
||||||
|
|
||||||
|
|
||||||
@defproc[(check-duplicate [lst list?]
|
@defproc[(check-duplicates [lst list?]
|
||||||
[same? (any/c any/c . -> . any/c) equal?]
|
[same? (any/c any/c . -> . any/c) equal?]
|
||||||
[#:key extract-key (-> any/c any/c) (lambda (x) x)])
|
[#:key extract-key (-> any/c any/c) (lambda (x) x)])
|
||||||
(or/c any/c #f)]{
|
(or/c any/c #f)]{
|
||||||
|
@ -1129,10 +1129,10 @@ The procedures @racket[equal?], @racket[eqv?], and @racket[eq?] automatically
|
||||||
use a dictionary for speed.
|
use a dictionary for speed.
|
||||||
|
|
||||||
@examples[#:eval list-eval
|
@examples[#:eval list-eval
|
||||||
(check-duplicate '(1 2 3 4))
|
(check-duplicates '(1 2 3 4))
|
||||||
(check-duplicate '(1 2 3 2 1))
|
(check-duplicates '(1 2 3 2 1))
|
||||||
(check-duplicate '((a 1) (b 2) (a 3)) #:key car)
|
(check-duplicates '((a 1) (b 2) (a 3)) #:key car)
|
||||||
(check-duplicate '(1 2 3 4 5 6)
|
(check-duplicates '(1 2 3 4 5 6)
|
||||||
(lambda (x y) (equal? (modulo x 3) (modulo y 3))))
|
(lambda (x y) (equal? (modulo x 3) (modulo y 3))))
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -328,17 +328,17 @@
|
||||||
(test `(,@fst ,@r2 ,@lst) add-between l x
|
(test `(,@fst ,@r2 ,@lst) add-between l x
|
||||||
#:splice? #t #:before-first fst #:after-last lst #:before-last y)))))
|
#:splice? #t #:before-first fst #:after-last lst #:before-last y)))))
|
||||||
|
|
||||||
;; ---------- check-duplicate ----------
|
;; ---------- check-duplicates ----------
|
||||||
|
|
||||||
(test #f check-duplicate '())
|
(test #f check-duplicates '())
|
||||||
(test 'a check-duplicate '(a a))
|
(test 'a check-duplicates '(a a))
|
||||||
(test 'a check-duplicate '(a b a))
|
(test 'a check-duplicates '(a b a))
|
||||||
(test 'a check-duplicate '(a a b))
|
(test 'a check-duplicates '(a a b))
|
||||||
(test '(a 3) check-duplicate '((a 1) (b 2) (a 3)) #:key car)
|
(test '(a 3) check-duplicates '((a 1) (b 2) (a 3)) #:key car)
|
||||||
(test 4 check-duplicate '(1 2 3 4 5 6) (lambda (x y) (equal? (modulo x 3) (modulo y 3))))
|
(test 4 check-duplicates '(1 2 3 4 5 6) (lambda (x y) (equal? (modulo x 3) (modulo y 3))))
|
||||||
(err/rt-test (check-duplicate 'a))
|
(err/rt-test (check-duplicates 'a))
|
||||||
(err/rt-test (check-duplicate '(1) #f))
|
(err/rt-test (check-duplicates '(1) #f))
|
||||||
(err/rt-test (check-duplicate '(1) #:key #f))
|
(err/rt-test (check-duplicates '(1) #:key #f))
|
||||||
|
|
||||||
;; ---------- remove-duplicates ----------
|
;; ---------- remove-duplicates ----------
|
||||||
(let ()
|
(let ()
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
flatten
|
flatten
|
||||||
add-between
|
add-between
|
||||||
remove-duplicates
|
remove-duplicates
|
||||||
check-duplicate
|
check-duplicates
|
||||||
filter-map
|
filter-map
|
||||||
count
|
count
|
||||||
partition
|
partition
|
||||||
|
@ -424,32 +424,32 @@
|
||||||
(cons x (loop l)))))))])])
|
(cons x (loop l)))))))])])
|
||||||
(if key (loop key) (loop no-key)))])))
|
(if key (loop key) (loop no-key)))])))
|
||||||
|
|
||||||
;; check-duplicate : (listof X)
|
;; check-duplicates : (listof X)
|
||||||
;; [(K K -> bool)]
|
;; [(K K -> bool)]
|
||||||
;; #:key (X -> K)
|
;; #:key (X -> K)
|
||||||
;; -> X or #f
|
;; -> X or #f
|
||||||
(define (check-duplicate items
|
(define (check-duplicates items
|
||||||
[same? equal?]
|
[same? equal?]
|
||||||
#:key [key values])
|
#:key [key values])
|
||||||
(unless (list? items)
|
(unless (list? items)
|
||||||
(raise-argument-error 'check-duplicate "list?" items))
|
(raise-argument-error 'check-duplicates "list?" items))
|
||||||
(unless (and (procedure? key)
|
(unless (and (procedure? key)
|
||||||
(procedure-arity-includes? key 1))
|
(procedure-arity-includes? key 1))
|
||||||
(raise-argument-error 'check-duplicate "(-> any/c any/c)" key))
|
(raise-argument-error 'check-duplicates "(-> any/c any/c)" key))
|
||||||
(cond [(eq? same? equal?)
|
(cond [(eq? same? equal?)
|
||||||
(check-duplicate/t items key (make-hash))]
|
(check-duplicates/t items key (make-hash))]
|
||||||
[(eq? same? eq?)
|
[(eq? same? eq?)
|
||||||
(check-duplicate/t items key (make-hasheq))]
|
(check-duplicates/t items key (make-hasheq))]
|
||||||
[(eq? same? eqv?)
|
[(eq? same? eqv?)
|
||||||
(check-duplicate/t items key (make-hasheqv))]
|
(check-duplicates/t items key (make-hasheqv))]
|
||||||
[else
|
[else
|
||||||
(unless (and (procedure? same?)
|
(unless (and (procedure? same?)
|
||||||
(procedure-arity-includes? same? 2))
|
(procedure-arity-includes? same? 2))
|
||||||
(raise-argument-error 'check-duplicate
|
(raise-argument-error 'check-duplicates
|
||||||
"(any/c any/c . -> . any/c)"
|
"(any/c any/c . -> . any/c)"
|
||||||
same?))
|
same?))
|
||||||
(check-duplicate/list items key same?)]))
|
(check-duplicates/list items key same?)]))
|
||||||
(define (check-duplicate/t items key table)
|
(define (check-duplicates/t items key table)
|
||||||
(let loop ([items items])
|
(let loop ([items items])
|
||||||
(and (pair? items)
|
(and (pair? items)
|
||||||
(let ([key-item (key (car items))])
|
(let ([key-item (key (car items))])
|
||||||
|
@ -457,7 +457,7 @@
|
||||||
(car items)
|
(car items)
|
||||||
(begin (hash-set! table key-item #t)
|
(begin (hash-set! table key-item #t)
|
||||||
(loop (cdr items))))))))
|
(loop (cdr items))))))))
|
||||||
(define (check-duplicate/list items key same?)
|
(define (check-duplicates/list items key same?)
|
||||||
(let loop ([items items] [sofar null])
|
(let loop ([items items] [sofar null])
|
||||||
(and (pair? items)
|
(and (pair? items)
|
||||||
(let ([key-item (key (car items))])
|
(let ([key-item (key (car items))])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user