Skip to content

Commit efc446e

Browse files
authored
format Option docstrings (#42)
1 parent 508b829 commit efc446e

File tree

1 file changed

+144
-135
lines changed

1 file changed

+144
-135
lines changed

src/Core__Option.resi

Lines changed: 144 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -22,224 +22,233 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

25-
/**
26-
We represent the existence and nonexistence of a value by wrapping it with
27-
the `option` type. In order to make it a bit more convenient to work with
28-
option-types, we provide utility-functions for it.
25+
/***
26+
We represent the existence and nonexistence of a value by wrapping it with
27+
the `option` type. In order to make it a bit more convenient to work with
28+
option-types, we provide utility-functions for it.
2929
30-
The `option` type is a part of the ReScript standard library which is defined
31-
like this:
30+
The `option` type is a part of the ReScript standard library which is defined
31+
like this:
3232
33-
```res sig
34-
type option<'a> = None | Some('a)
35-
```
33+
```rescript
34+
type option<'a> = None | Some('a)
35+
```
3636
37-
```res example
38-
let someString: option<string> = Some("hello")
39-
```
37+
```rescript
38+
let someString: option<string> = Some("hello")
39+
```
4040
*/
41+
4142
/**
42-
If `optionValue` is `Some(value)` and `p(value) = true`, it returns `Some(value)`; otherwise returns `None`
43+
`filter(opt, f)` applies `f` to `opt`, if `f` returns `true`, then it returns `Some(value)`, otherwise returns `None`.
44+
45+
## Examples
4346
44-
```res example
45-
Option.filter(Some(10), x => x > 5) /* returns `Some(10)` */
46-
Option.filter(Some(4), x => x > 5) /* returns `None` */
47-
Option.filter(None, x => x > 5) /* returns `None` */
48-
```
47+
```rescript
48+
Option.filter(Some(10), x => x > 5) // Some(10)
49+
Option.filter(Some(4), x => x > 5) // None
50+
Option.filter(None, x => x > 5) // None
51+
```
4952
*/
5053
let filter: (option<'a>, 'a => bool) => option<'a>
5154

5255
/**
53-
If `optionValue` is `Some(value`), it calls `f(value)`; otherwise returns `()`
56+
`forEach(opt, f)` call `f` on `opt`. if `opt` is `Some(value)`, then if calls
57+
`f`, otherwise returns `unit`.
5458
55-
```res example
56-
Option.forEach(Some("thing"), x => Console.log(x)) /* logs "thing" */
57-
Option.forEach(None, x => Console.log(x)) /* returns () */
58-
```
59+
## Examples
60+
61+
```rescript
62+
Option.forEach(Some("thing"), x => Console.log(x)) // logs "thing"
63+
Option.forEach(None, x => Console.log(x)) // returns ()
64+
```
5965
*/
6066
let forEach: (option<'a>, 'a => unit) => unit
6167

6268
/**
63-
Raises an Error in case `None` is provided. Use with care.
69+
`getExn(opt)` raises an Error in case `None` is provided.
70+
71+
```rescript
72+
Option.getExn(Some(3)) // 3
73+
Option.getExn(None) /* Raises an Error */
74+
```
6475
65-
```res example
66-
Option.getExn(Some(3)) /* 3 */
76+
## Exceptions
6777
68-
Option.getExn(None) /* Raises an Error */
69-
```
78+
- Raises an error if `opt` is `None`
7079
*/
7180
let getExn: option<'a> => 'a
7281

7382
/**
74-
`getUnsafe(x)` returns `x`
83+
`getUnsafe(value)` returns `value`.
84+
85+
## Examples
7586
76-
This is an unsafe operation, it assumes `x` is neither `None`
77-
nor `Some(None(...)))`
87+
```rescript
88+
Option.getUnsafe(Some(3)) == 3
89+
Option.getUnsafe(None) // Raises an error
90+
```
91+
92+
## Exceptions
93+
94+
- This is an unsafe operation, it assumes `value` is neither `None` nor `Some(None(...)))`
7895
*/
7996
external getUnsafe: option<'a> => 'a = "%identity"
8097

8198
/**
82-
If `optionValue` is of `Some(value)`,
83-
this function returns that value applied with `f`, in other words `f(value)`.
99+
`mapWithDefault(opt, default, f)` applies `f` to `opt`, if `opt` is `None`, then
100+
`f` return `default`, otherwise return that value applied with `f`.
84101
85-
If `optionValue` is `None`, the default is returned.
102+
## Examples
86103
87-
```res example
88-
let someValue = Some(3)
89-
someValue->Option.mapWithDefault(0, x => x + 5) /* 8 */
104+
```rescript
105+
let someValue = Some(3)
106+
someValue->Option.mapWithDefault(0, x => x + 5) // 8
90107
91-
let noneValue = None
92-
noneValue->Option.mapWithDefault(0, x => x + 5) /* 0 */
93-
```
108+
let noneValue = None
109+
noneValue->Option.mapWithDefault(0, x => x + 5) // 0
110+
```
94111
*/
95112
let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b
96113

97114
/**
98-
If `optionValue` is `Some(value)` this returns `f(value)`, otherwise it returns `None`.
115+
`map(opt f)` applies `f` to `opt`, if `opt` is `Some(value)` this returns
116+
`f(value)`, otherwise it returns `None`.
99117
100-
```res example
101-
Option.map(Some(3), x => x * x) /* Some(9) */
118+
## Examples
102119
103-
Option.map(None, x => x * x) /* None */
104-
```
120+
```rescript
121+
Option.map(Some(3), x => x * x) // Some(9)
122+
Option.map(None, x => x * x) // None
123+
```
105124
*/
106125
let map: (option<'a>, 'a => 'b) => option<'b>
107126

108127
/**
109-
If `optionValue` is `Some(value)`, returns `f(value)`, otherwise returns
110-
`None`.<br/>
111-
The function `f` must have a return type of `option<'b>`.
112-
113-
```res example
114-
let addIfAboveOne = value =>
115-
if (value > 1) {
116-
Some(value + 1)
117-
} else {
118-
None
119-
}
120-
121-
Option.flatMap(Some(2), addIfAboveOne) /* Some(3) */
122-
123-
Option.flatMap(Some(-4), addIfAboveOne) /* None */
124-
125-
Option.flatMap(None, addIfAboveOne) /* None */
126-
```
128+
`flatMap(opt, f)` returns `f` applied to `opt` if `opt` is `Some(value)`,
129+
otherwise `None`. The function `f` must have a return type of `option<'b>`.
130+
131+
## Examples
132+
133+
```rescript
134+
let addIfAboveOne = value =>
135+
if (value > 1) {
136+
Some(value + 1)
137+
} else {
138+
None
139+
}
140+
141+
Option.flatMap(Some(2), addIfAboveOne) // Some(3)
142+
Option.flatMap(Some(-4), addIfAboveOne) // None
143+
Option.flatMap(None, addIfAboveOne) // None
144+
```
127145
*/
128146
let flatMap: (option<'a>, 'a => option<'b>) => option<'b>
129147

130148
/**
131-
If `optionalValue` is `Some(value)`, returns `value`, otherwise default.
132-
133-
```res example
134-
Option.getWithDefault(None, "Banana") /* Banana */
149+
`getWithDefault(opt, default)` returns `value` if `opt` is `Some(value)`,
150+
otherwise return `default` if `opt` is `None`.
135151
136-
Option.getWithDefault(Some("Apple"), "Banana") /* Apple */
137-
```
152+
## Examples
138153
139-
```res example
140-
let greet = (firstName: option<string>) =>
141-
"Greetings " ++ firstName->Option.getWithDefault("Anonymous")
154+
```rescript
155+
Option.getWithDefault(None, "Banana") // Banana
156+
Option.getWithDefault(Some("Apple"), "Banana") // Apple
142157
143-
Some("Jane")->greet /* "Greetings Jane" */
158+
let greet = (firstName: option<string>) =>
159+
"Greetings " ++ firstName->Option.getWithDefault("Anonymous")
144160
145-
None->greet /* "Greetings Anonymous" */
146-
```
161+
Some("Jane")->greet // "Greetings Jane"
162+
None->greet // "Greetings Anonymous"
163+
```
147164
*/
148165
let getWithDefault: (option<'a>, 'a) => 'a
149166

150167
/**
151-
`orElse optionalValue otherOptional`
168+
`orElse(opt1, opt2)` returns `opt2` if `opt1` is `None`, otherwise `opt1`.
152169
153-
If `optionalValue` is `Some value`, returns `Some value`, otherwise `otherOptional`
170+
## Examples
154171
155-
```
156-
orElse (Some 1812) (Some 1066) = Some 1812;;
157-
orElse None (Some 1066) = Some 1066;;
158-
orElse None None = None;;
159-
```
172+
```rescript
173+
Option.orElse(Some(1812), Some(1066)) == Some(1812)
174+
Option.orElse(None, Some(1066) == Some(1066)
175+
Option.orElse(None, None) == None
176+
```
160177
*/
161178
let orElse: (option<'a>, option<'a>) => option<'a>
162179

163180
/**
164-
Returns `true` if the argument is `Some(value)`, `false` otherwise.
181+
`isSome(opt)` returns `true` if `opt` is `Some(value)`, otherwise returns `false`.
165182
166-
```res example
167-
Option.isSome(None) /* false */
183+
## Examples
168184
169-
Option.isSome(Some(1)) /* true */
170-
```
185+
```rescript
186+
Option.isSome(None) // false
187+
Option.isSome(Some(1)) // true
188+
```
171189
*/
172190
let isSome: option<'a> => bool
173191

174192
/**
175-
Returns `true` if the argument is `None`, `false` otherwise.
193+
`isNone(opt)` returns `true` if `opt` is `None`, false otherwise.
176194
177-
```res example
178-
Option.isNone(None) /* true */
195+
## Examples
179196
180-
Option.isNone(Some(1)) /* false */
181-
```
197+
```rescript
198+
Option.isNone(None) // true
199+
Option.isNone(Some(1)) // false
200+
```
182201
*/
183202
let isNone: option<'a> => bool
184203

185204
/**
186-
Evaluates two optional values for equality with respect to a predicate
187-
function. If both `optValue1` and `optValue2` are `None`, returns `true`.
188-
If one of the arguments is `Some(value)` and the other is `None`, returns
189-
`false`.
190-
191-
If arguments are `Some(value1)` and `Some(value2)`, returns the result of
192-
`predicate(value1, value2)`; the predicate function must return a bool.
193-
194-
```res example
195-
let clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
196-
197-
open Option
205+
`eq(opt1, opt2, f)` evaluates two optional values for equality with respect to a predicate function `f`. If both `opt1` and `opt2` are `None`, returns `true`.
206+
If one of the arguments is `Some(value)` and the other is `None`, returns
207+
`false`.
208+
If arguments are `Some(value1)` and `Some(value2)`, returns the result of
209+
`f(value1, value2)`, the predicate function `f` must return a bool.
198210
199-
eq(Some(3), Some(15), clockEqual) /* true */
211+
## Examples
200212
201-
eq(Some(3), None, clockEqual) /* false */
213+
```rescript
214+
let clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
202215
203-
eq(None, Some(3), clockEqual) /* false */
216+
open Option
204217
205-
eq(None, None, clockEqual) /* true */
206-
```
218+
eq(Some(3), Some(15), clockEqual) // true
219+
eq(Some(3), None, clockEqual) // false
220+
eq(None, Some(3), clockEqual) // false
221+
eq(None, None, clockEqual) // true
222+
```
207223
*/
208224
let eq: (option<'a>, option<'b>, ('a, 'b) => bool) => bool
209225

210226
/**
211-
`cmp(optValue1, optValue2, comparisonFunction)` compares two optional values
212-
with respect to given `comparisonFunction`.
213-
214-
If both `optValue1` and `optValue2` are `None`, it returns `0`.
215-
216-
If the first argument is `Some(value1)` and the second is `None`, returns `1`
217-
(something is greater than nothing).
218-
219-
If the first argument is `None` and the second is `Some(value2)`, returns `-1`
220-
(nothing is less than something).
221-
222-
If the arguments are `Some(value1)` and `Some(value2)`, returns the result of
223-
`comparisonFunction(value1, value2)`; comparisonFunction takes two arguments
224-
and returns `-1` if the first argument is less than the second, `0` if the
225-
arguments are equal, and `1` if the first argument is greater than the second.
226-
227-
```res example
228-
let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12))
227+
`cmp(opt1, opt2, f)` compares two optional values with respect to given `f`.
229228
230-
open Option
229+
If both `opt1` and `opt2` are `None`, it returns `0`. If the first argument is `Some(value1)` and the second is `None`, returns `1` (something is greater than nothing).
231230
232-
cmp(Some(3), Some(15), clockCompare) /* 0 */
231+
If the first argument is `None` and the second is `Some(value2)`, returns `-1`
232+
(nothing is less than something).
233233
234-
cmp(Some(3), Some(14), clockCompare) /* 1 */
234+
If the arguments are `Some(value1)` and `Some(value2)`, returns the result of
235+
`f(value1, value2)`, `f` takes two arguments and returns `-1` if the first
236+
argument is less than the second, `0` if the arguments are equal, and `1` if
237+
the first argument is greater than the second.
235238
236-
cmp(Some(2), Some(15), clockCompare) /* (-1) */
239+
## Examples
237240
238-
cmp(None, Some(15), clockCompare) /* (-1) */
241+
```rescript
242+
let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12))
239243
240-
cmp(Some(14), None, clockCompare) /* 1 */
244+
open Option
241245
242-
cmp(None, None, clockCompare) /* 0 */
243-
```
246+
cmp(Some(3), Some(15), clockCompare) // 0
247+
cmp(Some(3), Some(14), clockCompare) // 1
248+
cmp(Some(2), Some(15), clockCompare) // (-1)
249+
cmp(None, Some(15), clockCompare) // (-1)
250+
cmp(Some(14), None, clockCompare) // 1
251+
cmp(None, None, clockCompare) // 0
252+
```
244253
*/
245254
let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int

0 commit comments

Comments
 (0)