Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving over contract building functions and utils.
- Loading branch information
Showing
2 changed files
with
44 additions
and
25 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/clojure/clojure/core/contracts/impl/transformers.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(ns clojure.core.contracts.impl.transformers | ||
(:require [clojure.core.unify :as unify] | ||
[clojure.core.contracts.impl.utils :as utils])) | ||
|
||
|
||
(defn build-condition-body | ||
[p body prefix-msg] | ||
(unify/subst | ||
{'?P p | ||
'?PREFIX prefix-msg | ||
'?BODY body} | ||
|
||
'(try | ||
((fn [] {?P ?PRE} | ||
?BODY)) | ||
(catch AssertionError ae | ||
(throw (AssertionError. (str ?PREFIX ?MSG \newline (.getMessage ae)))))))) | ||
|
||
|
||
(defn- build-pre-post-map | ||
"(build-pre-post-map '[(odd? n) (pos? n) => (int? %)]) | ||
;=> {:pre [...] :post [...]} | ||
" | ||
[cnstr] | ||
(if (vector? cnstr) | ||
(let [[L M R] (partition-by #{'=>} cnstr)] | ||
{:pre (vec (when (not= L '(=>)) L)) | ||
:post (vec (if (= L '(=>)) M R))}) | ||
cnstr)) | ||
|
||
|
||
(defn- tag-hocs | ||
[cnstr] | ||
(map (fn [form] | ||
(if (and (seq? form) (= '_ (first form))) | ||
(list 'fn? (second form)) | ||
form)) | ||
cnstr)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters