From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: master 4b79c80c999 1/2: New function 'sort-on' Date: Wed, 06 Mar 2024 14:10:06 +0200 Message-ID: <864jdjlfxd.fsf@gnu.org> 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> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22959"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Michael Heerdegen , "John Wiegley" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Mar 06 13:10:59 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 1rhq6s-0005ka-Uw for ged-emacs-devel@m.gmane-mx.org; Wed, 06 Mar 2024 13:10:59 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhq6K-0002sC-K0; Wed, 06 Mar 2024 07:10:24 -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 1rhq6F-0002rs-CI for emacs-devel@gnu.org; Wed, 06 Mar 2024 07:10:19 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rhq66-0000aM-Ef; Wed, 06 Mar 2024 07:10:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=iD1uSxOZdgicofBvanreI4iqbLagtOpwE2fipcEbDfA=; b=rYfVNbwMzAbK mxSvqI2VyBB+/WHTezE5Mdvdnx5FyUP9IbNk/PYZ2TQN3EyC8k9/s/mNp6rbtwoXTclBt2teNu+fB 0L26lagD7fAiZdjlq8pxwv3fwO00GqXt8mPgPOO/Hur58e8KZohz5K/LTBqHay41SLbA8A712PWv1 OaA64VWRyfXXukeJAPB/wivx8U6w8dZsNqmltBnkcagDUxeCClhhsJGoXBQpI1HsL5NR/0tSIZ7b6 0G2I0UATZhHLjPy+71DUW6wzHIQzganPp20Iq3aKG1RXNVS8xVH81zPmkxw6C8p3SM/LpZg6V8Qbl g2JvlQuW3Dw2D2bhrMWFag==; In-Reply-To: <877cighwqf.fsf@web.de> (message from Michael Heerdegen on Wed, 06 Mar 2024 04:20:40 +0100) 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:316854 Archived-At: > 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.