* Structured data in Emacs Lisp
@ 2005-04-26 15:20 PT
2005-04-26 15:28 ` Denis Bueno
` (4 more replies)
0 siblings, 5 replies; 12+ messages in thread
From: PT @ 2005-04-26 15:20 UTC (permalink / raw)
I'm writing a moderately complex Emacs package and not being a Lisp guru I
wonder what is the best way to handle data structures in Elisp. The
emphasis is not on efficiency, rather on readability.
For example, from a function I want to return three values. How should I
do this?
Using a list? (Value1 Value2 Value3)?
This has the disadvantage of storing specific values on specific
positions, so the caller must now the first element of the list is Value1,
etc. And what if the return value is changed later and Value2 is not
returned anymore? Then I have to fix every invocation of the function.
Or maybe an association list? '((value1 . 3) (value2 . 4) (value3 . 5))
It's certainly more resistant to code changes, but feels a bit
heavyweight. (Maybe its just me.)
Or is there an other Lispish way to handle structured data I don't know
about?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 15:20 Structured data in Emacs Lisp PT
@ 2005-04-26 15:28 ` Denis Bueno
[not found] ` <mailman.3069.1114529432.2895.help-gnu-emacs@gnu.org>
` (3 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Denis Bueno @ 2005-04-26 15:28 UTC (permalink / raw)
Cc: help-gnu-emacs
On 4/26/05, PT <mailshield.gg@mailnull.com> wrote:
<snip explanation>
> Or maybe an association list? '((value1 . 3) (value2 . 4) (value3 . 5))
> It's certainly more resistant to code changes, but feels a bit
> heavyweight. (Maybe its just me.)
You could use a plist:
(:key1 1 :key2 2)
Then to get a particular value:
(getf '(:key1 1 :key2 2) :key1)
=> 1
According to C-h f getf RET:
getf is a compiled Lisp function in `cl-extra'.
(getf plist tag &optional def)
So you might need to do (require 'cl) before using it....
-Denis
PGP: http://pgp.mit.edu:11371/pks/lookup?search=0xA1B51B4B&op=index
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
[not found] ` <mailman.3069.1114529432.2895.help-gnu-emacs@gnu.org>
@ 2005-04-26 15:57 ` PT
2005-04-26 16:59 ` Jim Ottaway
0 siblings, 1 reply; 12+ messages in thread
From: PT @ 2005-04-26 15:57 UTC (permalink / raw)
On Tue, 26 Apr 2005 17:28:48 +0200, Denis Bueno <dbueno@gmail.com> wrote:
>
> You could use a plist:
>
> (:key1 1 :key2 2)
>
> Then to get a particular value:
>
> (getf '(:key1 1 :key2 2) :key1)
> => 1
>
Nice! I like it better than association lists. Thanks!
> So you might need to do (require 'cl) before using it....
I already require 'cl, so it's no problem. I checked Info and getf was
listed under the Symbols node, that's why I didn't know about it.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 15:20 Structured data in Emacs Lisp PT
2005-04-26 15:28 ` Denis Bueno
[not found] ` <mailman.3069.1114529432.2895.help-gnu-emacs@gnu.org>
@ 2005-04-26 16:03 ` Phillip Lord
2005-04-26 17:21 ` PT
2005-04-27 15:27 ` Klaus Berndl
2005-05-09 21:39 ` Christopher C. Stacy
4 siblings, 1 reply; 12+ messages in thread
From: Phillip Lord @ 2005-04-26 16:03 UTC (permalink / raw)
>>>>> "PT" == PT <mailshield.gg@mailnull.com> writes:
PT> I'm writing a moderately complex Emacs package and not being a
PT> Lisp guru I wonder what is the best way to handle data
PT> structures in Elisp. The emphasis is not on efficiency, rather
PT> on readability.
PT> For example, from a function I want to return three values. How
PT> should I do this?
PT> Using a list? (Value1 Value2 Value3)?
PT> This has the disadvantage of storing specific values on specific
PT> positions, so the caller must now the first element of the list
PT> is Value1, etc. And what if the return value is changed later
PT> and Value2 is not returned anymore? Then I have to fix every
PT> invocation of the function.
PT> Or maybe an association list? '((value1 . 3) (value2 . 4)
PT> (value3 . 5)) It's certainly more resistant to code changes, but
PT> feels a bit heavyweight. (Maybe its just me.)
PT> Or is there an other Lispish way to handle structured data I
PT> don't know about?
plists, or a hashtable would also work.
Another solution would be to look at eieio, which gives you objects
that you can then pass around. This doesn't directly answer your
question, but it gets away from some of the nastiness of having
multiply nested list structures.
Phil
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 15:57 ` PT
@ 2005-04-26 16:59 ` Jim Ottaway
2005-04-27 5:35 ` Ian Zimmerman
0 siblings, 1 reply; 12+ messages in thread
From: Jim Ottaway @ 2005-04-26 16:59 UTC (permalink / raw)
>>>>> PT <mailshield.gg@mailnull.com> writes:
> On Tue, 26 Apr 2005 17:28:48 +0200, Denis Bueno <dbueno@gmail.com> wrote:
>>
>> You could use a plist:
>>
>> (:key1 1 :key2 2)
>>
>> Then to get a particular value:
>>
>> (getf '(:key1 1 :key2 2) :key1)
>> => 1
>>
Or you could use defstruct [also in 'cl]
--
Jim Ottaway
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 16:03 ` Phillip Lord
@ 2005-04-26 17:21 ` PT
2005-04-27 11:13 ` Phillip Lord
0 siblings, 1 reply; 12+ messages in thread
From: PT @ 2005-04-26 17:21 UTC (permalink / raw)
On Tue, 26 Apr 2005 18:03:45 +0200, Phillip Lord <p.lord@cs.man.ac.uk>
wrote:
>
> plists, or a hashtable would also work.
Are there any portability considerations regarding these constructs? Any
XEmacs guys here?
I develop on GNU Emacs, but I want to be on the safe side if the need of
supporting XEmacs arises in the future, so I'd avoid using GNU Emacs
specific data structures if possible.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 16:59 ` Jim Ottaway
@ 2005-04-27 5:35 ` Ian Zimmerman
0 siblings, 0 replies; 12+ messages in thread
From: Ian Zimmerman @ 2005-04-27 5:35 UTC (permalink / raw)
Denis> You could use a plist:
Denis>
Denis> (:key1 1 :key2 2)
Denis>
Denis> Then to get a particular value:
Denis>
Denis> (getf '(:key1 1 :key2 2) :key1) => 1
Denis>
Jim> Or you could use defstruct [also in 'cl]
I don't know how many different structures you have. If not too many, I
would just use lists (or even better, vectors) and for each field define
an accessor macro:
(defsubst get-key1 (result) (nth 0 result))
or
(defsubst get-key1 (result) (aref result 0))
--
Optimist: We're only two weeks behind schedule.
Pessimist: The schedule is a whole two weeks ahead of us.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 17:21 ` PT
@ 2005-04-27 11:13 ` Phillip Lord
0 siblings, 0 replies; 12+ messages in thread
From: Phillip Lord @ 2005-04-27 11:13 UTC (permalink / raw)
>>>>> "PT" == PT <mailshield.gg@mailnull.com> writes:
PT> On Tue, 26 Apr 2005 18:03:45 +0200, Phillip Lord
PT> <p.lord@cs.man.ac.uk>
PT> wrote:
>>
>> plists, or a hashtable would also work.
PT> Are there any portability considerations regarding these
PT> constructs? Any XEmacs guys here?
plist should work fine I think. hashtables were new for Emacs 21. But
I've used them in packages which apparently work on XEmacs, so you
should be alright.
PT> I develop on GNU Emacs, but I want to be on the safe side if the
PT> need of supporting XEmacs arises in the future, so I'd avoid
PT> using GNU Emacs specific data structures if possible.
Sensible enough. My experience with trying to support both emacs is
that it's often small differences which cause problems rather than
missing features. For one package I wrote, I spent an hour trying to
debug an XEmacs problem which turned out to be because the `subseq'
function behaves differently between the two. What a nightmare!
In general, now, I write for GNU Emacs. If it's a nice package,
someone will port it to XEmacs sooner or later.
Cheers
Phil
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 15:20 Structured data in Emacs Lisp PT
` (2 preceding siblings ...)
2005-04-26 16:03 ` Phillip Lord
@ 2005-04-27 15:27 ` Klaus Berndl
2005-04-27 18:38 ` PT
2005-05-09 21:39 ` Christopher C. Stacy
4 siblings, 1 reply; 12+ messages in thread
From: Klaus Berndl @ 2005-04-27 15:27 UTC (permalink / raw)
If you want to move around datas which belong together i recommend `defstruct'
which is in both Emacs and XEmacs available! It is a macro of the cl-package
so you can use it safely also with GNU Emacs ;-)
See the info-manual of `defstruct' - It#s in the cl-info-manual!
defstruct allows you to get and set slots via key-names so there are position
independed!
Does this help?
Klaus
On Tue, 26 Apr 2005, mailshield.gg@mailnull.com wrote:
> I'm writing a moderately complex Emacs package and not being a Lisp
> guru I wonder what is the best way to handle data structures in
> Elisp. The emphasis is not on efficiency, rather on readability.
>
> For example, from a function I want to return three values. How should
> I do this?
>
> Using a list? (Value1 Value2 Value3)?
>
> This has the disadvantage of storing specific values on specific
> positions, so the caller must now the first element of the list is
> Value1, etc. And what if the return value is changed later and Value2
> is not returned anymore? Then I have to fix every invocation of the
> function.
>
> Or maybe an association list? '((value1 . 3) (value2 . 4) (value3 . 5))
> It's certainly more resistant to code changes, but feels a bit
> heavyweight. (Maybe its just me.)
>
> Or is there an other Lispish way to handle structured data I don't
> know about?
--
Klaus Berndl mailto: klaus.berndl@sdm.de
sd&m AG http://www.sdm.de
software design & management
Carl-Wery-Str. 42, 81739 Muenchen, Germany
Tel +49 89 63812-392, Fax -220
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-27 15:27 ` Klaus Berndl
@ 2005-04-27 18:38 ` PT
0 siblings, 0 replies; 12+ messages in thread
From: PT @ 2005-04-27 18:38 UTC (permalink / raw)
On Wed, 27 Apr 2005 17:27:16 +0200, Klaus Berndl <klaus.berndl@sdm.de>
wrote:
>
> If you want to move around datas which belong together i recommend
> `defstruct'
[..]
>
> Does this help?
> Klaus
Yep. Someone else also recommended defstruct earlier in the thread, I
checked it out and that's exactly what I need. The code is much more
readable without those deep association lists.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-04-26 15:20 Structured data in Emacs Lisp PT
` (3 preceding siblings ...)
2005-04-27 15:27 ` Klaus Berndl
@ 2005-05-09 21:39 ` Christopher C. Stacy
2005-05-10 10:05 ` Phillip Lord
4 siblings, 1 reply; 12+ messages in thread
From: Christopher C. Stacy @ 2005-05-09 21:39 UTC (permalink / raw)
PT <mailshield.gg@mailnull.com> writes:
> For example, from a function I want to return three values.
> How should I do this?
> Using a list? (Value1 Value2 Value3)?
> Or maybe an association list? '((value1 . 3) (value2 . 4) (value3 . 5))
> Or is there an other Lispish way to handle structured
> data I don't know about?
This isn't going to help with your Emacs problem, but just so
you know: Emacs Lisp is a very simple kind of Lisp, lacking many
features present in most modern (post-1979) dialects of Lisp.
For example, in ANSI Common Lisp, you can directly pass mutiple
values around without packing them into some kind of container.
And for representing structured data, you can use either structs
or object-oriented programming. There are of course many other
data types and a zillion other important features in "real"
Lisp that Emacs Lisp doesn't have. I'm telling you this
because you asked for a "Lispy" way to do things.
I think the "CL" library in Emacs Lisp might give
you some version of DEFSTRUCT, which is something
like the one in Common Lisp.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Structured data in Emacs Lisp
2005-05-09 21:39 ` Christopher C. Stacy
@ 2005-05-10 10:05 ` Phillip Lord
0 siblings, 0 replies; 12+ messages in thread
From: Phillip Lord @ 2005-05-10 10:05 UTC (permalink / raw)
>>>>> "Christopher" == Christopher C Stacy <cstacy@news.dtpq.com> writes:
Christopher> PT <mailshield.gg@mailnull.com> writes:
>> For example, from a function I want to return three values. How
>> should I do this?
>> Using a list? (Value1 Value2 Value3)?
>> Or maybe an association list? '((value1 . 3) (value2 . 4) (value3
>> . 5))
>> Or is there an other Lispish way to handle structured data I
>> don't know about?
Christopher> This isn't going to help with your Emacs problem, but
Christopher> just so you know: Emacs Lisp is a very simple kind of
Christopher> Lisp, lacking many features present in most modern
Christopher> (post-1979) dialects of Lisp. For example, in ANSI
Christopher> Common Lisp, you can directly pass mutiple values
Christopher> around without packing them into some kind of
Christopher> container. And for representing structured data, you
Christopher> can use either structs or object-oriented
Christopher> programming. There are of course many other data types
Christopher> and a zillion other important features in "real" Lisp
Christopher> that Emacs Lisp doesn't have. I'm telling you this
Christopher> because you asked for a "Lispy" way to do things.
Christopher> I think the "CL" library in Emacs Lisp might give you
Christopher> some version of DEFSTRUCT, which is something like the
Christopher> one in Common Lisp.
Much of what you say about emacs lisp is true. But it is worth
mentioning that you can do all three of the things you mention
here. Multiple values you can pass around with the :construct, structs
with CL and classes with eieio (which is not a standard part of
emacs).
Phil
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2005-05-10 10:05 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-26 15:20 Structured data in Emacs Lisp PT
2005-04-26 15:28 ` Denis Bueno
[not found] ` <mailman.3069.1114529432.2895.help-gnu-emacs@gnu.org>
2005-04-26 15:57 ` PT
2005-04-26 16:59 ` Jim Ottaway
2005-04-27 5:35 ` Ian Zimmerman
2005-04-26 16:03 ` Phillip Lord
2005-04-26 17:21 ` PT
2005-04-27 11:13 ` Phillip Lord
2005-04-27 15:27 ` Klaus Berndl
2005-04-27 18:38 ` PT
2005-05-09 21:39 ` Christopher C. Stacy
2005-05-10 10:05 ` Phillip Lord
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).