mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-11 13:06:42 +00:00
Added Contracts (#127)
* Added description of contracts * Added ES6 example * linted and toc
This commit is contained in:
parent
eceef7553d
commit
b77effbd02
1 changed files with 16 additions and 1 deletions
17
readme.md
17
readme.md
|
|
@ -31,6 +31,8 @@ __Table of Contents__
|
||||||
* [Value](#value)
|
* [Value](#value)
|
||||||
* [Constant](#constant)
|
* [Constant](#constant)
|
||||||
* [Functor](#functor)
|
* [Functor](#functor)
|
||||||
|
* [Preserves identity](#preserves-identity)
|
||||||
|
* [Composable](#composable)
|
||||||
* [Pointed Functor](#pointed-functor)
|
* [Pointed Functor](#pointed-functor)
|
||||||
* [Lift](#lift)
|
* [Lift](#lift)
|
||||||
* [Referential Transparency](#referential-transparency)
|
* [Referential Transparency](#referential-transparency)
|
||||||
|
|
@ -270,7 +272,20 @@ const predicate = (a) => a > 2
|
||||||
|
|
||||||
## Contracts
|
## Contracts
|
||||||
|
|
||||||
TODO
|
A contract specifies the obligations and guarentees of the behavior from a function or expression at runtime. This acts as a set of rules that are expected from the input and output of a function or expression, and errors are generally reported whenever a contract is violated.
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Define our contract : int -> int
|
||||||
|
const contract = (input) => {
|
||||||
|
if (typeof input === 'number') return true
|
||||||
|
throw new Error('Contract violated: expected int -> int')
|
||||||
|
}
|
||||||
|
|
||||||
|
const addOne = (num) => contract(num) && num + 1
|
||||||
|
|
||||||
|
addOne(2) // 3
|
||||||
|
addOne('some string') // Contract violated: expected int -> int
|
||||||
|
```
|
||||||
|
|
||||||
## Guarded Functions
|
## Guarded Functions
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue