all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Any interest in a function like this to add to subr.el?
@ 2016-10-18 17:53 John Wiegley
  2016-10-18 20:13 ` Dmitry Gutov
  2016-10-19 13:46 ` Andy Moreton
  0 siblings, 2 replies; 13+ messages in thread
From: John Wiegley @ 2016-10-18 17:53 UTC (permalink / raw)
  To: emacs-devel

(defun sort-on (seq predicate accessor)
  "Sort SEQ use 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))))))

-- 
John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-10-20  1:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-18 17:53 Any interest in a function like this to add to subr.el? John Wiegley
2016-10-18 20:13 ` Dmitry Gutov
2016-10-18 20:34   ` John Wiegley
2016-10-18 21:09     ` Dmitry Gutov
2016-10-18 21:25       ` John Wiegley
2016-10-19  5:01         ` Ken Raeburn
2016-10-19  6:02           ` John Wiegley
2016-10-19 14:51         ` Stefan Monnier
2016-10-19 14:48       ` Stefan Monnier
2016-10-19 23:12         ` Dmitry Gutov
2016-10-20  1:48           ` Stefan Monnier
2016-10-19 13:46 ` Andy Moreton
2016-10-19 14:52   ` Stefan Monnier

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.