From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.devel Subject: Re: master 4b79c80c999 1/2: New function 'sort-on' Date: Wed, 6 Mar 2024 20:34:38 +0200 Message-ID: <3ef80237-e393-450a-a87f-7c588424f29f@gutov.dev> References: <170688047526.14693.2994051491358257471@vcs2.savannah.gnu.org> <20240202132756.4272CC0EFE7@vcs2.savannah.gnu.org> <87cytej4hy.fsf@daniel-mendler.de> <86zfwi52m1.fsf@gnu.org> <87plxe28as.fsf@web.de> <86wmrm50i5.fsf@gnu.org> <3ff9d4cf-7b4f-4924-8663-3f43625760bf@gutov.dev> <87zfvldokn.fsf@web.de> <8b31204f-4e8c-4c56-bbc7-73c9bfabb651@gutov.dev> <87edcq8pgm.fsf@web.de> <6df7bc2e-4fc4-4ed8-8dfe-9b3b74c6ae8a@gutov.dev> <8734t5hzl2.fsf@web.de> <87frx5q8qp.fsf@web.de> <86v860luop.fsf@gnu.org> <877cighwqf.fsf@web.de> <864jdjlfxd.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17506"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla Thunderbird Cc: emacs-devel@gnu.org To: Eli Zaretskii , Michael Heerdegen , John Wiegley Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Mar 06 19:35:50 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rhw7I-0004Bf-IY for ged-emacs-devel@m.gmane-mx.org; Wed, 06 Mar 2024 19:35:49 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhw6M-0003ya-7i; Wed, 06 Mar 2024 13:34:50 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhw6K-0003yJ-6c for emacs-devel@gnu.org; Wed, 06 Mar 2024 13:34:48 -0500 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhw6I-0007Ho-4u; Wed, 06 Mar 2024 13:34:47 -0500 Original-Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 71A2C32009F9; Wed, 6 Mar 2024 13:34:41 -0500 (EST) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Wed, 06 Mar 2024 13:34:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1709750080; x=1709836480; bh=DObuhC5jFVO3tubl3dK1b9jqooCVu0LXhHL69frFnec=; b= TthRzglg+LgUq9ip4h2BKHzdVvvK7DmG3d+daei/akpMXQ3LYdEneEprzbpkPNHK iheoq6O5tJez+Y35F+6Ya9Y8DUw0jdpvQwRi+4KVlVyfekmOIikIOovItM6MrVbG f+iRILx0PYWHA23veF/cML0lvEYTbtjOEwwi7ze1umNCTJpoZl7oBoIGd+chunLn /MfIvuGwOyZ41sThQIcTm3ac8JdIkR8SlToiPcYpI8p9OUV0d8/BlgvgQS6OvX6n 6DCFEGYAK1LkIxODm5IJU0P3AhCCMV8YKkMYn+m1eXgVfn1oxyashBY+kghIp4XC eT1aZnbsYiJqkBnmzw9QrQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1709750080; x= 1709836480; bh=DObuhC5jFVO3tubl3dK1b9jqooCVu0LXhHL69frFnec=; b=L rfJYYS5vrjhAHJUzYKmSYVHTOLto+NP6U3dzw5xMjC/OD0/eBLATGCIrnt1DVnX8 JB3iwxxRZsP+BeNknky2DvHB7QKwzTVPgSxhBzVbWyDqcDRZbg66M2+gihsS9ORT hqj41GUcIENeLndzvllYn0qOmES/PKCfYuIgsZQM8q4POy7cB/oEsKAUfowinOMp pC2Rt+JTOYJANtXNMiw+n4b/mXyYwnnfiEdg+xiTbRyQCNAKSBiwCRChEgGdgfSf b5vUTv4lxPeyf7EQq9suhrVA2V9UCe7FF8dFu0r4ROJVVVtd4+4UCVp3qI9jAOU1 VPJLKB7cTaF/JnqWBAI8A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledriedugdduudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtvdejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhepteduleejgeehtefgheegjeekueehvdevieekueeftddvtdevfefhvdevgedu jeehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepug hmihhtrhihsehguhhtohhvrdguvghv X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 6 Mar 2024 13:34:39 -0500 (EST) Content-Language: en-US In-Reply-To: <864jdjlfxd.fsf@gnu.org> Received-SPF: pass client-ip=64.147.123.19; envelope-from=dmitry@gutov.dev; helo=wout3-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:316862 Archived-At: On 06/03/2024 14:10, Eli Zaretskii wrote: >> From: Michael Heerdegen >> Cc:emacs-devel@gnu.org >> Date: Wed, 06 Mar 2024 04:20:40 +0100 >> >> Eli Zaretskii writes: >> >>> Any way you can think of rewriting this so that it's easier to read >>> and understand, i.e. with less macrology? >> To make the code readable on wants to factor out the operation of >> replacing the list elements destructively, because that is done multiple >> times and the main aspect. The standard way would be to use helper >> functions, but that would make the code less efficient due to lambdas, >> or require several top-level definitions, which would be nonsense for >> such a small defun. So I did the factoring using a local macro. >> >> The expanded definition would look like this: >> >> #+begin_src emacs-lisp >> (defun sort-on (sequence predicate accessor) >> (let* ((l (sort >> (let ((ret sequence)) >> (while sequence >> (setcar sequence >> (let* ((elt (car sequence))) >> (cons elt (funcall accessor elt)))) >> (setq sequence (cdr sequence))) >> ret) >> #'(lambda (x y) (funcall predicate (cdr x) (cdr y))))) >> (ret l)) >> (while l >> (setcar l (let* ((elt (car l))) (car elt))) (setq l (cdr l))) >> ret)) >> #+end_src >> >> You would prefer that? But now the operations on the list elements are >> spread over all the code. Not a good style. It depends on the reader >> which version is easier to understand, but from all I learned about >> coding the macro-based version is better and easier to understand. >> >> To make my sort predicate building code as efficient as possible (as had >> been requested), I will also have to rely on some form of code rewriting >> like this. >> >> >> And Dmitry Gutov wrote: >> >>> But if we're going to merge this functionality into the core 'sort', >>> then I guess the new code would have to move to src/fns.c, and it >>> might be difficult to use macros there. >> Anyone please feel free to do this. Might be better to simply code this >> in C. > Perhaps John (CC'ed) has some comments about these matters. Here's a version from Daniel as well: (defun sort-on (list predicate &optional accessor) (let ((pred (cond (accessor (cl-loop for x in-ref list do (setf x (cons x (funcall accessor x)))) (lambda (x y) (funcall predicate (cdr x) (cdr y)))) (t predicate)))) (setq list (sort list pred)) (when accessor (cl-loop for x in-ref list do (setf x (car x)))) list)) It should be easier to grok and ultimately translate to C.