Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding manipm and keysapp to util ns

  • Loading branch information...
commit fd4ab54da4eb74e4f7b8ffd1b88fd9c8be3660fc 1 parent 351be85
Fogus authored
Showing with 45 additions and 37 deletions.
  1. +2 −37 src/trammel/core.clj
  2. +43 −0 src/trammel/utils.clj
39 src/trammel/core.clj
View
@@ -14,7 +14,8 @@
(ns trammel.core
"The core contracts programming functions and macros for Trammel."
(:use [trammel.funcify :only (funcify)])
- (:use trammel.factors))
+ (:use trammel.factors)
+ (:use trammel.utils))
;; HOF support
@@ -29,32 +30,6 @@
;; # base functions and macros
-(defn- keys-apply
- "Takes a function, a set of keys, and a map and applies the function to the map on the given keys.
- A new map of the results of the function applied to the keyed entries is returned.
- "
- [f ks m]
- {:pre [(or (fn? f) (keyword? f) (symbol? f))
- (except (coll? ks) (map? ks))
- (map? m)]
- :post [(map? %)]}
- (let [only (select-keys m ks)]
- (zipmap (keys only)
- (map f (vals only)))))
-
-(defn- manip-map
- "Takes a function, a set of keys, and a map and applies the function to the map on the given keys.
- A modified version of the original map is returned with the results of the function applied to each
- keyed entry.
- "
- [f ks m]
- {:pre [(or (fn? f) (keyword? f) (symbol? f))
- (except (coll? ks) (map? ks))
- (map? m)]
- :post [(map? %)
- (= (keys %) (keys m))]}
- (conj m (keys-apply f ks m)))
-
(defn- build-pre-post-map
"Takes a vector of the form `[pre ... => post ...]` and infers the expectations described
therein. The map that comes out will look like Clojure's default pre- anlein d post-conditions
@@ -248,16 +223,6 @@
~(str (:doc mdata))
~@body)))
-(defn ^:private check-args!
- [name slots inv-description invariants]
- (assert (and inv-description (string? inv-description))
- (str "Expecting an invariant description for " name))
- (assert (and invariants (or (map? invariants) (vector? invariants)))
- (str "Expecting invariants of the form "
- "[pre-conditions => post-conditions] or "
- "{:pre [pre-conditions]}"
- "for record type " name)))
-
(defmacro defconstrainedrecord
[name slots inv-description invariants & etc]
(check-args! name slots inv-description invariants)
43 src/trammel/utils.clj
View
@@ -0,0 +1,43 @@
+(ns trammel.utils
+ (:use trammel.factors))
+
+(defmacro ^:private assert-w-message
+ [check message]
+ `(when-not ~check
+ (throw (new AssertionError (str "Trammel asserttion failed: " ~message "\n" (pr-str '~check))))))
+
+(defn check-args!
+ [name slots inv-description invariants]
+ (assert-w-message (and inv-description (string? inv-description))
+ (str "Expecting an invariant description for " name))
+ (assert-w-message (and invariants (or (map? invariants) (vector? invariants)))
+ (str "Expecting invariants of the form "
+ "[pre-conditions => post-conditions] or "
+ "{:pre [pre-conditions]}"
+ "for record type " name)))
+
+(defn ^:private keys-apply
+ "Takes a function, a set of keys, and a map and applies the function to the map on the given keys.
+ A new map of the results of the function applied to the keyed entries is returned.
+ "
+ [f ks m]
+ {:pre [(or (fn? f) (keyword? f) (symbol? f))
+ (except (coll? ks) (map? ks))
+ (map? m)]
+ :post [(map? %)]}
+ (let [only (select-keys m ks)]
+ (zipmap (keys only)
+ (map f (vals only)))))
+
+(defn manip-map
+ "Takes a function, a set of keys, and a map and applies the function to the map on the given keys.
+ A modified version of the original map is returned with the results of the function applied to each
+ keyed entry.
+ "
+ [f ks m]
+ {:pre [(or (fn? f) (keyword? f) (symbol? f))
+ (except (coll? ks) (map? ks))
+ (map? m)]
+ :post [(map? %)
+ (= (keys %) (keys m))]}
+ (conj m (keys-apply f ks m)))
Please sign in to comment.
Something went wrong with that request. Please try again.