5dd997c40424
Add `extrema`
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Thu, 19 Jan 2017 17:05:14 +0000 |
parents | fbd9837856a8 |
children | d25b6f52fad1 |
branches/tags | (none) |
files | DOCUMENTATION.markdown losh.lisp package.lisp |
Changes
--- 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))
--- 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.