/
index.html
208 lines (159 loc) · 5.95 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>core.contracts</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="core.contracts"/>
<meta name="generator" content="Org-mode"/>
<meta name="generated" content="2013-01-22 11:03:47 EST"/>
<meta name="author" content="Fogus"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
html { font-family: Times, serif; font-size: 12pt; }
.title { text-align: center; }
.todo { color: red; }
.done { color: green; }
.tag { background-color: #add8e6; font-weight:normal }
.target { }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right {margin-left:auto; margin-right:0px; text-align:right;}
.left {margin-left:0px; margin-right:auto; text-align:left;}
.center {margin-left:auto; margin-right:auto; text-align:center;}
p.verse { margin-left: 3% }
pre {
border: 1pt solid #AEBDCC;
background-color: #F3F5F7;
padding: 5pt;
font-family: courier, monospace;
font-size: 90%;
overflow:auto;
}
table { border-collapse: collapse; }
td, th { vertical-align: top; }
th.right { text-align:center; }
th.left { text-align:center; }
th.center { text-align:center; }
td.right { text-align:right; }
td.left { text-align:left; }
td.center { text-align:center; }
dt { font-weight: bold; }
div.figure { padding: 0.5em; }
div.figure p { text-align: center; }
div.inlinetask {
padding:10px;
border:2px solid gray;
margin:10px;
background: #ffffcc;
}
textarea { overflow-x: auto; }
.linenr { font-size:smaller }
.code-highlighted {background-color:#ffff00;}
.org-info-js_info-navigation { border-style:none; }
#org-info-js_console-label { font-size:10px; font-weight:bold;
white-space:nowrap; }
.org-info-js_search-highlight {background-color:#ffff00; color:#000000;
font-weight:bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="styles/default.css" />
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble">
</div>
<div id="content">
<h1 class="title">core.contracts</h1>
<div id="rinich">
<div class="log">
<div class="description">a contracts-programming library for <a href="http://clojure.org">Clojure</a></div>
<div class="item">
<h4>Contracts Programming</h4>
<div class="citation"><p>the relationship between a class and
its clients as a formal agreement, expressing each party's
rights and obligations</p></div>
<div class="citation"><a href="http://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a></div>
<!-- TODO TODO TODO
<a class="more" href="http://fogus.me/fun/trammel/cp.html">read more</a>
-->
</div>
<div class="item">
<p>
First add the following to your <a href="https://github.com/technomancy/leiningen">Leiningen</a> <i>project.clj</i> file in the <code>:dependencies</code> section:
</p>
<pre class="src src-clojure">[org.clojure/core.contracts <span style="color: #00cd00;">"0.0.1"</span>]
</pre>
<p>
To include core.contracts, add it to your namespace declaration:
</p>
<pre class="src src-clojure">(<span style="color: #0000ee; font-weight: bold;">ns</span> my.awesome.lib
(<span style="color: #cdcd00;">require</span>
[clojure.core.contracts <span style="color: #cd00cd;">:as</span> contracts]))
</pre>
<p>
Write your functions as you normally would:
</p>
<pre class="src src-clojure">(<span style="color: #00cdcd; font-weight: bold;">defn</span> <span style="color: #0000ee; font-weight: bold;">sqr</span> [n] (<span style="color: #cdcd00;">*</span> n n))
(sqr 10)
<span style="color: #cd0000;">;</span><span style="color: #cd0000;">=> 100</span>
(sqr 0)
<span style="color: #cd0000;">;</span><span style="color: #cd0000;">=> 0</span>
</pre>
<p>
Later, annotate your functions separately with the constraints:
</p>
<pre class="src src-clojure">(contracts/provide
(sqr <span style="color: #00cd00;">"Constraints for squaring"</span>
[x] [number? (<span style="color: #cdcd00;">not=</span> 0 x) => number? pos?]))
</pre>
<p>
The core.contracts validation will occur at run-time:
</p>
<pre class="src src-clojure">(sqr 10)
<span style="color: #cd0000;">;</span><span style="color: #cd0000;">=> 100</span>
(sqr 0)
<span style="color: #cd0000;">; </span><span style="color: #cd0000;">java.lang.AssertionError: </span>
<span style="color: #cd0000;">; </span><span style="color: #cd0000;">Pre-condition failed: (not= 0 x)</span>
</pre>
<p>
To turn-off contracts for any given file, add the following to the top of the file in question before compiling:
</p>
<pre class="src src-clojure">(set! *assert* false)
</pre>
</div>
<a class="button" href="http://github.com/clojure/core.contracts">source code →</a>
<a class="button" href="http://github.com/clojure/core.contracts/issues">tickets →</a>
<div class="footer">
© 2010-2013 Michael Fogus <br/>
theme based on a design by <a href="http://rinich.tumblr.com">Rory Marinich</a>
</div>
</div>
</div>
</div>
</body>
</html>