diff --git a/src/useful.clj b/src/useful.clj index 7a03268..93e7a19 100644 --- a/src/useful.clj +++ b/src/useful.clj @@ -22,14 +22,16 @@ map)))) (defn or-min - "The minimium value of a and b, or whichever is not nil." - [a b] - (min (or a b) (or b a))) + "The minimium value of vals, ignoring nils." + [& vals] + (when-let [vals (seq (remove nil? vals))] + (apply min vals))) (defn or-max - "The maximum value of a and b, or whichever is not nil." - [a b] - (max (or a b) (or b a))) + "The maximum value of vals, ignoring nils." + [& vals] + (when-let [vals (seq (remove nil? vals))] + (apply max vals))) (defn conj-vec "Conj onto collection ensuring it is a vector." diff --git a/test/useful_test.clj b/test/useful_test.clj index 946132b..2fe7eab 100644 --- a/test/useful_test.clj +++ b/test/useful_test.clj @@ -14,6 +14,18 @@ (assoc-or :b 2) (assoc-or :c 3))))) +(deftest test-or-min + (is (= 3 (or-min nil 4 3 nil 9))) + (is (= 1 (or-min 1 2 3 4))) + (is (= 1 (or-min 1 nil))) + (is (= nil (or-min nil nil nil)))) + +(deftest test-or-max + (is (= 9 (or-max nil 4 3 nil 9))) + (is (= 4 (or-max 1 2 3 4))) + (is (= 1 (or-max 1 nil))) + (is (= nil (or-max nil nil nil)))) + (deftest test-conj-vec (is (= (conj-vec nil 5) [5])) (is (= (conj-vec '(4) 5) [4 5]))