-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.affine
More file actions
350 lines (304 loc) · 8.11 KB
/
Copy pathoption.affine
File metadata and controls
350 lines (304 loc) · 8.11 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2025 hyperpolymath
//
// Option - Utilities for Option<T> type
module option;
// `Option`/`Result` types + constructors are owned by `prelude` (ADR-011).
// `map` is defined locally below as the Option map (f, Option<T>);
// it is NOT imported from prelude (prelude's `map` is the list map
// with a different signature — importing both would conflict).
use prelude::{Option, Some, None, Result, Ok, Err};
// This module is the single canonical home for the Option *operations*
// (is_some/is_none/unwrap/unwrap_or/map/filter/contains/…). #133 removed
// the duplicate copies that previously also lived in prelude.affine.
// ============================================================================
// Combinators
// ============================================================================
/// Map over Some value
fn map<T, U>(f: T -> U, opt: Option<T>) -> Option<U> {
match opt {
Some(value) => Some(f(value)),
None => None
}
}
/// Apply function in Option to value in Option
fn apply<T, U>(f_opt: Option<T -> U>, value_opt: Option<T>) -> Option<U> {
match (f_opt, value_opt) {
(Some(f), Some(value)) => Some(f(value)),
_ => None
}
}
/// Flat map (bind) over Option
fn flat_map<T, U>(f: T -> Option<U>, opt: Option<T>) -> Option<U> {
match opt {
Some(value) => f(value),
None => None
}
}
/// Also known as 'and_then'
fn and_then<T, U>(opt: Option<T>, f: T -> Option<U>) -> Option<U> {
flat_map(f, opt)
}
/// Or else - use alternative Option if None
fn or_else<T>(opt: Option<T>, alternative: Option<T>) -> Option<T> {
match opt {
Some(_) => opt,
None => alternative
}
}
/// Or else computed - compute alternative only if None
fn or_else_with<T>(opt: Option<T>, f: () -> Option<T>) -> Option<T> {
match opt {
Some(_) => opt,
None => f()
}
}
// ============================================================================
// Queries
// ============================================================================
/// Check if Option is Some
fn is_some<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => true,
None => false
}
}
/// Check if Option is None
fn is_none<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => false,
None => true
}
}
/// Check if Option contains specific value
fn contains<T>(opt: Option<T>, value: T) -> Bool {
match opt {
Some(x) => x == value,
None => false
}
}
// ============================================================================
// Extractors
// ============================================================================
/// Unwrap Some value or panic
fn unwrap<T>(opt: Option<T>) -> T {
match opt {
Some(value) => value,
None => panic("Called unwrap on None")
}
}
/// Unwrap with custom error message
fn expect<T>(opt: Option<T>, msg: String) -> T {
match opt {
Some(value) => value,
None => panic(msg)
}
}
/// Unwrap or provide default value
pub fn unwrap_or<T>(opt: Option<T>, default: T) -> T {
match opt {
Some(value) => value,
None => default
}
}
/// Unwrap or compute default value
fn unwrap_or_else<T>(opt: Option<T>, f: () -> T) -> T {
match opt {
Some(value) => value,
None => f()
}
}
/// Get value or return from function with default
fn ok_or<T, E>(opt: Option<T>, err: E) -> Result<T, E> {
match opt {
Some(value) => Ok(value),
None => Err(err)
}
}
/// Get value or compute error
fn ok_or_else<T, E>(opt: Option<T>, f: () -> E) -> Result<T, E> {
match opt {
Some(value) => Ok(value),
None => Err(f())
}
}
// ============================================================================
// Filtering and Zipping
// ============================================================================
/// Filter Option based on predicate
fn filter<T>(pred: T -> Bool, opt: Option<T>) -> Option<T> {
match opt {
Some(value) => if pred(value) { Some(value) } else { None },
None => None
}
}
/// Zip two Options together
fn zip<A, B>(a: Option<A>, b: Option<B>) -> Option<(A, B)> {
match (a, b) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
}
}
/// Zip with function
fn zip_with<A, B, C>(f: (A, B) -> C, a: Option<A>, b: Option<B>) -> Option<C> {
match (a, b) {
(Some(x), Some(y)) => Some(f(x, y)),
_ => None
}
}
/// Unzip Option of pair
fn unzip<A, B>(opt: Option<(A, B)>) -> (Option<A>, Option<B>) {
match opt {
Some((a, b)) => (Some(a), Some(b)),
None => (None, None)
}
}
// ============================================================================
// Collections
// ============================================================================
/// Transpose Option of list to list of Option
fn transpose<T>(opt: Option<[T]>) -> [Option<T>] {
match opt {
Some(list) => {
let mut result = [];
for x in list {
result = result ++ [Some(x)];
}
result
},
None => []
}
}
/// Collect list of Options into Option of list (None on any None)
fn collect<T>(opts: [Option<T>]) -> Option<[T]> {
let mut values = [];
for opt in opts {
match opt {
Some(value) => values = values ++ [value],
None => return None
}
}
Some(values)
}
/// Filter out Nones from list
fn cat_options<T>(opts: [Option<T>]) -> [T] {
let mut values = [];
for opt in opts {
match opt {
// Statement-block arm so the arm is Unit-typed and matches the
// `None => {}` arm (a bare assignment expression is typed as its
// RHS, which would clash Array vs Unit across the arms).
Some(value) => { values = values ++ [value]; },
None => {}
}
}
values
}
/// Map list with function returning Option, filtering Nones
fn map_filter<T, U>(f: T -> Option<U>, list: [T]) -> [U] {
let mut results = [];
for x in list {
results = results ++ [f(x)];
}
cat_options(results)
}
/// Find first Some in list of Options
fn first_some<T>(opts: [Option<T>]) -> Option<T> {
for opt in opts {
match opt {
Some(value) => return Some(value),
None => {}
}
}
None
}
/// Get head of list as Option
fn head<T>(list: [T]) -> Option<T> {
if len(list) > 0 {
Some(list[0])
} else {
None
}
}
/// Get tail of list as Option
fn tail<T>(list: [T]) -> Option<[T]> {
if len(list) > 0 {
Some(list[1:])
} else {
None
}
}
/// Get last element of list as Option
fn last<T>(list: [T]) -> Option<T> {
let n = len(list);
if n > 0 {
Some(list[n - 1])
} else {
None
}
}
/// Get element at index as Option
fn get<T>(list: [T], index: Int) -> Option<T> {
if index >= 0 && index < len(list) {
Some(list[index])
} else {
None
}
}
// ============================================================================
// Boolean Options
// ============================================================================
/// Convert bool to Option
fn bool_to_option(b: Bool) -> Option<()> {
if b {
Some(())
} else {
None
}
}
/// AND operation on Options (both must be Some)
fn option_and<T, U>(a: Option<T>, b: Option<U>) -> Option<(T, U)> {
zip(a, b)
}
/// OR operation on Options (first Some wins)
fn option_or<T>(a: Option<T>, b: Option<T>) -> Option<T> {
or_else(a, b)
}
/// XOR operation on Options (exactly one must be Some)
fn option_xor<T>(a: Option<T>, b: Option<T>) -> Option<T> {
match (a, b) {
(Some(x), None) => Some(x),
(None, Some(y)) => Some(y),
_ => None
}
}
// ============================================================================
// Utility Functions
// ============================================================================
/// Flatten nested Option
fn flatten<T>(opt: Option<Option<T>>) -> Option<T> {
match opt {
Some(inner) => inner,
None => None
}
}
/// Replace None with provided Option
fn replace_none<T>(opt: Option<T>, replacement: Option<T>) -> Option<T> {
or_else(opt, replacement)
}
/// Take value from Option, leaving None
fn take<T>(mut opt: Option<T>) -> Option<T> {
let result = opt;
opt = None;
result
}
/// Insert value into Option if None
fn get_or_insert<T>(mut opt: Option<T>, value: T) -> T {
match opt {
Some(x) => x,
None => {
opt = Some(value);
value
}
}
}