From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#69709: `sort` interface improvement and universal ordering predicate Date: Fri, 29 Mar 2024 12:52:37 +0100 Message-ID: <46794723-8324-41DD-8A1F-C1206A8EAFC2@gmail.com> References: <86zfv6uqjn.fsf@gnu.org> <4391448A-C7AF-4D7F-8866-C0313956D52D@gmail.com> <8366111E-97C4-4839-AA1E-A577C81A6035@gmail.com> <4B7ACA81-DEB9-4878-BE0B-88A302AF7081@gmail.com> <2BB79019-C075-445C-A9E0-9D29929EA02A@gmail.com> <7219B5AB-4EA7-4050-92AE-7FB949D6D415@gmail.com> <87edbtnu8i.fsf@daniel-mendler.de> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_1827E7F0-089F-4DA4-AFE2-E086A78FE7A3" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18929"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 69709@debbugs.gnu.org, Dmitry Gutov , Eli Zaretskii , Gerd =?UTF-8?Q?M=C3=B6llmann?= , Stefan Monnier To: Daniel Mendler Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Mar 29 12:53:22 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1rqAnR-0004hv-IL for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 29 Mar 2024 12:53:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rqAnA-0003Ce-J8; Fri, 29 Mar 2024 07:53:04 -0400 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 1rqAn6-0003By-O7 for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 07:53:00 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rqAn6-00076d-FR for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 07:53:00 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rqAn7-0004wm-N1 for bug-gnu-emacs@gnu.org; Fri, 29 Mar 2024 07:53:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 29 Mar 2024 11:53:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69709 X-GNU-PR-Package: emacs Original-Received: via spool by 69709-submit@debbugs.gnu.org id=B69709.171171316918965 (code B ref 69709); Fri, 29 Mar 2024 11:53:01 +0000 Original-Received: (at 69709) by debbugs.gnu.org; 29 Mar 2024 11:52:49 +0000 Original-Received: from localhost ([127.0.0.1]:41826 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rqAmv-0004vp-C7 for submit@debbugs.gnu.org; Fri, 29 Mar 2024 07:52:49 -0400 Original-Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]:52255) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rqAmt-0004vZ-0s for 69709@debbugs.gnu.org; Fri, 29 Mar 2024 07:52:47 -0400 Original-Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-51381021af1so2718677e87.0 for <69709@debbugs.gnu.org>; Fri, 29 Mar 2024 04:52:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711713159; x=1712317959; darn=debbugs.gnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:sender:from:to:cc:subject:date:message-id:reply-to; bh=OBylIq5aIReUeJMgDsgysa+ZrwZbh8pep2+zpJG/XiI=; b=kmni/M+b2PaRdU0ocjOojKaJ0HmuWQvqIzNsNpEDUsLWh6PDO3S8GC1/IZfAf7Jke8 8w6kSrFD2sCI4GIO3P0YknUMUccVF1YIRw79zExItFvcAJkQ2+16i8CoYdjOvG2elOhv AptqAz198N73tu8tbEdggT8/y9pNZtxiHhACYEQlmHbK7/TSrLFXyGxCww2GDocpQ/Xm pQm7a6gBr0UwCS1Ydlp92sSbe2L03+sVSALuD3o8U3OsWzS1D/8d0umGJldWsTZ0furq haG0dHYbWuu9qDQjOf9bos4RDnnDeP8wWeTALP17HAoBIPPvS9UlA8Fw1da2+70HwnzM hM+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711713159; x=1712317959; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OBylIq5aIReUeJMgDsgysa+ZrwZbh8pep2+zpJG/XiI=; b=CK6Yc7SR6cTV+RWjgCpXdFcxs6JWVs5G5bQ1ZDfmhDmPc0NyPRMQYt5+n1BBs1F/rB r3JSJcbzl1TjEd0zJxhbrBf+m3drdgiXkww8dbodrVca544lZON+M16/Xe0JFxYqlT7L pXxB0PPunRqrU0f0uFMUq7E0BNue/FEKr+2zufQlppsBOfrRjnvA0aAHqLLo4zcfg3/u ic5Ujs4NR3M8mJEIbCPM41aWXysDLm2mSSjxgBRxOC9S/5k/8BwCwaN5wJnKN8LFLH/I yGoE3e2YVPpIpCrQyXKvw1by1tstFoSG5Y+djbVsk1CKEvplJ3Xse9qMzPpmBemGQ9HH FmbA== X-Forwarded-Encrypted: i=1; AJvYcCX2TuJIn0MAivejFo2lzKRviEMsBKUL6jIx62kcrtBFzp4Zd6CuX0ZEqlSKniQB/BQ2VUYMCyj6A8dDLO67Z+me2uOb5ZA= X-Gm-Message-State: AOJu0YxYNeJznf1SP6Q9C2ksUiR+PLa7OhnMZfpxvz5smpuGxKU0O2ir 8EAfk+z8RG8ENTdY+ZLADfZxHzQcCDmDxnrOdIpP1Cvb8LjZ25Bw X-Google-Smtp-Source: AGHT+IGFqEY2DnUwjfJEEKGYpx4WMSuXldX2f6ez3u9alU8oP9tb7lS3nSNpm+R9Ux6yEj+KLIg9sA== X-Received: by 2002:ac2:48ac:0:b0:513:e6db:5bee with SMTP id u12-20020ac248ac000000b00513e6db5beemr1485020lfg.64.1711713159271; Fri, 29 Mar 2024 04:52:39 -0700 (PDT) Original-Received: from smtpclient.apple (c80-217-1-132.bredband.tele2.se. [80.217.1.132]) by smtp.gmail.com with ESMTPSA id i34-20020a0565123e2200b00515d24b4760sm175281lfv.257.2024.03.29.04.52.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Mar 2024 04:52:38 -0700 (PDT) In-Reply-To: <87edbtnu8i.fsf@daniel-mendler.de> X-Mailer: Apple Mail (2.3654.120.0.1.15) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282286 Archived-At: --Apple-Mail=_1827E7F0-089F-4DA4-AFE2-E086A78FE7A3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 29 mars 2024 kl. 12.38 skrev Daniel Mendler : > 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. --Apple-Mail=_1827E7F0-089F-4DA4-AFE2-E086A78FE7A3 Content-Disposition: attachment; filename=compatsort.el Content-Type: application/octet-stream; x-unix-mode=0644; name="compatsort.el" Content-Transfer-Encoding: 7bit ;;; -*- 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) --Apple-Mail=_1827E7F0-089F-4DA4-AFE2-E086A78FE7A3--