Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add set-get-one' and set-get-one/rest' #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions collects/racket/set.rkt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
set? set-eq? set-eqv? set-equal? set? set-eq? set-eqv? set-equal?
set-empty? set-count set-empty? set-count
set-member? set-add set-remove set-member? set-add set-remove
set-get-one set-get-one/rest
set-union set-intersect set-subtract set-symmetric-difference set-union set-intersect set-subtract set-symmetric-difference
subset? proper-subset? subset? proper-subset?
set-map set-for-each set-map set-for-each
Expand Down Expand Up @@ -147,6 +148,15 @@
(unless (set? set) (raise-type-error 'set-remove "set" 0 set v)) (unless (set? set) (raise-type-error 'set-remove "set" 0 set v))
(make-set (hash-remove (set-ht set) v))) (make-set (hash-remove (set-ht set) v)))


(define (set-get-one set)
(unless (set? set) (raise-type-error 'set-some-element "set" 0 set))
(for/first ([e (set-ht set)]) e))

(define (set-get-one/rest set)
(unless (set? set) (raise-type-error 'set-some-element "set" 0 set))
(let ((element (for/first ([e (set-ht set)]) e)))
(values element (set-remove set element))))

(define set-union (define set-union
(case-lambda (case-lambda
;; No 0 argument set exists because its not clear what type of set ;; No 0 argument set exists because its not clear what type of set
Expand Down
11 changes: 11 additions & 0 deletions collects/scribblings/reference/sets.scrbl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ Produces a set that includes @racket[v] plus all elements of
Produces a set that includes all elements of @racket[st] except Produces a set that includes all elements of @racket[st] except
@racket[v]. This operation runs in constant time.} @racket[v]. This operation runs in constant time.}


@defproc[(set-get-one [st set?]) any/c]{
Produces a random element from @racket[st]. This procedure is not guaranteed to
produce the same element if it is called again with the same set. This operation
runs in constant time.}

@defproc[(set-get-one/rest [st set?]) (values any/c set?)]{

Produces a random element from @racket[st] and a new set that does not contain
that element. This procedure is not guaranteed to produce the same element if it
is called again with the original set. This operation runs in constant time.}



@defproc[(set-union [st set?] ...+) set?]{ @defproc[(set-union [st set?] ...+) set?]{


Expand Down
6 changes: 6 additions & 0 deletions collects/tests/racket/set.rktl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
(test #t set-member? (set-remove s 5) 3) (test #t set-member? (set-remove s 5) 3)
(test #f set-member? (set-remove s 3) 3) (test #f set-member? (set-remove s 3) 3)


(test #t 'set-get-one (set-member? s (set-get-one s)))

(test #t 'set-get-one/rest (set-member? s (let-values (((e _) (set-get-one/rest s))) e)))
(test #f 'set-get-one/rest (let-values (((e s-no-e) (set-get-one/rest s)))
(set-member? s-no-e e)))

(test #t subset? (set 1 3) s) (test #t subset? (set 1 3) s)
(test #t subset? (set 1 2 3) s) (test #t subset? (set 1 2 3) s)
(test #f subset? (set 1 4) s) (test #f subset? (set 1 4) s)
Expand Down