From: Drew Adams <drew.adams@oracle.com>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: Emacs Development <emacs-devel@gnu.org>
Subject: RE: [External] : Add a function for building sort predicates
Date: Fri, 2 Feb 2024 22:30:13 +0000 [thread overview]
Message-ID: <SJ0PR10MB54889AF243C34DE8CCC9062EF3422@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <87o7cyzl68.fsf@web.de>
> > See this for something quite similar:
> > https://www.emacswiki.org/emacs/ApplesAndOranges
>
> Thanks.
>
> I think my approach covers all of the cases
> mentioned there, at least when you specify
> sort predicate definitions in the rule list
> recursively.
Didn't mean to suggest otherwise. I like it.
The "top-level" bit of your code:
(catch result
;; any rule throws 'result' when a<b is
;; decidable with it
(dolist (rule rules) (funcall rule a b))
;; no rule was able to decide - return nil
;; for stable sorting
nil))
is similar to mine:
(defun bmkp-multi-sort (b1 b2)
(let ((preds (car bmkp-sort-comparer))
(final-pred (cadr bmkp-sort-comparer))
(result nil))
(when bmkp-reverse-multi-sort-p
(setq preds (reverse preds)))
(catch 'bmkp-multi-sort
(dolist (pred preds)
(setq result (funcall pred b1 b2))
(when (consp result)
(when bmkp-reverse-multi-sort-p
(setq result (list (not (car result)))))
(throw 'bmkp-multi-sort (car result))))
(and final-pred
(if bmkp-reverse-multi-sort-p
(not (funcall final-pred b1 b2))
(funcall final-pred b1 b2))))))
In fact, that's all the code I have for this.
User code just binds `bmkp-sort-comparer' to
a list of predicates (like your rules list).
That multi-sort function is called in one
place in my code for use with completion, and
in one place for use in a displayed bookmark
list (different libraries for those use cases).
It would similarly be used in a single place
in user code for a given sorting purpose.
Because I use a variable (well, two - one for
each of those use cases), code that wants a
different sort order for a given context can
just bind the variable to a different "rules"
list.
The code that sorts for completion, and the
code that sorts the bookmark list, each
invoke the function (above) that iterates
over the predicates.
But code that wants different sort orders
within either of those use cases typically
just extends or modifies the default "rules"
list for the use case (completion or buffer
sorting).
E.g., there are many kinds of completion,
which can call for different sort orders.
Each completion context (e.g. command) can
override the default sorting behavior just
by binding the comparer variable to its own
list of predicates (rules).
___
(Maybe consider providing an easy way to get
the reverse sort order, as a nice-to-have?)
prev parent reply other threads:[~2024-02-02 22:30 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-01 17:06 Add a function for building sort predicates Michael Heerdegen
2024-02-01 17:19 ` Daniel Mendler via Emacs development discussions.
2024-02-01 17:26 ` Eli Zaretskii
2024-02-01 18:10 ` Michael Heerdegen via Emacs development discussions.
2024-02-01 19:09 ` Eli Zaretskii
2024-02-02 20:11 ` Michael Heerdegen
2024-02-03 2:55 ` Emanuel Berg
2024-02-03 7:08 ` Eli Zaretskii
2024-02-03 10:35 ` Michael Heerdegen
2024-02-01 18:04 ` Michael Heerdegen via Emacs development discussions.
2024-02-01 18:23 ` Daniel Mendler via Emacs development discussions.
2024-02-01 19:22 ` Michael Heerdegen
2024-02-01 20:19 ` Daniel Mendler via Emacs development discussions.
2024-02-01 22:48 ` [External] : " Drew Adams
2024-02-02 20:05 ` Michael Heerdegen
2024-02-02 22:30 ` Drew Adams [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=SJ0PR10MB54889AF243C34DE8CCC9062EF3422@SJ0PR10MB5488.namprd10.prod.outlook.com \
--to=drew.adams@oracle.com \
--cc=emacs-devel@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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).