1745fd2d70d6

Merge.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 05 Dec 2019 19:42:14 -0500
parents 19519aba4cb5 (diff) a85855efde4a (current diff)
children 18da0214d5a9
branches/tags (none)
files

Changes

--- a/src/sequences.lisp	Thu Dec 05 19:35:58 2019 -0500
+++ b/src/sequences.lisp	Thu Dec 05 19:42:14 2019 -0500
@@ -223,7 +223,7 @@
     (sequence (drop-while-seq predicate seq))))
 
 
-(defun extrema (predicate sequence)
+(defun extrema (predicate sequence &key (key #'identity))
   "Return the smallest and largest elements of `sequence` according to `predicate`.
 
   `predicate` should be a strict ordering predicate (e.g. `<`).
@@ -232,12 +232,16 @@
   respectively.
 
   "
-  (iterate (with min = (elt sequence 0))
-           (with max = (elt sequence 0))
-           (for el :in-whatever sequence)
-           (when (funcall predicate el min) (setf min el))
-           (when (funcall predicate max el) (setf max el))
-           (finally (return (values min max)))))
+  (iterate
+    (with min = (elt sequence 0))
+    (with min-val = (funcall key min))
+    (with max = (elt sequence 0))
+    (with max-val = (funcall key max))
+    (for el :in-whatever sequence)
+    (for val = (funcall key el))
+    (when (funcall predicate val min-val) (setf min el min-val val))
+    (when (funcall predicate max-val val) (setf max el max-val val))
+    (finally (return (values min max)))))
 
 
 (defun enumerate (sequence &key (start 0) (step 1) key)