From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "John Wiegley" Newsgroups: gmane.emacs.devel Subject: New function for subr.el: sort-on Date: Fri, 24 Nov 2017 09:36:43 -0800 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1511545051 25422 195.159.176.226 (24 Nov 2017 17:37:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 24 Nov 2017 17:37:31 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (darwin) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Nov 24 18:37:27 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eIHum-0006AD-81 for ged-emacs-devel@m.gmane.org; Fri, 24 Nov 2017 18:37:24 +0100 Original-Received: from localhost ([::1]:50497 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIHut-0003Hr-Ci for ged-emacs-devel@m.gmane.org; Fri, 24 Nov 2017 12:37:31 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35829) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIHuG-0003Go-Q5 for emacs-devel@gnu.org; Fri, 24 Nov 2017 12:36:53 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eIHuC-00038C-Qy for emacs-devel@gnu.org; Fri, 24 Nov 2017 12:36:52 -0500 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:35869) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIHuC-00037K-OZ for emacs-devel@gnu.org; Fri, 24 Nov 2017 12:36:48 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:37105) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1eIHuC-0007tz-Fz for emacs-devel@gnu.org; Fri, 24 Nov 2017 12:36:48 -0500 Original-Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 6E92920BFD for ; Fri, 24 Nov 2017 12:36:47 -0500 (EST) Original-Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Fri, 24 Nov 2017 12:36:47 -0500 X-ME-Sender: Original-Received: from localhost (76-234-69-149.lightspeed.frokca.sbcglobal.net [76.234.69.149]) by mail.messagingengine.com (Postfix) with ESMTPA id B1917240F6 for ; Fri, 24 Nov 2017 12:36:46 -0500 (EST) Mail-Followup-To: emacs-devel@gnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:220432 Archived-At: This can be more memory efficient than using `sort' with a predicate, since it computes each value used by the predicate only once. If the predicate only needs to access sub-elements, and not do any computation, there may be no advantage. --8<---------------cut here---------------start------------->8--- (defsubst sort-on (seq predicate accessor) "Sort SEQ using PREDICATE applied to values returned by ACCESSOR. This implements the so-called Schwartzian transform, which has the performance advantage of applying ACCESSOR at most once per element in the list, as opposed to using `sort' with a PREDICATE that applies the ACCESSOR. Note: this function is only a win over `sort' if ACCESSOR is compute-intensive; otherwise, it uses more intermediate cons cells than regular `sort', and so represents a memory for CPU tradeoff." (mapcar #'cdr (sort (mapcar #'(lambda (x) (cons (funcall accessor x) x)) seq) #'(lambda (x y) (funcall predicate (car x) (car y)))))) (defun sort-on* (seq predicate accessor) "Sort SEQ using PREDICATE applied to values returned by ACCESSOR. This is a destructive version of `sort-on', which attempts to reuse storage as much as possible." (let ((seq2 seq)) (while seq2 (setcar seq2 (cons (funcall accessor (car seq2)) (car seq2))) (setq seq2 (cdr seq2)))) (setq seq (sort* seq #'(lambda (x y) (funcall predicate (car x) (car y))))) (let ((seq2 seq)) (while seq2 (setcar seq2 (cdar seq2)) (setq seq2 (cdr seq2))) seq)) --8<---------------cut here---------------end--------------->8--- -- John Wiegley GPG fingerprint = 4710 CF98 AF9B 327B B80F http://newartisans.com 60E1 46C4 BD1A 7AC1 4BA2