# HG changeset patch # User Steve Losh # Date 1484845514 0 # Node ID 5dd997c404247dee64556b99c6f5d69ffde26d12 # Parent fbd9837856a842beb9fb7696a2d0ce1c5c093b65 Add `extrema` diff -r fbd9837856a8 -r 5dd997c40424 DOCUMENTATION.markdown --- a/DOCUMENTATION.markdown Tue Jan 17 21:33:28 2017 +0000 +++ b/DOCUMENTATION.markdown Thu Jan 19 17:05:14 2017 +0000 @@ -1403,6 +1403,19 @@ +### `EXTREMA` (function) + + (EXTREMA PREDICATE SEQUENCE) + +Return the smallest and largest elements of `sequence` according to `predicate` + + `predicate` should be a strict ordering predicate (e.g. `<`). + + Returns the smallest and largest elements in the sequence as two values, + respectively. + + + ### `FREQUENCIES` (function) (FREQUENCIES SEQUENCE &KEY (TEST 'EQL)) diff -r fbd9837856a8 -r 5dd997c40424 losh.lisp --- a/losh.lisp Tue Jan 17 21:33:28 2017 +0000 +++ b/losh.lisp Thu Jan 19 17:05:14 2017 +0000 @@ -1676,6 +1676,23 @@ (sequence (drop-seq n seq)))) +(defun extrema (predicate sequence) + "Return the smallest and largest elements of `sequence` according to `predicate` + + `predicate` should be a strict ordering predicate (e.g. `<`). + + Returns the smallest and largest elements in the sequence as two values, + 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))))) + + ;;;; Debugging & Logging ------------------------------------------------------ (defun pr (&rest args) "Print `args` readably, separated by spaces and followed by a newline. diff -r fbd9837856a8 -r 5dd997c40424 package.lisp --- a/package.lisp Tue Jan 17 21:33:28 2017 +0000 +++ b/package.lisp Thu Jan 19 17:05:14 2017 +0000 @@ -241,6 +241,7 @@ (defpackage :losh.sequences (:documentation "Utilities for operating on sequences.") (:export + :extrema :prefix-sums :frequencies :proportions