Re: Getting a random hash-table element?
- From: Tonci Damjanic <tonci.damjanic@xxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 16:27:45 +0100
On Thu, 29 Nov 2007 00:41:47 -0500, Ken Tilton wrote:
The nice thing about every form returning a value is:
(find-selected-symbol
(random (hash-table-count *my-table*)))
Initially, I've written this in one line. As that was too ugly, I've
introduced new variable in order to clarify things. But now it looks OK,
especially with this line break. :)
But we get more mileage out of code when it is parameterized. Sure, you
could rebind the special before each call and sometimes that is
appropriate (when other wise you are just forever having to pass the
table around) but by default I'd like to see a parameter:
(defun gre (h)
(fss (random (hash-table-count h))))
You're right, but in this situation *my-table* is the table used throughout
the whole program and passing it around as a parameter would be to
confusing. *my-table* is defined once, during initialization of parameters,
and used unmodified later.
Yep, and I like the counting down and testing for zero instead of
counting up with a second variable.
When you are ready (and don't wait too long) learn loop:
(defun gre-fss (h)
(loop for x downfrom (random (hash-table-count h))
for v being the hash-values of h
when (zerop x) return v))
Alright, this is the simplicity I needed! Learning loop is my next lesson.
:)
Although my solution works, maphash shouldn't be used like I did...
Thanks!
Tonci
--
"For millions of years mankind lived just like the animals
Then something happened which unleashed the power of our imagination
We learned to talk"
.
- References:
- Getting a random hash-table element?
- From: Tonci Damjanic
- Re: Getting a random hash-table element?
- From: Tonci Damjanic
- Re: Getting a random hash-table element?
- From: Ken Tilton
- Getting a random hash-table element?
- Prev by Date: Re: Reasons to choose CLISP over other free implementations
- Next by Date: asdf-install sbcl windows
- Previous by thread: Re: Getting a random hash-table element?
- Next by thread: Re: Getting a random hash-table element?
- Index(es):
Relevant Pages
|
|