Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added base c.c.contracts functionality. contracts, with-constraints, …

…and provide
  • Loading branch information...
commit 64dfa8b854e0d88a88155fd7bcf4f0d318c30666 1 parent 01d4ae2
@fogus fogus authored
Showing with 26 additions and 1 deletion.
  1. +1 −1  project.clj
  2. +25 −0 src/main/clojure/clojure/core/contracts.clj
View
2  project.clj
@@ -1,6 +1,6 @@
(defproject core.contracts "0.0.1"
:description "Contracts programming for Clojure."
- :dependencies [[org.clojure/clojure "1.5.0-alpha1"]
+ :dependencies [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]
[org.clojure/core.unify "0.5.3"]]
:dev-dependencies [[lein-clojars "0.5.0-SNAPSHOT"]
[jline "0.9.94"]
View
25 src/main/clojure/clojure/core/contracts.clj
@@ -12,7 +12,32 @@
{:docstring ~docstring
::constraints :TBD}))
+(defn with-constraints
+ "A contract combinator.
+ Takes a target function and a number of contracts and returns a function with the contracts
+ applied to the original. This is the preferred way to apply a contract previously created
+ using `contract` as the use of `partial` may not work as implementation details change.
+ "
+ ([f] f)
+ ([f c] (partial c f))
+ ([f c & more]
+ (apply with-constraints (with-constraints f c) more)))
+
+(defmacro provide
+ "Provides the Var manipulation macro offering ex post facto application of contracts
+ to existing functions."
+ [& kontracts]
+ (let [fn-names (map first kontracts)
+ kontracts (for [[n ds & more] kontracts]
+ (if (list? (first more))
+ (list* `contract n ds more)
+ (first more)))]
+ `(do
+ ~@(for [[n# c#] (zipmap fn-names kontracts)]
+ (list `alter-var-root (list `var n#)
+ (list `fn '[f c] (list `with-constraints 'f 'c)) c#))
+ nil)))
(comment
(def doubler-contract
Please sign in to comment.
Something went wrong with that request. Please try again.