From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: master 4b79c80c999 1/2: New function 'sort-on' Date: Wed, 06 Mar 2024 04:20:40 +0100 Message-ID: <877cighwqf.fsf@web.de> 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> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21596"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Mar 06 04:21:17 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 1rhhqG-0005T1-T8 for ged-emacs-devel@m.gmane-mx.org; Wed, 06 Mar 2024 04:21:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhhpU-0002hQ-2U; Tue, 05 Mar 2024 22:20:28 -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 1rhhpJ-0002fk-KO for emacs-devel@gnu.org; Tue, 05 Mar 2024 22:20:18 -0500 Original-Received: from mout.web.de ([212.227.15.3]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhhpG-00035x-E6; Tue, 05 Mar 2024 22:20:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1709695209; x=1710300009; i=michael_heerdegen@web.de; bh=BuZWR68FgHn7+AvLwEf3oITsNpvS8jKkQpBTfl3mCsA=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References: Date; b=tg3CcltGTwsRc2Xx3EBblcHet31WQ4BJ7eQAAOz299bVvV847NSWkwyyrAyQl6Jx TAfyDVrvZ9to7A9NOcbGOBFwS1F8KmiwV/3G5lLRJQZKw0BJ6TsZ3cKS1nOrAh3BQ svZWxsBYLjoaDtMqcl5HIXPJ+FwHkPaUZ6mAROLlaA9tlNefjDpPH1Lt1uKWTcyY0 m5VPvy+HKQKQYE7krXgyfbd6J7iU0Xmp4n6DaecWEYy6vf87GLgdxoRyBjjKMV0Z9 O2TLmN1QXuZyy+EfQUt1pGdkQv4ThSO9iXZMn5yUR6JgPsjalaX1uDSbgWEKBk4QN q9EE32hUhJT3/g+DaQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Original-Received: from drachen.dragon ([92.76.229.82]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1McZnl-1r9D1A36y5-00cX0D; Wed, 06 Mar 2024 04:20:09 +0100 In-Reply-To: <86v860luop.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 05 Mar 2024 14:39:02 +0200") X-Provags-ID: V03:K1:xq6EK6EjtQYC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ICf32C3o Received-SPF: pass client-ip=212.227.15.3; envelope-from=michael_heerdegen@web.de; helo=mout.web.de 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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:316846 Archived-At: 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. Michael.