[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "assoc" for returning ALL associations for a given key
From: |
Nikolaj Schumacher |
Subject: |
Re: "assoc" for returning ALL associations for a given key |
Date: |
Wed, 06 May 2009 12:10:58 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (darwin) |
florian <lorian@fsavigny.de> wrote:
> (defun assocs (key alist)
> "Like `assoc', but return all elements of ALIST whose car is
> `equal' to KEY.
> In other words, return a subset of ALIST."
> (delq nil
> (mapcar '(lambda (cons-cell)
> (if (equal (car cons-cell) key)
> cons-cell))
> alist)))
>
> But I am wondering whether that would have been necessary - perhaps I
> am overlooking something? Otherwise, would anybody care to comment
> about the efficiency of the above function?
A lot of temporary cons objects are created, so it's probably not the
most efficient way of doing this.
If efficiency is really critical, you should build the list yourself.
Or maybe create no list at all, if appropriate...
(dolist (el lst)
(when (equal (car el) key)
(do-stuff-here ...
That's easier on the eyes, too. ;)
regards,
Nikolaj Schumacher