Skip to content

Commit

Permalink
or-min or-max tests and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjudd committed Mar 16, 2011
1 parent d1cb198 commit 4fd3c36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/useful.clj
Expand Up @@ -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."
Expand Down
12 changes: 12 additions & 0 deletions test/useful_test.clj
Expand Up @@ -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]))
Expand Down

0 comments on commit 4fd3c36

Please sign in to comment.