|
22 | 22 | * along with this program; if not, write to the Free Software
|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
24 | 24 |
|
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. |
29 | 29 |
|
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: |
32 | 32 |
|
33 |
| - ```res sig |
34 |
| - type option<'a> = None | Some('a) |
35 |
| - ``` |
| 33 | +```rescript |
| 34 | +type option<'a> = None | Some('a) |
| 35 | +``` |
36 | 36 |
|
37 |
| - ```res example |
38 |
| - let someString: option<string> = Some("hello") |
39 |
| - ``` |
| 37 | +```rescript |
| 38 | +let someString: option<string> = Some("hello") |
| 39 | +``` |
40 | 40 | */
|
| 41 | + |
41 | 42 | /**
|
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 |
43 | 46 |
|
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 | +``` |
49 | 52 | */
|
50 | 53 | let filter: (option<'a>, 'a => bool) => option<'a>
|
51 | 54 |
|
52 | 55 | /**
|
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`. |
54 | 58 |
|
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 | +``` |
59 | 65 | */
|
60 | 66 | let forEach: (option<'a>, 'a => unit) => unit
|
61 | 67 |
|
62 | 68 | /**
|
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 | +``` |
64 | 75 |
|
65 |
| - ```res example |
66 |
| - Option.getExn(Some(3)) /* 3 */ |
| 76 | +## Exceptions |
67 | 77 |
|
68 |
| - Option.getExn(None) /* Raises an Error */ |
69 |
| - ``` |
| 78 | +- Raises an error if `opt` is `None` |
70 | 79 | */
|
71 | 80 | let getExn: option<'a> => 'a
|
72 | 81 |
|
73 | 82 | /**
|
74 |
| - `getUnsafe(x)` returns `x` |
| 83 | +`getUnsafe(value)` returns `value`. |
| 84 | +
|
| 85 | +## Examples |
75 | 86 |
|
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(...)))` |
78 | 95 | */
|
79 | 96 | external getUnsafe: option<'a> => 'a = "%identity"
|
80 | 97 |
|
81 | 98 | /**
|
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`. |
84 | 101 |
|
85 |
| - If `optionValue` is `None`, the default is returned. |
| 102 | +## Examples |
86 | 103 |
|
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 |
90 | 107 |
|
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 | +``` |
94 | 111 | */
|
95 | 112 | let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b
|
96 | 113 |
|
97 | 114 | /**
|
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`. |
99 | 117 |
|
100 |
| - ```res example |
101 |
| - Option.map(Some(3), x => x * x) /* Some(9) */ |
| 118 | +## Examples |
102 | 119 |
|
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 | +``` |
105 | 124 | */
|
106 | 125 | let map: (option<'a>, 'a => 'b) => option<'b>
|
107 | 126 |
|
108 | 127 | /**
|
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 | +``` |
127 | 145 | */
|
128 | 146 | let flatMap: (option<'a>, 'a => option<'b>) => option<'b>
|
129 | 147 |
|
130 | 148 | /**
|
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`. |
135 | 151 |
|
136 |
| - Option.getWithDefault(Some("Apple"), "Banana") /* Apple */ |
137 |
| - ``` |
| 152 | +## Examples |
138 | 153 |
|
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 |
142 | 157 |
|
143 |
| - Some("Jane")->greet /* "Greetings Jane" */ |
| 158 | +let greet = (firstName: option<string>) => |
| 159 | + "Greetings " ++ firstName->Option.getWithDefault("Anonymous") |
144 | 160 |
|
145 |
| - None->greet /* "Greetings Anonymous" */ |
146 |
| - ``` |
| 161 | +Some("Jane")->greet // "Greetings Jane" |
| 162 | +None->greet // "Greetings Anonymous" |
| 163 | +``` |
147 | 164 | */
|
148 | 165 | let getWithDefault: (option<'a>, 'a) => 'a
|
149 | 166 |
|
150 | 167 | /**
|
151 |
| - `orElse optionalValue otherOptional` |
| 168 | +`orElse(opt1, opt2)` returns `opt2` if `opt1` is `None`, otherwise `opt1`. |
152 | 169 |
|
153 |
| - If `optionalValue` is `Some value`, returns `Some value`, otherwise `otherOptional` |
| 170 | +## Examples |
154 | 171 |
|
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 | +``` |
160 | 177 | */
|
161 | 178 | let orElse: (option<'a>, option<'a>) => option<'a>
|
162 | 179 |
|
163 | 180 | /**
|
164 |
| - Returns `true` if the argument is `Some(value)`, `false` otherwise. |
| 181 | +`isSome(opt)` returns `true` if `opt` is `Some(value)`, otherwise returns `false`. |
165 | 182 |
|
166 |
| - ```res example |
167 |
| - Option.isSome(None) /* false */ |
| 183 | +## Examples |
168 | 184 |
|
169 |
| - Option.isSome(Some(1)) /* true */ |
170 |
| - ``` |
| 185 | +```rescript |
| 186 | +Option.isSome(None) // false |
| 187 | +Option.isSome(Some(1)) // true |
| 188 | +``` |
171 | 189 | */
|
172 | 190 | let isSome: option<'a> => bool
|
173 | 191 |
|
174 | 192 | /**
|
175 |
| - Returns `true` if the argument is `None`, `false` otherwise. |
| 193 | +`isNone(opt)` returns `true` if `opt` is `None`, false otherwise. |
176 | 194 |
|
177 |
| - ```res example |
178 |
| - Option.isNone(None) /* true */ |
| 195 | +## Examples |
179 | 196 |
|
180 |
| - Option.isNone(Some(1)) /* false */ |
181 |
| - ``` |
| 197 | +```rescript |
| 198 | +Option.isNone(None) // true |
| 199 | +Option.isNone(Some(1)) // false |
| 200 | +``` |
182 | 201 | */
|
183 | 202 | let isNone: option<'a> => bool
|
184 | 203 |
|
185 | 204 | /**
|
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. |
198 | 210 |
|
199 |
| - eq(Some(3), Some(15), clockEqual) /* true */ |
| 211 | +## Examples |
200 | 212 |
|
201 |
| - eq(Some(3), None, clockEqual) /* false */ |
| 213 | +```rescript |
| 214 | +let clockEqual = (a, b) => mod(a, 12) == mod(b, 12) |
202 | 215 |
|
203 |
| - eq(None, Some(3), clockEqual) /* false */ |
| 216 | +open Option |
204 | 217 |
|
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 | +``` |
207 | 223 | */
|
208 | 224 | let eq: (option<'a>, option<'b>, ('a, 'b) => bool) => bool
|
209 | 225 |
|
210 | 226 | /**
|
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`. |
229 | 228 |
|
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). |
231 | 230 |
|
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). |
233 | 233 |
|
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. |
235 | 238 |
|
236 |
| - cmp(Some(2), Some(15), clockCompare) /* (-1) */ |
| 239 | +## Examples |
237 | 240 |
|
238 |
| - cmp(None, Some(15), clockCompare) /* (-1) */ |
| 241 | +```rescript |
| 242 | +let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12)) |
239 | 243 |
|
240 |
| - cmp(Some(14), None, clockCompare) /* 1 */ |
| 244 | +open Option |
241 | 245 |
|
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 | +``` |
244 | 253 | */
|
245 | 254 | let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int
|
0 commit comments