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: Hannu Koivisto
Subject: Re: alist and multiple values for one key
Date: Tue, 21 Jan 2003 01:45:44 +0200
User-agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.2 (i686-pc-linux-gnu)

kai.grossjohann@uni-duisburg.de (Kai Großjohann) writes:

> ncauderan@hotmail.com (Norbert C.) writes:
>
>>        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.
>
> Oh, with (require 'cl), you could do
>
> (remove-if-not (lambda (x) (eq (car x) KEY)) ALIST)
>
> Is that natural?  It's short, anyway.

If all your predicate does is (comparison-function (key-retriever x) key),
you can use the :key and :test{,-not} parameters of plain remove*,
resulting to a still shorter alternative:

(remove* key alist :key #'car :test-not #'eq)

But neither of these retrieve all values for a given key, which is
what the OP wanted.  Instead, they evaluate to alists with elements
whose keys match the given key.

So, something like this could be used to do what the OP wanted
(loop is from the cl package as well):

(loop for (key . val) in trees
      when (eq 'pine key) collect val)

-- 
Hannu


reply via email to

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