diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/contrib/greatest_least.clj | 62 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/greatest_least.clj | 65 |
2 files changed, 127 insertions, 0 deletions
diff --git a/src/clojure/contrib/greatest_least.clj b/src/clojure/contrib/greatest_least.clj new file mode 100644 index 00000000..10c7eae6 --- /dev/null +++ b/src/clojure/contrib/greatest_least.clj @@ -0,0 +1,62 @@ +(ns clojure.contrib.greatest-least) + +(defn- boundary + [cmp-fn f & args] + (when args + (reduce (fn [a b] (if (cmp-fn (compare (f b) (f a))) + b + a)) args))) + +(defn greatest-by + "Return the argument for which f yields the greatest value." + [f & args] + (apply boundary pos? f args)) + +(defn greatest + "Return the greatest argument." + [& args] + (apply greatest-by identity args)) + +(defn least-by + "Return the argument for which f yields the smallest value." + [f & args] + (apply boundary neg? f args)) + +(defn least + "Return the smallest element." + [& args] + (apply least-by identity args)) + + +(defn- boundary-all + [cmp-fn f & args] + (when args + (reduce (fn [a b] + (if (nil? a) + (cons b nil) + (let [x (compare (f b) (f (first a)))] + (cond (zero? x) (cons b a) + (cmp-fn x) (cons b nil) + :else a)))) + nil + args))) + +(defn all-greatest-by + "Return all the elements for which f yields the greatest value." + [f & args] + (apply boundary-all pos? f args)) + +(defn all-greatest + "Returns all the greatest elements." + [& args] + (apply all-greatest-by identity args)) + +(defn all-least-by + "Return all the elements for which f yields the least value." + [f & args] + (apply boundary-all neg? f args)) + +(defn all-least + "Returns all the least elements." + [& args] + (apply all-least-by identity args)) diff --git a/src/clojure/contrib/test_contrib/greatest_least.clj b/src/clojure/contrib/test_contrib/greatest_least.clj new file mode 100644 index 00000000..557c0a3c --- /dev/null +++ b/src/clojure/contrib/test_contrib/greatest_least.clj @@ -0,0 +1,65 @@ +(ns clojure.contrib.test-contrib.greatest-least + (:use clojure.contrib.greatest-least + [clojure.contrib.test-is :only (is deftest run-tests)])) + +(deftest test-greatest + (is (nil? (greatest)) "greatest with no arguments is nil") + (is (= 1 (greatest 1))) + (is (= 2 (greatest 1 2))) + (is (= 2 (greatest 2 1))) + (is (= "b" (greatest "aa" "b")))) + +(deftest test-greatest-by + (is (nil? (greatest-by identity)) "greatest-by with no arguments is nil") + (is (= "" (greatest-by count ""))) + (is (= "a" (greatest-by count "a" ""))) + (is (= "a" (greatest-by count "" "a"))) + (is (= "aa" (greatest-by count "aa" "b")))) + +(deftest test-least + (is (nil? (least)) "least with no arguments is nil") + (is (= 1 (least 1))) + (is (= 1 (least 1 2))) + (is (= 1 (least 2 1))) + (is (= "aa" (least "aa" "b")))) + +(deftest test-least-by + (is (nil? (least-by identity)) "least-by with no arguments is nil") + (is (= "" (least-by count ""))) + (is (= "" (least-by count "a" ""))) + (is (= "" (least-by count "" "a"))) + (is (= "b" (least-by count "aa" "b")))) + +(deftest test-all-greatest + (is (nil? (all-greatest)) "all-greatest with no arguments is nil") + (is (= (list 1) (all-greatest 1))) + (is (= (list 1 1) (all-greatest 1 1))) + (is (= (list 2) (all-greatest 2 1 1))) + (is (= (list 2) (all-greatest 1 2 1))) + (is (= (list 2) (all-greatest 1 1 2))) + (is (= (list :c) (all-greatest :b :c :a)))) + +(deftest test-all-greatest-by + (is (nil? (all-greatest-by identity)) "all-greatest-by with no arguments is nil") + (is (= (list "a")) (all-greatest-by count "a")) + (is (= (list "a" "a")) (all-greatest-by count "a" "a")) + (is (= (list "aa")) (all-greatest-by count "aa" "b")) + (is (= (list "aa")) (all-greatest-by count "b" "aa" "c")) + (is (= (list "cc" "aa")) (all-greatest-by count "aa" "b" "cc"))) + +(deftest test-all-least + (is (nil? (all-least)) "all-least with no arguments is nil") + (is (= (list 1) (all-least 1))) + (is (= (list 1 1) (all-least 1 1))) + (is (= (list 1 1) (all-least 2 1 1))) + (is (= (list 1 1) (all-least 1 2 1))) + (is (= (list 1 1) (all-least 1 1 2))) + (is (= (list :a) (all-least :b :c :a)))) + +(deftest test-all-least-by + (is (nil? (all-least-by identity)) "all-least-by with no arguments is nil") + (is (= (list "a")) (all-least-by count "a")) + (is (= (list "a" "a")) (all-least-by count "a" "a")) + (is (= (list "b")) (all-least-by count "aa" "b")) + (is (= (list "c" "b")) (all-least-by count "b" "aa" "c")) + (is (= (list "b")) (all-least-by count "aa" "b" "cc"))) |