unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* LISP Questions - random, random elements and memory management
@ 2009-11-18 12:54 Jeff Clough
  2009-11-18 13:09 ` Jeff Clough
  2009-11-19  3:03 ` Kevin Rodgers
  0 siblings, 2 replies; 11+ messages in thread
From: Jeff Clough @ 2009-11-18 12:54 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I've started making real forays into Emacs Lisp over the last few
days, making good progress (I think I'm finally starting to "get it"),
and I have some questions.

random - Is there some way I can tell that (random t) has already been
done?  I know I can detect if my own code has done this via setting
setting J Random Variable, but I'm looking for something more robust.

get-random-element - Is there a function somewhere in Emacs that,
given a list (or sequence in general, perhaps), returns a random
element of the list?  Something like this?

(defun get-random-element (list)
  "Returns a random element of LIST."
  (if (not (and (list) (listp list)))
      (nth (random (1- (1+ (length list)))) list)
    (error "Argument to get-random-element not a list or the list is empty")))

Obviously I have this functionality now, but I'm looking for something
that Joe Average is already going to have in his Emacs.  (PS, if the
code above sucks, please tell me why.)

Lastly, I have a function that takes two lists, pulls a random element
from each and concats the elements to form a string (a random name
generator).  It works just swell with the lists as defconsts, but one
of the lists is quite large (88,000 elements today) and burns about a
megabyte of RAM.  It takes above half a second to evaluate the
defconst, but then using the list is zippy (even nth'ing near the end
of the list takes no time).

What is the done thing in Emacs Lisp to keep this data out of memory
until it's needed that also lets the function stay zippy?  Bonus
points if I can keep the data and function bundled up in the same .el
file.

I've taken a look at autoload, provide and require, but I'm not
certain I understand them fully or how they would be used to solve
this problem.

Any help would be most appreciated!

Jeff




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-11-20  6:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-18 12:54 LISP Questions - random, random elements and memory management Jeff Clough
2009-11-18 13:09 ` Jeff Clough
2009-11-19  2:53   ` Kevin Rodgers
2009-11-19  3:03 ` Kevin Rodgers
2009-11-19 11:56   ` Jeff Clough
     [not found]   ` <mailman.11040.1258631785.2239.help-gnu-emacs@gnu.org>
2009-11-19 13:44     ` Barry Margolin
2009-11-19 15:16       ` Jeff Clough
2009-11-20  3:52         ` Kevin Rodgers
     [not found]         ` <mailman.11114.1258689162.2239.help-gnu-emacs@gnu.org>
2009-11-20  6:28           ` Barry Margolin
     [not found]       ` <mailman.11056.1258643746.2239.help-gnu-emacs@gnu.org>
2009-11-19 17:40         ` Colin S. Miller
2009-11-19 18:57           ` Jeff Clough

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).