help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LISP Questions - random, random elements and memory management


From: Jeff Clough
Subject: LISP Questions - random, random elements and memory management
Date: Wed, 18 Nov 2009 07:54:28 -0500 (EST)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]