From ea915c6d23869d1025eb6ddde309e73ecc16d2f8 Mon Sep 17 00:00:00 2001 From: fogus Date: Tue, 22 Jan 2013 11:05:19 -0500 Subject: [PATCH] Annotated example --- index.html | 81 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index e61c2d9..5c991b2 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@ - + @@ -95,26 +95,82 @@

core.contracts

-
-
-
+
+
+
a contracts-programming library for Clojure
+
+

Contracts Programming

+

the relationship between a class and + its clients as a formal agreement, expressing each party's + rights and obligations

+ + + +
-
(require '[clojure.core.contracts :as contracts])
 
-(defn sqr [n] (* n n))
+    
+ +

+First add the following to your Leiningen project.clj file in the :dependencies section: +

+ + + +
[org.clojure/core.contracts "0.0.1"]
+
+ + +

+To include core.contracts, add it to your namespace declaration: +

+ + + +
(ns my.awesome.lib
+  (require 
+    [clojure.core.contracts :as contracts]))
+
+ + +

+Write your functions as you normally would: +

+ + + +
(defn sqr [n] (* n n))
 
 (sqr 10)
 ;=> 100
 
 (sqr 0)
 ;=> 0
+
+ + +

+Later, annotate your functions separately with the constraints: +

+ -(contracts/provide + +
(contracts/provide 
   (sqr "Constraints for squaring" 
     [x] [number? (not= 0 x) => number? pos?]))
+
-(sqr 10) + +

+The core.contracts validation will occur at run-time: +

+ + + +
(sqr 10)
 ;=> 100
 
 (sqr 0)
@@ -123,6 +179,14 @@ 

core.contracts

+

+To turn-off contracts for any given file, add the following to the top of the file in question before compiling: +

+ + + +
(set! *assert* false)
+
@@ -138,7 +202,6 @@

core.contracts

-