all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: Daniel Mendler <mail@daniel-mendler.de>
Cc: 69709@debbugs.gnu.org, "Dmitry Gutov" <dmitry@gutov.dev>,
	"Eli Zaretskii" <eliz@gnu.org>,
	"Gerd Möllmann" <gerd.moellmann@gmail.com>,
	"Stefan Monnier" <monnier@iro.umontreal.ca>
Subject: bug#69709: `sort` interface improvement and universal ordering predicate
Date: Fri, 29 Mar 2024 12:52:37 +0100	[thread overview]
Message-ID: <46794723-8324-41DD-8A1F-C1206A8EAFC2@gmail.com> (raw)
In-Reply-To: <87edbtnu8i.fsf@daniel-mendler.de>

[-- Attachment #1: Type: text/plain, Size: 588 bytes --]

29 mars 2024 kl. 12.38 skrev Daniel Mendler <mail@daniel-mendler.de>:

> I would
> like to back-port the new feature in the Compat library, specifically in
> the emacs-30 branch of the Compat repository. I've seen you mentioned
> some pure Lisp code, implementing the new feature, in a slower, but
> backward compatible way, which we could perhaps use?

Here is my proof-of-concept hack that maybe you could use as starting point. It's incomplete but I think you can fill in the blanks. You could use the tests in fns-tests.el, perhaps adapted to fit. Let us know how it goes.


[-- Attachment #2: compatsort.el --]
[-- Type: application/octet-stream, Size: 2787 bytes --]

;;; -*- lexical-binding: t -*-

;; Quick-and-dirty compat backport of Emacs 30 `sort' keyword args to
;; older versions

(defun value< (a b)
  "Cheapo Lisp impl of Emacs 30 `value<'."
  (cond
   ((numberp a) (< a b))
   ((stringp a) (string< a b))
   ((symbolp a)
    (or (and (null a) (consp b))
        (string< a b)))
   ((consp a)
    (while (and (consp b) (equal (car a) (car b)))
      (setq a (cdr a))
      (setq b (cdr b)))
    (if (consp a)
        (if (consp b)
            (value< (car a) (car b))
          (if b
              (error "value< type mismatch: %S %S" a b)
            nil))
      (value< a b)))
   ((vectorp a)
    (if (vectorp b)
        (let* ((na (length a))
               (nb (length b))
               (n (min na nb))
               (i 0))
          (while (and (< i n) (equal (aref a i) (aref b i)))
            (setq i (1+ i)))
          (if (< i n)
              (value< (aref a i) (aref b i))
            (< n nb)))
      (error "value< type mismatch: %S %S" a b)))
   ;; FIXME: records, markers, buffers, processes, bool-vectors
   (t (if (eq (type-of a) (type-of b))
          nil
        (error "value< type mismatch: %S %S" a b)))))

(defun sort-30 (seq key lessp reverse in-place)
  (unless lessp
    (setq lessp #'value<))
  ;; careful, can't just assign to lessp here -- use a new binding
  (let ((new-lessp
         (if key
             ;; FIXME: this is a bit inefficient if `key' is expensive
             (lambda (a b) (funcall lessp (funcall key a) (funcall key b)))
           lessp)))
    (unless in-place
      (setq seq (copy-sequence seq)))
    ;; FIXME: this is actually not quite correct when sorting lists
    ;; in-place -- we should really update the original list.
    (if reverse
        (nreverse (sort (if in-place (nreverse seq) (reverse seq))
                        new-lessp))
      (sort seq new-lessp))))

(defun sort-compat-transformer (form &rest args)
  "Compiler macro for `sort' that takes Emacs 30 keyword args."
  (let ((nargs (length args)))
    (if (zerop (% nargs 2))
        form                    ; old syntax, or error -- no transform
      ;; new syntax
      (let ((seq (car args))
            (key nil)
            (in-place nil)
            (reverse nil)
            (lessp nil))
        (setq args (cdr args))
        (while (cdr args)
          (let ((kw (car args))
                (val (cadr args)))
            (pcase kw
              (:key (setq key val))
              (:lessp (setq lessp val))
              (:reverse (setq reverse val))
              (:in-place (setq in-place val))
              (_ (error "bad `sort' keyword `%s'" kw))))
          (setq args (cddr args)))
        `(sort-30 ,seq ,key ,lessp ,reverse ,in-place)))))
    
(put 'sort 'compiler-macro 'sort-compat-transformer)

  reply	other threads:[~2024-03-29 11:52 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-10 13:28 bug#69709: `sort` interface improvement and universal ordering predicate Mattias Engdegård
2024-03-10 14:09 ` Eli Zaretskii
2024-03-10 14:56   ` Mattias Engdegård
2024-03-20 19:01     ` Mattias Engdegård
2024-03-20 19:37       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-21 14:55         ` Mattias Engdegård
2024-03-21 14:54       ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-22 20:55       ` Dmitry Gutov
2024-03-23 14:58         ` Mattias Engdegård
2024-03-23 17:39           ` Dmitry Gutov
2024-03-23 20:09             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-23 23:19               ` Dmitry Gutov
2024-03-23 23:25                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-25 11:11                   ` Mattias Engdegård
2024-03-29 10:59                     ` Mattias Engdegård
2024-03-29 11:38                       ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-29 11:52                         ` Mattias Engdegård [this message]
2024-05-17 12:29                           ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-17 17:49                             ` Mattias Engdegård
2024-03-29 12:06                       ` Eli Zaretskii
2024-03-29 15:02                         ` Mattias Engdegård
2024-03-29 15:35                           ` Eli Zaretskii
2024-03-29 16:13                             ` Mattias Engdegård
2024-03-29 18:09                               ` Eli Zaretskii
2024-03-10 15:48 ` Dmitry Gutov
2024-03-10 15:56   ` Mattias Engdegård
2024-03-10 16:03     ` Dmitry Gutov
2024-03-10 16:46       ` Mattias Engdegård
2024-03-10 16:55         ` Eli Zaretskii
2024-03-10 17:54           ` Dmitry Gutov
2024-03-11  7:01 ` Gerd Möllmann
2024-04-14 14:03 ` Aris Spathis
2024-04-14 16:26   ` Eli Zaretskii
2024-04-14 16:33     ` Mattias Engdegård

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=46794723-8324-41DD-8A1F-C1206A8EAFC2@gmail.com \
    --to=mattias.engdegard@gmail.com \
    --cc=69709@debbugs.gnu.org \
    --cc=dmitry@gutov.dev \
    --cc=eliz@gnu.org \
    --cc=gerd.moellmann@gmail.com \
    --cc=mail@daniel-mendler.de \
    --cc=monnier@iro.umontreal.ca \
    /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.