From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.help Subject: Re: Writing a function/macro in Elisp that generates a function at runtime Date: Tue, 28 Oct 2008 11:47:46 -0500 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <86fxmg4qfh.fsf@lifelogs.com> References: <4906EB9C.10501@dr-qubit.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1225215860 30325 80.91.229.12 (28 Oct 2008 17:44:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 28 Oct 2008 17:44:20 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 28 18:45:21 2008 connect(): Connection refused 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 1Kusd3-0000fO-AM for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 18:45:17 +0100 Original-Received: from localhost ([127.0.0.1]:40324 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kusbx-0008GX-2Z for geh-help-gnu-emacs@m.gmane.org; Tue, 28 Oct 2008 13:44:09 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!newsfeed.freenet.de!news.albasani.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 80 Original-X-Trace: news.albasani.net njg59Df6xtBxmqor2ZHwCw7c/GbJIKgMpXl4RM0empYuWfGb7jWn7XsiNEemr0P9PqImbqHbPsHAD3hMNIHR8AotX79CWQ1HIFN3HMTc0Kmsh/1vLDLCvCdND1Jsyl1P Original-X-Complaints-To: abuse@albasani.net Original-NNTP-Posting-Date: Tue, 28 Oct 2008 16:47:48 +0000 (UTC) X-User-ID: R9183QvVzO7N5ESpR0LAlG27xdQg0dowu27g/wEdoeM= X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" Cancel-Lock: sha1:mqMbX3H/qvPbWPlVl5l0ugJf/vQ= sha1:yz8EhqrqlUGxdeBwspWy5bccMII= User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux) X-NNTP-Posting-Host: HxCI1aVX7ZypyqEAKVuK+JhZF+pCKRpLCHdQ6yGQYmU= Original-Xref: news.stanford.edu gnu.emacs.help:163866 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:59209 Archived-At: On Tue, 28 Oct 2008 13:42:57 +0100 Toby Cubitt wrote: TC> I have a higher-level data structure built on top of a lower-level one. TC> Internally, the higher-level structure stores data "cells" in the TC> lower-level structure. The lower-level structure of course knows nothing TC> about this. It just stores any kind of data, without knowing anything TC> about its structure. And users of the higher-level structure also know TC> nothing about the data cells, which are part of the internal TC> implementation. As far as users are concerned, they can just store data TC> of whatever kind they like in the higher-level structure. TC> For the lower-level structure, I have defined exactly this kind of TC> insert-with-function, called say `low-level-insert-with-function'. What TC> I'm trying to do is to define a `high-level-insert-with-function' for TC> the higher-level data structure. Clearly, it has to call TC> `low-level-insert-with-function' to insert the data into the lower-level TC> structure on which it is build, something like: TC> (defun high-level-insert-with-function (higher-structure data insfun) TC> (low-level-insert-with-function TC> (higher-structure--get-lower-structure higher-structure) TC> data ??insfun??)) TC> Now insfun knows nothing about the data cells, since it was passed in by TC> the user. And `low-level-insert-with-function' knows nothing about data TC> cells, since it acts on the lower-level data structure. TC> So it would seem I need to wrap insfun on the fly in the above function, TC> to make it aware that the data is actually stored within "cells". And TC> we're back to the problem from my previous mail: how to generate a TC> wrapped insfun on the fly, in such a way that the setf macro required to TC> enter data into a "cell" is expanded into the cell-data accessor TC> function at compile-time. I don't understand why you're trying to cross between the storage and the logic (essentially, that's what your "low level" and "high level" layers are). The logic layer should know how to convert its data to and from a neutral data format (in Lisp, that's usually a list). The storage layer should just store and retrieve the neutral data format. Your approach makes sense, IMO, in an OOP environment where every object knows how to serialize itself. I got the following from http://dorophone.blogspot.com/ and it may help you to sort-of-do serializable objects if that's your goal... You just print the value of each object you create, and later you can eval them back in. HTH Ted ;;; from J.V. Toups http://dorophone.blogspot.com/ (defun toups-bang (sym) (intern (format "%s!" sym))) (defun toups-s-cat (sym1 sym2) (intern (format "%s-%s" sym1 sym2))) (defun toups-ques (sym) (intern (format "%s?" sym))) (defmacro defstruquine (name &rest slots) (let* ((n-fields (length slots)) (i 1) (out `(progn (defun ,(toups-bang name) ,slots (list ',(toups-bang name) ,@slots)) (defun ,(toups-ques name) (item) (eq (car item) ',(toups-bang name)))))) (loop for slot in slots do (setf out (append out (list `(defun ,(toups-s-cat name slot) (item) (elt item ,i))))) (setf i (+ i 1))) (append out (list nil)))) ;; Which can be used thusly: ;; (defstruquine person first-name last-name age) ;; (let ((p (person! "Edward" "Olmos" 61))) ;; (person-first-name p) ;; is "Edward" ;; (person-age p) ;; is 62 ;; p) ;; returns (person! "Edward" "Olmos" 61)