Permalink
Browse files

generic.comparison: fixed typo in <=, added pos? neg? min max

  • Loading branch information...
1 parent 8e3fca7 commit f7a454fba2e2f8fea30dedd9f58a13d73b54f2ec @khinsen khinsen committed Mar 4, 2010
Showing with 42 additions and 5 deletions.
  1. +42 −5 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)))

0 comments on commit f7a454f

Please sign in to comment.