From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: best way to get the list of unique keys from 2 alists Date: Fri, 16 Dec 2005 16:13:29 -0800 Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1134778518 5791 80.91.229.2 (17 Dec 2005 00:15:18 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 17 Dec 2005 00:15:18 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Dec 17 01:15:09 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1EnPj4-0003LU-6n for geh-help-gnu-emacs@m.gmane.org; Sat, 17 Dec 2005 01:15:02 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EnPjm-00036k-2a for geh-help-gnu-emacs@m.gmane.org; Fri, 16 Dec 2005 19:15:46 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1EnPiZ-0002qD-70 for help-gnu-emacs@gnu.org; Fri, 16 Dec 2005 19:14:31 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1EnPiR-0002nY-6x for help-gnu-emacs@gnu.org; Fri, 16 Dec 2005 19:14:27 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1EnPiQ-0002mm-1c for help-gnu-emacs@gnu.org; Fri, 16 Dec 2005 19:14:22 -0500 Original-Received: from [141.146.126.228] (helo=agminet01.oracle.com) by monty-python.gnu.org with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24) (Exim 4.34) id 1EnPkv-0005lx-9G for help-gnu-emacs@gnu.org; Fri, 16 Dec 2005 19:16:57 -0500 Original-Received: from rgmsgw301.us.oracle.com (rgmsgw301.us.oracle.com [138.1.186.50]) by agminet01.oracle.com (Switch-3.1.7/Switch-3.1.7) with ESMTP id jBH0WKFr023425 for ; Fri, 16 Dec 2005 18:32:21 -0600 Original-Received: from rgmsgw301.us.oracle.com (localhost [127.0.0.1]) by rgmsgw301.us.oracle.com (Switch-3.1.7/Switch-3.1.7) with ESMTP id jBH0DXE5013588 for ; Fri, 16 Dec 2005 17:13:34 -0700 Original-Received: from dradamslap (dhcp-amer-rmdc-csvpn-gw5-141-144-108-2.vpn.oracle.com [141.144.108.2]) by rgmsgw301.us.oracle.com (Switch-3.1.7/Switch-3.1.7) with SMTP id jBH0DWnr013580 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Fri, 16 Dec 2005 17:13:33 -0700 Original-To: X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Brightmail-Tracker: AAAAAQAAAAI= X-Whitelist: TRUE 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:32003 Archived-At: I've got 2 alists of (SYMBOL . "STRING") pairs, and I need to get the list of unique symbol names to pass to completing-read as its TABLE argument: (("SYMBOL-NAME") ...) I know about remove-duplicates and union, but I'd like to avoid using cl*.el functions. Since completing-read seems to ignore nil entries in TABLE, this is what I've got now: (nconc (mapcar (lambda (assoc) (list (symbol-name (car assoc)))) alist-1) (mapcar (lambda (assoc) (or (assq (car assoc) alist-1) (list (symbol-name (car assoc))))) alist-2)) Is there a cleaner way? That looks good to me. You could always define union or remove-duplicates, but removing duplicates would be less efficient than what you're doing (consing up the second list completely, just to perhaps remove stuff). Anyway, here are non-destructive versions of remove-duplicates and union, FWIW: (defun remove-duplicates (list) "Copy of LIST with duplicate elements removed. Tested with `equal'." (let ((tail list) new) (while tail (unless (member (car tail) new) (push (car tail) new)) (pop tail)) (nreverse new))) (defun union (list1 list2) "Combine LIST1 and LIST2 using a set-union operation. The result list contains all items that appear in either LIST1 or LIST2. This is a non-destructive function; it copies the data if necessary." (cond ((null list1) list2) ((null list2) list1) ((equal list1 list2) list1) (t (or (>= (length list1) (length list2)) (setq list1 (prog1 list2 (setq list2 list1)))) ; Swap them. (while list2 (unless (member (car list2) list1) (setq list1 (cons (car list2) list1))) (setq list2 (cdr list2))) list1)))