aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@fastmail.net>2010-03-04 17:50:45 +0100
committerKonrad Hinsen <konrad.hinsen@fastmail.net>2010-03-04 17:50:45 +0100
commitf7a454fba2e2f8fea30dedd9f58a13d73b54f2ec (patch)
tree9bcfaff2b16c23d73d54f2e5bbf6ebb7941b6cb7 /src
parent8e3fca7b112209c36cef281f54803ea679b7c97e (diff)
generic.comparison: fixed typo in <=, added pos? neg? min max
Diffstat (limited to 'src')
-rw-r--r--src/main/clojure/clojure/contrib/generic/comparison.clj47
1 files changed, 42 insertions, 5 deletions
diff --git a/src/main/clojure/clojure/contrib/generic/comparison.clj b/src/main/clojure/clojure/contrib/generic/comparison.clj
index 7e2b81fd..625471e3 100644
--- a/src/main/clojure/clojure/contrib/generic/comparison.clj
+++ b/src/main/clojure/clojure/contrib/generic/comparison.clj
@@ -1,9 +1,9 @@
;; Generic interfaces for comparison operations
;; by Konrad Hinsen
-;; last updated May 5, 2009
+;; last updated March 4, 2010
-;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
+;; Copyright (c) Konrad Hinsen, 2009-2010. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
@@ -18,18 +18,28 @@
as multimethods that can be defined for any type. Of the
greater/less-than relations, types must minimally implement >."}
clojure.contrib.generic.comparison
- (:refer-clojure :exclude [= < > <= >= zero?])
+ (:refer-clojure :exclude [= < > <= >= zero? pos? neg? min max])
(:use [clojure.contrib.generic
:only (root-type nulary-type nary-type nary-dispatch)]))
;
-; zero?
+; zero? pos? neg?
;
(defmulti zero?
"Return true of x is zero."
{:arglists '([x])}
type)
+(defmulti pos?
+ "Return true of x is positive."
+ {:arglists '([x])}
+ type)
+
+(defmulti neg?
+ "Return true of x is negative."
+ {:arglists '([x])}
+ type)
+
;
; Equality
;
@@ -137,7 +147,7 @@
(defmethod <= root-type
[x] true)
-(defmethod >= [root-type root-type]
+(defmethod <= [root-type root-type]
[x y]
(not (> x y)))
@@ -156,6 +166,14 @@
[x]
(clojure.core/zero? x))
+(defmethod pos? java.lang.Number
+ [x]
+ (clojure.core/pos? x))
+
+(defmethod neg? java.lang.Number
+ [x]
+ (clojure.core/neg? x))
+
(defmethod = [Object Object]
[x y]
(clojure.core/= x y))
@@ -175,3 +193,22 @@
(defmethod <= [Object Object]
[x y]
(clojure.core/<= x y))
+
+;
+; Functions defined in terms of the comparison operators
+;
+(defn max
+ "Returns the greatest of its arguments. Like clojure.core/max except that
+ is uses generic comparison functions implementable for any data type."
+ ([x] x)
+ ([x y] (if (> x y) x y))
+ ([x y & more]
+ (reduce max (max x y) more)))
+
+(defn min
+ "Returns the least of its arguments. Like clojure.core/min except that
+ is uses generic comparison functions implementable for any data type."
+ ([x] x)
+ ([x y] (if (< x y) x y))
+ ([x y & more]
+ (reduce min (min x y) more)))