Skip to content
Browse files

Moved helper functions to their own namespace

  • Loading branch information...
1 parent 7a9ee07 commit 81c769866cfab5c869a13adedb5495e89cbc2513 @dnaumov committed Apr 21, 2012
Showing with 43 additions and 37 deletions.
  1. +2 −37 src/contracts/core.clj
  2. +41 −0 src/contracts/utils.clj
View
39 src/contracts/core.clj
@@ -1,26 +1,12 @@
(ns contracts.core
(:refer-clojure :exclude [and or not vector-of])
(:require [clojure.core :as clj])
- (:use [clojure.core.match :only [match]]
+ (:use contracts.utils
+ [clojure.core.match :only [match]]
[clojure.walk :only [postwalk]]))
-(declare =>)
(def current-var (atom nil))
-(defmacro match-s
- "Like match, but also wraps every check in (... :seq)"
- [expr & clauses]
- (letfn [(wrap-in-seq [form]
- (postwalk #(if (clj/and (list? %)
- (clj/not (keyword? (first %))))
- (list % :seq)
- %)
- form))]
- `(match ~expr ~@(->> clauses
- (partition 2)
- (map (fn [[q a]] [(wrap-in-seq q) a]))
- (apply concat)))))
-
(defn humanize-checked-expr [expr]
(let [s (match-s expr
(deref (var x)) (name x)
@@ -59,11 +45,6 @@
expecting
(pr-str value))))
-(defn fn-contract-expr? [expr]
- (clj/and (seq? expr)
- (symbol? (first expr))
- (= (resolve (first expr)) #'=>)))
-
;; TODO: rename
(defn gen-check* [{:keys [type cond return-val pred expr value]}]
`(if ~cond
@@ -88,21 +69,6 @@
:pred pred})])
(into {})))
-(defn wrap-in-list-if [pred x]
- (if (pred x)
- (list x)
- x))
-
-(defn amp? [x]
- (= x '&))
-
-(defn split-at-amp
- "Returns [things-before-& things-after-&]"
- [coll]
- (let [[normal maybe-rest] (split-with (complement amp?) coll)]
- [normal (when (amp? (first maybe-rest))
- (next maybe-rest))]))
-
(defn gen-constrained-body [f post pre args]
(let [[pre-check-results result] (map gensym ["pre-check-results" "result"])
[normal-args rest-args] (split-at-amp args)]
@@ -114,7 +80,6 @@
~result (apply ~f ~@normal-args ~(clj/or (first rest-args) []))]
~(-> (gen-check :post {result post}) first val)))))
-
(defn normalize-pre
"Returns preconditions in the form ([pre ...] ...)"
[expr]
View
41 src/contracts/utils.clj
@@ -0,0 +1,41 @@
+(ns contracts.utils
+ "For internal use only."
+ (:use [clojure.core.match :only [match]]
+ [clojure.walk :only [postwalk]]))
+
+(defmacro match-s
+ "Like match, but there is no need to wrap checks in (... :seq) in
+ order to match list."
+ [expr & clauses]
+ (letfn [(wrap-in-seq [form]
+ (postwalk #(if (and (list? %)
+ (not (keyword? (first %))))
+ (list % :seq)
+ %)
+ form))]
+ `(match ~expr ~@(->> clauses
+ (partition 2)
+ (map (fn [[q a]] [(wrap-in-seq q) a]))
+ (apply concat)))))
+
+(defn fn-contract-expr?
+ "Checks whether expr is (c/=> ...)"
+ [expr]
+ (and (seq? expr)
+ (symbol? (first expr))
+ (= (resolve (first expr)) (find-var 'contracts.core/=>))))
+
+(defn wrap-in-list-if [pred x]
+ (if (pred x)
+ (list x)
+ x))
+
+(defn amp? [x]
+ (= x '&))
+
+(defn split-at-amp
+ "Returns [things-before-& things-after-&]"
+ [coll]
+ (let [[normal maybe-rest] (split-with (complement amp?) coll)]
+ [normal (when (amp? (first maybe-rest))
+ (next maybe-rest))]))

0 comments on commit 81c7698

Please sign in to comment.
Something went wrong with that request. Please try again.