[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [External] : Add a function for building sort predicates
From: |
Drew Adams |
Subject: |
RE: [External] : Add a function for building sort predicates |
Date: |
Fri, 2 Feb 2024 22:30:13 +0000 |
> > 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?)
- Re: Add a function for building sort predicates, (continued)
Re: Add a function for building sort predicates, Michael Heerdegen, 2024/02/01
RE: [External] : Add a function for building sort predicates, Drew Adams, 2024/02/01