From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jeff Clough Newsgroups: gmane.emacs.help Subject: LISP Questions - random, random elements and memory management Date: Wed, 18 Nov 2009 07:54:28 -0500 (EST) Message-ID: <20091118.075428.224836459.jeff@chaosphere.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1258548896 15807 80.91.229.12 (18 Nov 2009 12:54:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 18 Nov 2009 12:54:56 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 18 13:54:49 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NAk3d-00069X-1E for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Nov 2009 13:54:49 +0100 Original-Received: from localhost ([127.0.0.1]:54291 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAk3c-0001aU-Cu for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Nov 2009 07:54:48 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NAk2q-0001Zx-3D for help-gnu-emacs@gnu.org; Wed, 18 Nov 2009 07:54:00 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NAk2n-0001ZR-70 for help-gnu-emacs@gnu.org; Wed, 18 Nov 2009 07:53:58 -0500 Original-Received: from [199.232.76.173] (port=43347 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAk2m-0001ZE-Ne for help-gnu-emacs@gnu.org; Wed, 18 Nov 2009 07:53:56 -0500 Original-Received: from hrndva-omtalb.mail.rr.com ([71.74.56.122]:45551) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NAk2m-0002pu-Dl for help-gnu-emacs@gnu.org; Wed, 18 Nov 2009 07:53:56 -0500 Original-Received: from localhost ([74.70.71.134]) by hrndva-omta03.mail.rr.com with ESMTP id <20091118125354882.NRJE8997@hrndva-omta03.mail.rr.com> for ; Wed, 18 Nov 2009 12:53:54 +0000 X-Mailer: Mew version 6.3rc1 on Emacs 23.1 / Mule 6.0 (HANACHIRUSATO) X-detected-operating-system: by monty-python.gnu.org: Solaris 10 (1203?) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69872 Archived-At: 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