# HG changeset patch # User Steve Losh # Date 1575592934 18000 # Node ID 1745fd2d70d6f8a4c9b24300f7c7f3f7bf22f0e4 # Parent 19519aba4cb546d3dd000c5bdec648a499af41f8# Parent a85855efde4af5cbd6d00682f8073679d54738ad Merge. diff -r a85855efde4a -r 1745fd2d70d6 src/sequences.lisp --- 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)