Skip to content

Commit f93ae59

Browse files
committed
documenting the and combinator
1 parent 2fb8dc4 commit f93ae59

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

doc/main/contracts.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ <h1 class="title">Contracts.js Documentation</h1>
137137
</ul></li>
138138
<li><a href="#combinators"><span class="toc-section-number">4.5</span> Combinators</a><ul>
139139
<li><a href="#or"><span class="toc-section-number">4.5.1</span> <code>or</code></a></li>
140+
<li><a href="#and"><span class="toc-section-number">4.5.2</span> <code>and</code></a></li>
140141
</ul></li>
141142
<li><a href="#naming-contracts"><span class="toc-section-number">4.6</span> Naming Contracts</a></li>
142143
<li><a href="#recursive-contracts"><span class="toc-section-number">4.7</span> Recursive Contracts</a></li>
@@ -410,14 +411,22 @@ <h3 id="proxied-arrays"><span class="header-section-number">4.4.2</span> Proxied
410411
arr[0] = &quot;string&quot;; // error wrong type</code></pre>
411412
<h2 id="combinators"><span class="header-section-number">4.5</span> Combinators</h2>
412413
<h3 id="or"><span class="header-section-number">4.5.1</span> <code>or</code></h3>
413-
<p>You can combine two contracts with the <code>or</code> combinator. If the first contract fails, the combined contract will succeed if the second passes.</p>
414+
<p>You can combine two or more contracts with the <code>or</code> combinator. If the first contract fails, the combined contract will succeed if the second passes.</p>
414415
<pre class="js"><code>@ (Num or Str) -&gt; Str
415416
function foo(x) { return x.toString(); }
416417

417418
foo(24); // passes
418419
foo(&quot;24&quot;); // passes
419420
foo(false); // error not a Num or Str</code></pre>
420421
<p>Note that <code>or</code> only makes sense for at most one higher-order contract. For example, <code>Num or (Num) -&gt; Num</code> is fine but <code>(Num) -&gt; Num or (Str) -&gt; Str</code> will not work.</p>
422+
<h3 id="and"><span class="header-section-number">4.5.2</span> <code>and</code></h3>
423+
<p>You can combine two or more contracts with the <code>and</code> combinator. Both contracts must pass for the combined contract to succeed.</p>
424+
<pre class="js"><code>@ (Num and (x) =&gt; x &gt; 5) -&gt; Num
425+
function foo(x) { return x; }
426+
427+
foo(10); // passes
428+
foo(&quot;10&quot;); // fails
429+
foo(1); // fails</code></pre>
421430
<h2 id="naming-contracts"><span class="header-section-number">4.6</span> Naming Contracts</h2>
422431
<p>When you have a complicated contract that is repeated in several places it can be convenient to refer to it by a shorter name. For this, you can use <code>let</code> after the <code>@</code> symbol:</p>
423432
<pre class="js"><code>@ let NumId = (Num) -&gt; Num

doc/main/contracts.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ arr[0] = "string"; // error wrong type
384384
385385
### `or`
386386
387-
You can combine two contracts with the `or` combinator. If the first
388-
contract fails, the combined contract will succeed if the second
387+
You can combine two or more contracts with the `or` combinator. If the
388+
first contract fails, the combined contract will succeed if the second
389389
passes.
390390
391391
```js
@@ -401,6 +401,20 @@ Note that `or` only makes sense for at most one higher-order contract.
401401
For example, `Num or (Num) -> Num` is fine but `(Num) -> Num or
402402
(Str) -> Str` will not work.
403403
404+
### `and`
405+
406+
You can combine two or more contracts with the `and` combinator.
407+
Both contracts must pass for the combined contract to succeed.
408+
409+
```js
410+
@ (Num and (x) => x > 5) -> Num
411+
function foo(x) { return x; }
412+
413+
foo(10); // passes
414+
foo("10"); // fails
415+
foo(1); // fails
416+
```
417+
404418
405419
## Naming Contracts
406420

0 commit comments

Comments
 (0)