From: Dmitry Gutov <dmitry@gutov.dev>
To: Eli Zaretskii <eliz@gnu.org>,
Michael Heerdegen <michael_heerdegen@web.de>,
John Wiegley <johnw@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: master 4b79c80c999 1/2: New function 'sort-on'
Date: Wed, 6 Mar 2024 20:34:38 +0200 [thread overview]
Message-ID: <3ef80237-e393-450a-a87f-7c588424f29f@gutov.dev> (raw)
In-Reply-To: <864jdjlfxd.fsf@gnu.org>
On 06/03/2024 14:10, Eli Zaretskii wrote:
>> From: Michael Heerdegen<michael_heerdegen@web.de>
>> Cc:emacs-devel@gnu.org
>> Date: Wed, 06 Mar 2024 04:20:40 +0100
>>
>> Eli Zaretskii<eliz@gnu.org> 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<dmitry@gutov.dev> 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.
next prev parent reply other threads:[~2024-03-06 18:34 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <170688047526.14693.2994051491358257471@vcs2.savannah.gnu.org>
[not found] ` <20240202132756.4272CC0EFE7@vcs2.savannah.gnu.org>
2024-02-02 15:00 ` master 4b79c80c999 1/2: New function 'sort-on' Daniel Mendler via Emacs development discussions.
2024-02-02 15:04 ` Eli Zaretskii
2024-02-02 15:26 ` Daniel Mendler via Emacs development discussions.
2024-02-02 15:47 ` Eli Zaretskii
2024-02-02 16:05 ` Daniel Mendler via Emacs development discussions.
2024-02-05 12:14 ` Michael Heerdegen
2024-02-02 15:55 ` Dmitry Gutov
2024-02-02 15:30 ` Michael Heerdegen via Emacs development discussions.
2024-02-02 15:35 ` Daniel Mendler via Emacs development discussions.
2024-02-02 16:08 ` Michael Heerdegen via Emacs development discussions.
2024-02-02 16:23 ` Daniel Mendler via Emacs development discussions.
2024-02-02 16:43 ` Michael Heerdegen via Emacs development discussions.
2024-02-02 15:50 ` Eli Zaretskii
2024-02-02 16:06 ` Eshel Yaron
2024-02-02 16:34 ` Eli Zaretskii
2024-02-02 16:46 ` Michael Heerdegen via Emacs development discussions.
2024-02-02 17:55 ` Emanuel Berg
2024-02-05 0:48 ` Dmitry Gutov
2024-02-05 5:30 ` Yuri Khan
2024-02-05 12:52 ` Eli Zaretskii
2024-02-05 13:25 ` Dmitry Gutov
2024-02-05 14:31 ` Eli Zaretskii
2024-02-05 14:47 ` Dmitry Gutov
2024-02-05 15:10 ` Eli Zaretskii
2024-02-05 15:29 ` Dmitry Gutov
2024-02-28 7:40 ` Michael Heerdegen
2024-03-01 23:37 ` Dmitry Gutov
2024-03-04 6:45 ` Michael Heerdegen
2024-03-04 16:43 ` Dmitry Gutov
2024-03-05 8:06 ` Michael Heerdegen
2024-03-05 10:21 ` Michael Heerdegen via Emacs development discussions.
2024-03-05 12:39 ` Eli Zaretskii
2024-03-06 3:20 ` Michael Heerdegen
2024-03-06 12:10 ` Eli Zaretskii
2024-03-06 18:34 ` Dmitry Gutov [this message]
2024-03-06 20:12 ` John Wiegley
2024-03-07 1:34 ` Dmitry Gutov
2024-03-05 12:44 ` Dmitry Gutov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3ef80237-e393-450a-a87f-7c588424f29f@gutov.dev \
--to=dmitry@gutov.dev \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=johnw@gnu.org \
--cc=michael_heerdegen@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.