Re: choose-and-remove! operation taylanbayirli@xxxxxx 12 Sep 2015 20:29 UTC

"Arthur A. Gleckler" <> writes:

> (choose-and-remove! hash-table) => key, value
> The idea is that it picks a key-value pair at random, removes it, and
> returns it.

Given my new hashtable-find (not in draft #3, but seen in the live
version), this can be done like:

    (define (hashtable-pop! ht)
      (let-values (((key value found?) (hashtable-find (lambda () #t) ht)))
        (assert found?)
        (hashtable-delete! ht key)
        (values key value)))

and it gives you flexibility in deciding what to do in the no-entries
case; here just an error via assertion.  So I think I'll leave
hashtable-pop! out of the spec.  It could save you a call to the hash
function (in hashtable-delete!) if provided directly instead of
implemented like this, but I don't think that matters.