Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 2 commits into from

3 participants

Daniel King Vincent St-Amour Eli Barzilay
Daniel King

The procedure `set-get-one/rest' returns a random element from the set and a new
set with that element removed. It is not guaranteed to return the same element
if it is called twice on the original set.

The procedure `set-get-one' only returns a random element from the set. This can
be used if the programmer simply needs some element from the set and is not
iterating over the set.

This was discussed here:
http://lists.racket-lang.org/users/archive/2011-September/047919.html

Daniel King danking add `set-get-one' and `set-get-one/rest'
The procedure `set-get-one/rest' returns a random element from the set and a new
set with that element removed. It is not guaranteed to return the same element
if it is called twice on the original set.

The procedure `set-get-one' only returns a random element from the set. This can
be used if the programmer simply needs some element from the set and is not
iterating over the set.

This was discussed here:
http://lists.racket-lang.org/users/archive/2011-September/047919.html
6fe5ba3
Vincent St-Amour

set-first and set-rest, added in ac5965a, make this redundant. @elibarzilay, you can close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 9, 2011
  1. Daniel King

    add `set-get-one' and `set-get-one/rest'

    danking authored
    The procedure `set-get-one/rest' returns a random element from the set and a new
    set with that element removed. It is not guaranteed to return the same element
    if it is called twice on the original set.
    
    The procedure `set-get-one' only returns a random element from the set. This can
    be used if the programmer simply needs some element from the set and is not
    iterating over the set.
    
    This was discussed here:
    http://lists.racket-lang.org/users/archive/2011-September/047919.html
Commits on Feb 1, 2012
  1. Daniel King
This page is out of date. Refresh to see the latest.
10 collects/racket/set.rkt
View
@@ -9,6 +9,7 @@
set? set-eq? set-eqv? set-equal?
set-empty? set-count
set-member? set-add set-remove
+ set-get-one set-get-one/rest
set-union set-intersect set-subtract set-symmetric-difference
subset? proper-subset?
set-map set-for-each
@@ -147,6 +148,15 @@
(unless (set? set) (raise-type-error 'set-remove "set" 0 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
(case-lambda
;; No 0 argument set exists because its not clear what type of set
11 collects/scribblings/reference/sets.scrbl
View
@@ -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
@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?]{
6 collects/tests/racket/set.rktl
View
@@ -50,6 +50,12 @@
(test #t set-member? (set-remove s 5) 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 2 3) s)
(test #f subset? (set 1 4) s)
Something went wrong with that request. Please try again.