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

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

Re: alist and multiple values for one key


From: David Kastrup
Subject: Re: alist and multiple values for one key
Date: 21 Jan 2003 12:04:19 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

Oliver Scholz <alkibiades@gmx.de> writes:

> ncauderan@hotmail.com (Norbert C.) writes:
> 
> > Hi,
> >
> >        I would lile to find the "good way" to retrieve multiple values
> > for a given key in an alist.
> >
> >        But I didn't find a natural way to do it. For example :
> > (setq trees '((pine . cones) (pine . acorns) (oak . acorns) (maple .
> > seeds)))
> > ==> ((pine . cones) (pine . acorns) (oak . acorns) (maple . seeds))
> > (assoc 'pine trees)
> > ==> (pine . cones)
> >
> > What I'd like is something that returns each value associated with
> > 'pine.
> 
> If I don't want to `(require 'cl)' I use something like this:
> 
> (delq nil
>       (mapcar (lambda (elt)
>               (if (eq (car elt) 'pine) elt nil))
>             mtrees))
> 
> You see this (delq nil (mapcar ... in Emacs' sources at some places,
> too.

How does it benchmark against

(apply #'append (mapcar (lambda (elt) (and (eq (car elt) 'pine) (list
  elt)))))
?

The latter has the advantage that you don't rip out elements that
were nil to start with, but only those that don't match the condition.


-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


reply via email to

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