Browse files

Added some predicates

  • Loading branch information...
1 parent 44d575d commit 9ea4ad14fcc0ac53d21292385e1135f368c9f9b3 @dnaumov committed Apr 8, 2012
Showing with 40 additions and 0 deletions.
  1. +40 −0 test/contracts/test/preds.clj
View
40 test/contracts/test/preds.clj
@@ -0,0 +1,40 @@
+(ns contracts.preds
+ (:refer-clojure :exclude [= == not= < > <= >=
+ identical? instance? extends? satisfies? isa?
+ contains? every? not-every? some not-any?
+ and or not])
+ (:require [clojure.core :as c]))
+
+;; TODO: tests!
+
+(defn boolean? [x] (c/instance? Boolean x))
+
+(def and c/every-pred)
+(def or c/some-fn)
+(def not c/complement)
+
+(defn ^:private make-collection-pred [type-pred]
+ (fn [pred]
+ (fn [x] (c/and (type-pred x) (c/every? pred x)))))
+
+(def coll-of (make-collection-pred coll?))
+(def map-of (make-collection-pred map?))
+(def vector-of (make-collection-pred vector?))
+(def seq-of (make-collection-pred seq?))
+(def sequential-of (make-collection-pred sequential?))
+
+(defmacro ^:private defcurried
+ ([name]
+ (let [clj-var (ns-resolve 'clojure.core name)]
+ `(-> (defn ~name
+ ([a#] (partial ~clj-var a#))
+ ([a# b#] (~clj-var a# b#)))
+ (alter-meta! merge (meta ~clj-var)))))
+ ([name & names]
+ `(do (defcurried ~name)
+ (defcurried ~@names))))
+
+(defcurried
+ = == not= < > <= >=
+ identical? instance? extends? satisfies? isa?
+ contains? every? not-every? some not-any?)

0 comments on commit 9ea4ad1

Please sign in to comment.