From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#30202: 27.0.50; Code refactoring on assq-delete-all assoc-delete-all Date: Tue, 23 Jan 2018 00:04:24 +0900 (JST) Message-ID: References: <87r2qilb3d.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII X-Trace: blaine.gmane.org 1516633422 8632 195.159.176.226 (22 Jan 2018 15:03:42 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 22 Jan 2018 15:03:42 +0000 (UTC) User-Agent: Alpine 2.20 (DEB 67 2015-01-07) Cc: 30202@debbugs.gnu.org, Tino Calancha To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Jan 22 16:03:37 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eddd2-0000wc-GI for geb-bug-gnu-emacs@m.gmane.org; Mon, 22 Jan 2018 16:03:20 +0100 Original-Received: from localhost ([::1]:46948 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eddf1-0005eF-2O for geb-bug-gnu-emacs@m.gmane.org; Mon, 22 Jan 2018 10:05:23 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50295) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1edden-0005VY-Hf for bug-gnu-emacs@gnu.org; Mon, 22 Jan 2018 10:05:15 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eddeh-0002qs-Na for bug-gnu-emacs@gnu.org; Mon, 22 Jan 2018 10:05:09 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:58625) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eddeh-0002q2-J2 for bug-gnu-emacs@gnu.org; Mon, 22 Jan 2018 10:05:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eddeg-00079m-Hm for bug-gnu-emacs@gnu.org; Mon, 22 Jan 2018 10:05:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 22 Jan 2018 15:05:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30202 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 30202-submit@debbugs.gnu.org id=B30202.151663347927480 (code B ref 30202); Mon, 22 Jan 2018 15:05:02 +0000 Original-Received: (at 30202) by debbugs.gnu.org; 22 Jan 2018 15:04:39 +0000 Original-Received: from localhost ([127.0.0.1]:38289 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eddeJ-000799-3T for submit@debbugs.gnu.org; Mon, 22 Jan 2018 10:04:39 -0500 Original-Received: from mail-pg0-f54.google.com ([74.125.83.54]:36464) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eddeG-00078t-Uo for 30202@debbugs.gnu.org; Mon, 22 Jan 2018 10:04:37 -0500 Original-Received: by mail-pg0-f54.google.com with SMTP id k68so7293110pga.3 for <30202@debbugs.gnu.org>; Mon, 22 Jan 2018 07:04:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=usAZyhin2nXMXzHIhdFRqh2DLCIqMju4hmO3rMjl0Cw=; b=Xv+5wHzEV0ZJvOQeT4ny4nrnZVhl9q3gAQvtoYpmYpZoM4XitpqVt9bodwRgDPcri0 yF+kFx5kLnpzyXPPOxadvdllGK4f/HHZ0wGWWyMoLqPFGehghJp3cWPf5Bqmhg0P/o7W suHHLeXA4uAF8UgwJSoVGSrPq5i5uErCkzt+D7ucQOAoZAnnIU27KDptK+EaYyfgCJay PUT5pYLMrJKgsnCNndOffu/1aFMqcqcG4d/ngFrO9egRSs4zwENbyeZwI5TZ8sHGpR6U IMw6oC/IwEJIu4ef+AjOwt1HT0mGxNAVKUVZsl/tcOdgtNR6GcexL/jJ6L6G6JDPb7IQ w3uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=usAZyhin2nXMXzHIhdFRqh2DLCIqMju4hmO3rMjl0Cw=; b=slezWQZF9GZmtApXM+hWZ00naNXgsahevVqJ97ONog7ISfKWcaLCH25IJNLUSlftJB oD6bwA+8Z86dl7iIKDoLCcFclTzNU5DaiJ8gbFD9XdwP0H63kxKWPF3nbwcuPOyx3bhB XZU4AldNt7DtAiLo4eFZHGHiGcpyrBSr6dMLoNRwGfFDyI43tUpJDruyxYS1t2iogeNb HW5JPJTy+P2XWbyqAxx1IECtX81ATSQjlF1h7LdoJc57pV41ShGcSAaPwK5dlrEhUlYF cTP18IhaW6JWrJ1H6xPqgIWpmdpzQeX+NGk0yp7V13Qg72tizmt9xL5C2dUUkwkmX1zD Lt/w== X-Gm-Message-State: AKwxytcWCRAkLeewGSA+1AMBPPRYw3nTQ/r9ALCE3uqKu8rrlfepEWzU q/xRS7MZICaUj7FIYokAv1Y= X-Google-Smtp-Source: AH8x225N4knJ+Js7KSK45YMb4QxzLKHIJ6KkPeQ19b7SL4vnRyU6eSgAEaiaU2Z1oFN2cmNyEmN/0Q== X-Received: by 10.101.81.7 with SMTP id f7mr7432028pgq.423.1516633468635; Mon, 22 Jan 2018 07:04:28 -0800 (PST) Original-Received: from calancha-pc (146.219.128.101.dy.bbexcite.jp. [101.128.219.146]) by smtp.gmail.com with ESMTPSA id w12sm901671pfd.86.2018.01.22.07.04.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Jan 2018 07:04:27 -0800 (PST) X-Google-Original-From: Tino Calancha X-X-Sender: calancha@calancha-pc In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:142372 Archived-At: On Mon, 22 Jan 2018, Stefan Monnier wrote: >> yesterday was added (how about to announce it in NEWS file?) >> the new function `assoc-delete-all', which is pretty much >> the same as `assq-delete-all' with `equal' instead of `eq'. >> How do you think below patch? > > How 'bout defining > > (defun assoc-delete-all (key alist &optional test) > (unless test (setq test #'equal)) > ...) Yes, I prefer this way. Thank you. Updated patch below. >> IIUC, the byte compiler will carry the substitution >> (funcall function foo bar) >> into >> (function foo bar) >> so that compile code won't suffer the funcall overhead, right? > > IIRC both forms result in the same bytecode except that the longer form > will not benefit from inlining and/or the use of specialized bytecodes > (e.g. when `foo` is `equal`). Here I tend to prefer to avoid the duplication of the similar code; in case that the gain of performance result critical, and considering that the functions are very short, then it might be OK repeat ourselves. --8<-----------------------------cut here---------------start------------->8--- commit 9ecce395b1f2def081187d3e430d0cb82af8ba01 Author: tino calancha Date: Mon Jan 22 23:53:27 2018 +0900 Code refactoring * lisp/subr.el (assoc-delete-all): Add optional arg TEST. (assq-delete-all): Use assoc-delete-all. diff --git a/lisp/subr.el b/lisp/subr.el index 092850a44d..e7a0ffc5be 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -705,17 +705,19 @@ member-ignore-case (setq list (cdr list))) list) -(defun assoc-delete-all (key alist) - "Delete from ALIST all elements whose car is `equal' to KEY. +(defun assoc-delete-all (key alist &optional test) + "Delete from ALIST all elements whose car is KEY. +Compare keys with TEST. Defaults to `equal'. Return the modified alist. Elements of ALIST that are not conses are ignored." + (unless test (setq test #'equal)) (while (and (consp (car alist)) - (equal (car (car alist)) key)) + (funcall test (caar alist) key)) (setq alist (cdr alist))) (let ((tail alist) tail-cdr) (while (setq tail-cdr (cdr tail)) (if (and (consp (car tail-cdr)) - (equal (car (car tail-cdr)) key)) + (funcall test (caar tail-cdr) key)) (setcdr tail (cdr tail-cdr)) (setq tail tail-cdr)))) alist) @@ -724,16 +726,7 @@ assq-delete-all "Delete from ALIST all elements whose car is `eq' to KEY. Return the modified alist. Elements of ALIST that are not conses are ignored." - (while (and (consp (car alist)) - (eq (car (car alist)) key)) - (setq alist (cdr alist))) - (let ((tail alist) tail-cdr) - (while (setq tail-cdr (cdr tail)) - (if (and (consp (car tail-cdr)) - (eq (car (car tail-cdr)) key)) - (setcdr tail (cdr tail-cdr)) - (setq tail tail-cdr)))) - alist) + (assoc-delete-all key alist #'eq)) (defun rassq-delete-all (value alist) "Delete from ALIST all elements whose cdr is `eq' to VALUE. --8<-----------------------------cut here---------------end--------------->8---