@carbonteq/fp 0.9.1 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/README.md +326 -97
  2. package/dist/errors.cjs +2 -3
  3. package/dist/errors.cjs.map +1 -1
  4. package/dist/errors.d.cts +2 -5
  5. package/dist/errors.d.cts.map +1 -1
  6. package/dist/errors.d.mts +2 -5
  7. package/dist/errors.d.mts.map +1 -1
  8. package/dist/errors.mjs +1 -2
  9. package/dist/errors.mjs.map +1 -1
  10. package/dist/flow-experimental.cjs +332 -0
  11. package/dist/flow-experimental.cjs.map +1 -0
  12. package/dist/flow-experimental.d.cts +69 -0
  13. package/dist/flow-experimental.d.cts.map +1 -0
  14. package/dist/flow-experimental.d.mts +69 -0
  15. package/dist/flow-experimental.d.mts.map +1 -0
  16. package/dist/flow-experimental.mjs +330 -0
  17. package/dist/flow-experimental.mjs.map +1 -0
  18. package/dist/flow.cjs +340 -0
  19. package/dist/flow.cjs.map +1 -0
  20. package/dist/flow.d.cts +77 -0
  21. package/dist/flow.d.cts.map +1 -0
  22. package/dist/flow.d.mts +77 -0
  23. package/dist/flow.d.mts.map +1 -0
  24. package/dist/flow.mjs +338 -0
  25. package/dist/flow.mjs.map +1 -0
  26. package/dist/index.cjs +29 -14
  27. package/dist/index.d.cts +10 -5
  28. package/dist/index.d.mts +10 -5
  29. package/dist/index.mjs +10 -5
  30. package/dist/match.cjs +376 -3
  31. package/dist/match.cjs.map +1 -1
  32. package/dist/match.d.cts +467 -8
  33. package/dist/match.d.cts.map +1 -1
  34. package/dist/match.d.mts +467 -8
  35. package/dist/match.d.mts.map +1 -1
  36. package/dist/match.mjs +371 -3
  37. package/dist/match.mjs.map +1 -1
  38. package/dist/option-experimental.cjs +1547 -0
  39. package/dist/option-experimental.cjs.map +1 -0
  40. package/dist/option-experimental.d.cts +1380 -0
  41. package/dist/option-experimental.d.cts.map +1 -0
  42. package/dist/option-experimental.d.mts +1380 -0
  43. package/dist/option-experimental.d.mts.map +1 -0
  44. package/dist/option-experimental.mjs +1545 -0
  45. package/dist/option-experimental.mjs.map +1 -0
  46. package/dist/option.cjs +650 -71
  47. package/dist/option.cjs.map +1 -1
  48. package/dist/option.d.cts +528 -39
  49. package/dist/option.d.cts.map +1 -1
  50. package/dist/option.d.mts +528 -39
  51. package/dist/option.d.mts.map +1 -1
  52. package/dist/option.mjs +648 -70
  53. package/dist/option.mjs.map +1 -1
  54. package/dist/result-experimental.cjs +1346 -0
  55. package/dist/result-experimental.cjs.map +1 -0
  56. package/dist/result-experimental.d.cts +1127 -0
  57. package/dist/result-experimental.d.cts.map +1 -0
  58. package/dist/result-experimental.d.mts +1127 -0
  59. package/dist/result-experimental.d.mts.map +1 -0
  60. package/dist/result-experimental.mjs +1345 -0
  61. package/dist/result-experimental.mjs.map +1 -0
  62. package/dist/result.cjs +746 -81
  63. package/dist/result.cjs.map +1 -1
  64. package/dist/result.d.cts +799 -104
  65. package/dist/result.d.cts.map +1 -1
  66. package/dist/result.d.mts +799 -104
  67. package/dist/result.d.mts.map +1 -1
  68. package/dist/result.mjs +742 -73
  69. package/dist/result.mjs.map +1 -1
  70. package/dist/tuple.cjs +5 -4
  71. package/dist/tuple.cjs.map +1 -1
  72. package/dist/tuple.d.cts +9 -7
  73. package/dist/tuple.d.cts.map +1 -1
  74. package/dist/tuple.d.mts +9 -7
  75. package/dist/tuple.d.mts.map +1 -1
  76. package/dist/tuple.mjs +4 -3
  77. package/dist/tuple.mjs.map +1 -1
  78. package/dist/unit.cjs +3 -3
  79. package/dist/unit.cjs.map +1 -1
  80. package/dist/unit.d.cts +4 -5
  81. package/dist/unit.d.cts.map +1 -1
  82. package/dist/unit.d.mts +4 -5
  83. package/dist/unit.d.mts.map +1 -1
  84. package/dist/unit.mjs +2 -2
  85. package/dist/unit.mjs.map +1 -1
  86. package/dist/utils.cjs +50 -3
  87. package/dist/utils.cjs.map +1 -1
  88. package/dist/utils.d.cts +14 -4
  89. package/dist/utils.d.cts.map +1 -1
  90. package/dist/utils.d.mts +14 -4
  91. package/dist/utils.d.mts.map +1 -1
  92. package/dist/utils.mjs +46 -3
  93. package/dist/utils.mjs.map +1 -1
  94. package/examples/README.md +60 -0
  95. package/examples/flow/01-basics.ts +74 -0
  96. package/examples/flow/02-async.ts +73 -0
  97. package/examples/flow/03-recovery.ts +60 -0
  98. package/examples/flow/04-complex-backend.ts +240 -0
  99. package/examples/flow/05-complex-backend-asyncGen.ts +224 -0
  100. package/examples/flow/index.ts +13 -0
  101. package/examples/flow-experimental/01-basics.ts +78 -0
  102. package/examples/flow-experimental/02-async.ts +77 -0
  103. package/examples/flow-experimental/03-recovery.ts +93 -0
  104. package/examples/flow-experimental/04-complex-backend.ts +244 -0
  105. package/examples/flow-experimental/05-complex-backend-asyncGen.ts +229 -0
  106. package/examples/flow-experimental/index.ts +14 -0
  107. package/examples/match/01-stable-match.ts +154 -0
  108. package/examples/match/index.ts +19 -0
  109. package/examples/match-experimental/01-experimental-match.ts +157 -0
  110. package/examples/match-experimental/index.ts +18 -0
  111. package/examples/option/01-basics.ts +16 -0
  112. package/examples/option/02-map-flatMap.ts +36 -0
  113. package/examples/option/03-zip-flatZip.ts +23 -0
  114. package/examples/option/04-constructors.ts +21 -0
  115. package/examples/option/05-transform.ts +15 -0
  116. package/examples/option/06-aggregation.ts +14 -0
  117. package/examples/option/index.ts +49 -0
  118. package/examples/option-experimental/01-map.ts +109 -0
  119. package/examples/option-experimental/02-flatMap.ts +234 -0
  120. package/examples/option-experimental/03-zip.ts +197 -0
  121. package/examples/option-experimental/04-flatZip.ts +251 -0
  122. package/examples/option-experimental/05-gen.ts +360 -0
  123. package/examples/option-experimental/06-genAdapter.ts +463 -0
  124. package/examples/option-experimental/07-asyncGen.ts +353 -0
  125. package/examples/option-experimental/08-asyncGenAdapter.ts +514 -0
  126. package/examples/option-experimental/09-workflow-simple-sync.ts +321 -0
  127. package/examples/option-experimental/10-workflow-async.ts +378 -0
  128. package/examples/option-experimental/index.ts +341 -0
  129. package/examples/option-experimental/shared-domain.ts +325 -0
  130. package/examples/result/01-basics.ts +16 -0
  131. package/examples/result/02-map-flatMap.ts +34 -0
  132. package/examples/result/03-zip-flatZip.ts +23 -0
  133. package/examples/result/04-validation.ts +44 -0
  134. package/examples/result/05-constructors.ts +30 -0
  135. package/examples/result/06-utilities.ts +36 -0
  136. package/examples/result/index.ts +53 -0
  137. package/examples/result-experimental/01-map.ts +60 -0
  138. package/examples/result-experimental/02-flatMap.ts +169 -0
  139. package/examples/result-experimental/03-zip.ts +145 -0
  140. package/examples/result-experimental/04-flatZip.ts +252 -0
  141. package/examples/result-experimental/05-gen.ts +293 -0
  142. package/examples/result-experimental/06-genAdapter.ts +396 -0
  143. package/examples/result-experimental/07-asyncGen.ts +379 -0
  144. package/examples/result-experimental/08-asyncGenAdapter.ts +553 -0
  145. package/examples/result-experimental/09-workflow-simple-sync.ts +304 -0
  146. package/examples/result-experimental/10-workflow-async.ts +356 -0
  147. package/examples/result-experimental/index.ts +312 -0
  148. package/examples/result-experimental/shared-domain.ts +263 -0
  149. package/package.json +54 -31
@@ -0,0 +1,1547 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_result_experimental = require("./result-experimental.cjs");
3
+ const require_unit = require("./unit.cjs");
4
+ const require_utils = require("./utils.cjs");
5
+
6
+ // src/option-experimental.ts
7
+ /**
8
+ * Error thrown when attempting to unwrap a `None` value.
9
+ *
10
+ * This exception is thrown by {@link ExperimentalOption.unwrap} when called on a `None`.
11
+ * It represents a programmer error - you should only call `unwrap()` when
12
+ * you are certain the ExperimentalOption contains a value, or use safe alternatives
13
+ * like {@link ExperimentalOption.unwrapOr} or {@link ExperimentalOption.safeUnwrap}.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * try {
18
+ * ExperimentalOption.None.unwrap();
19
+ * } catch (e) {
20
+ * e instanceof UnwrappedNone // true
21
+ * e.name // "UnwrapError"
22
+ * }
23
+ * ```
24
+ */
25
+ var UnwrappedNone = class extends Error {
26
+ name = "UnwrapError";
27
+ constructor() {
28
+ super("Attempted to unwrap ExperimentalOption::None");
29
+ }
30
+ };
31
+ const UNWRAPPED_NONE_ERR = new UnwrappedNone();
32
+ const NONE_VAL = Symbol("ExperimentalOption::None");
33
+ /**
34
+ * Wrapper that makes ExperimentalOption yieldable with proper type tracking.
35
+ *
36
+ * @internal
37
+ */
38
+ var OptionYieldWrap = class {
39
+ option;
40
+ constructor(option) {
41
+ this.option = option;
42
+ }
43
+ *[Symbol.iterator]() {
44
+ return yield this;
45
+ }
46
+ };
47
+ /**
48
+ * Wrapper that makes ExperimentalOption yieldable in async generators with proper type tracking.
49
+ *
50
+ * @internal
51
+ */
52
+ var AsyncOptionYieldWrap = class {
53
+ option;
54
+ constructor(option) {
55
+ this.option = require_utils.isPromiseLike(option) ? option : Promise.resolve(option);
56
+ }
57
+ async *[Symbol.asyncIterator]() {
58
+ return yield this;
59
+ }
60
+ };
61
+ /**
62
+ * A type-safe container for optional values.
63
+ *
64
+ * `ExperimentalOption<T>` represents a value that may or may not be present. It eliminates the
65
+ * need for `null` and `undefined` checks by making absence explicit through the
66
+ * type system.
67
+ *
68
+ * ## States
69
+ *
70
+ * - **Some\<T\>**: Contains a value of type `T`
71
+ * - **None**: Represents absence of a value (singleton)
72
+ *
73
+ * ## Basic Usage
74
+ *
75
+ * ```ts
76
+ * import { ExperimentalOption } from "@carbonteq/fp";
77
+ *
78
+ * // Creating ExperimentalOptions
79
+ * const some: ExperimentalOption<number> = ExperimentalOption.Some(42);
80
+ * const none: ExperimentalOption<number> = ExperimentalOption.None;
81
+ *
82
+ * // From nullable values
83
+ * const fromNull = ExperimentalOption.fromNullable(null); // None
84
+ * const fromValue = ExperimentalOption.fromNullable("hello"); // Some("hello")
85
+ *
86
+ * // Checking and extracting
87
+ * if (some.isSome()) {
88
+ * console.log(some.unwrap()); // 42
89
+ * }
90
+ *
91
+ * // Chaining operations
92
+ * const result = ExperimentalOption.Some(5)
93
+ * .map(x => x * 2)
94
+ * .flatMap(x => ExperimentalOption.Some(x + 1));
95
+ * // Some(11)
96
+ * ```
97
+ *
98
+ * ## Async Variants
99
+ *
100
+ * For operations that return Promises, use `*Async` methods:
101
+ *
102
+ * ```ts
103
+ * // Sync
104
+ * ExperimentalOption.Some(5).map(x => x * 2); // ExperimentalOption<number>
105
+ *
106
+ * // Async
107
+ * ExperimentalOption.Some(5).mapAsync(async x => x * 2); // Promise<ExperimentalOption<number>>
108
+ * ```
109
+ *
110
+ * @template T - The type of the contained value
111
+ *
112
+ * @see {@link https://carbonteq.github.io/ct-fp/option | ExperimentalOption Documentation}
113
+ */
114
+ var ExperimentalOption = class ExperimentalOption {
115
+ /** Discriminant tag for type-level identification */
116
+ _tag;
117
+ #val;
118
+ constructor(val, tag) {
119
+ this.#val = val;
120
+ this._tag = tag;
121
+ }
122
+ /**
123
+ * Singleton `None` instance representing absence of a value.
124
+ *
125
+ * This is a shared instance - all `None` values reference the same object.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const none1 = ExperimentalOption.None;
130
+ * const none2 = ExperimentalOption.None;
131
+ * none1 === none2 // true
132
+ * ```
133
+ */
134
+ static None = new ExperimentalOption(NONE_VAL, "None");
135
+ /**
136
+ * Creates a `Some` variant containing the provided value.
137
+ *
138
+ * @template Inner - The type of value to wrap
139
+ * @param val - The value to wrap in a Some
140
+ * @returns An `ExperimentalOption<Inner>` in the Some state
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const opt = ExperimentalOption.Some(42); // ExperimentalOption<number>
145
+ * const str = ExperimentalOption.Some("hello"); // ExperimentalOption<string>
146
+ * const obj = ExperimentalOption.Some({ id: 1 }); // ExperimentalOption<{ id: number }>
147
+ * ```
148
+ */
149
+ static Some(val) {
150
+ return new ExperimentalOption(val, "Some");
151
+ }
152
+ /**
153
+ * Creates an `ExperimentalOption` from a nullable value, returning `None` for `null` or `undefined`.
154
+ *
155
+ * Unlike {@link fromFalsy}, this only treats `null` and `undefined` as absence.
156
+ * Values like `0`, `""`, and `false` are considered valid and wrapped in `Some`.
157
+ *
158
+ * @template T - The input type which may include null/undefined
159
+ * @param val - The value to convert to an ExperimentalOption
160
+ * @returns `Some<NonNullable<T>>` if value is non-nullish, else `None`
161
+ *
162
+ * @example
163
+ * ```ts
164
+ * ExperimentalOption.fromNullable("hello") // Some("hello")
165
+ * ExperimentalOption.fromNullable(null) // None
166
+ * ExperimentalOption.fromNullable(undefined) // None
167
+ * ExperimentalOption.fromNullable(0) // Some(0) - 0 is not nullish
168
+ * ExperimentalOption.fromNullable("") // Some("") - empty string is not nullish
169
+ * ExperimentalOption.fromNullable(false) // Some(false) - false is not nullish
170
+ *
171
+ * // With user input
172
+ * function findUser(id: string): User | null {
173
+ * return database.getUser(id) ?? null;
174
+ * }
175
+ * const user = ExperimentalOption.fromNullable(findUser("123"));
176
+ * // ExperimentalOption<User>
177
+ * ```
178
+ */
179
+ static fromNullable(val) {
180
+ return val === null || val === void 0 ? ExperimentalOption.None : ExperimentalOption.Some(val);
181
+ }
182
+ /**
183
+ * Creates an `ExperimentalOption` from a value that may be falsy, returning `None` for falsy values.
184
+ *
185
+ * Treats `false`, `0`, `""`, `null`, and `undefined` as absence.
186
+ * Use {@link fromNullable} if you only want to exclude `null`/`undefined`.
187
+ *
188
+ * @template T - The type of value to wrap
189
+ * @param val - The value to convert to an ExperimentalOption
190
+ * @returns `Some<T>` if value is truthy, else `None`
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * ExperimentalOption.fromFalsy("hello") // Some("hello")
195
+ * ExperimentalOption.fromFalsy("") // None
196
+ * ExperimentalOption.fromFalsy(0) // None
197
+ * ExperimentalOption.fromFalsy(false) // None
198
+ * ExperimentalOption.fromFalsy(null) // None
199
+ * ExperimentalOption.fromFalsy(undefined) // None
200
+ *
201
+ * // Useful for validation
202
+ * const isValid = ExperimentalOption.fromFalsy(getConfigValue("API_KEY"));
203
+ * ```
204
+ */
205
+ static fromFalsy(val) {
206
+ return val ? ExperimentalOption.Some(val) : ExperimentalOption.None;
207
+ }
208
+ /**
209
+ * Creates an `ExperimentalOption` based on a predicate function result.
210
+ *
211
+ * Returns `Some` if the predicate returns `true`, otherwise `None`.
212
+ *
213
+ * @template T - The type of value to test
214
+ * @param val - The value to test
215
+ * @param pred - A predicate function that returns true if value should be wrapped
216
+ * @returns `Some<T>` if predicate returns true, else `None`
217
+ *
218
+ * @example
219
+ * ```ts
220
+ * const age = 25;
221
+ * ExperimentalOption.fromPredicate(age, a => a >= 18) // Some(25)
222
+ * ExperimentalOption.fromPredicate(15, a => a >= 18) // None
223
+ *
224
+ * // Can be used for validation
225
+ * const isValidEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
226
+ * ExperimentalOption.fromPredicate(user.email, isValidEmail)
227
+ *
228
+ * // With complex predicates
229
+ * const isValidPassword = (pw: string) =>
230
+ * pw.length >= 8 && /[A-Z]/.test(pw) && /[0-9]/.test(pw);
231
+ * ExperimentalOption.fromPredicate(password, isValidPassword)
232
+ * ```
233
+ */
234
+ static fromPredicate(val, pred) {
235
+ return pred(val) ? ExperimentalOption.Some(val) : ExperimentalOption.None;
236
+ }
237
+ /**
238
+ * Combines multiple ExperimentalOptions into a single ExperimentalOption of an array.
239
+ *
240
+ * Returns `Some` containing an array of all unwrapped values if ALL inputs are `Some`.
241
+ * Returns `None` if ANY input is `None`.
242
+ *
243
+ * @template T - Variadic tuple of ExperimentalOption types
244
+ * @param options - Variable number of ExperimentalOptions to combine
245
+ * @returns `ExperimentalOption<[T1, T2, ...]>` with array of values, or `None`
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * // All Some -> array of values
250
+ * const all = ExperimentalOption.all(
251
+ * ExperimentalOption.Some(1),
252
+ * ExperimentalOption.Some(2),
253
+ * ExperimentalOption.Some(3)
254
+ * );
255
+ * // Some([1, 2, 3])
256
+ *
257
+ * // Any None -> None
258
+ * const withNone = ExperimentalOption.all(
259
+ * ExperimentalOption.Some(1),
260
+ * ExperimentalOption.None,
261
+ * ExperimentalOption.Some(3)
262
+ * );
263
+ * // None
264
+ *
265
+ * // Empty -> Some([])
266
+ * const empty = ExperimentalOption.all();
267
+ * // Some([])
268
+ *
269
+ * // Practical example: combine multiple validations
270
+ * const name = ExperimentalOption.fromNullable(user.name);
271
+ * const email = ExperimentalOption.fromNullable(user.email);
272
+ * const age = ExperimentalOption.fromNullable(user.age);
273
+ *
274
+ * const complete = ExperimentalOption.all(name, email, age);
275
+ * // ExperimentalOption<[string, string, number]>
276
+ * ```
277
+ */
278
+ static all(...options) {
279
+ const vals = [];
280
+ for (const opt of options) {
281
+ if (opt.isNone()) return ExperimentalOption.None;
282
+ vals.push(opt.#val);
283
+ }
284
+ return ExperimentalOption.Some(vals);
285
+ }
286
+ /**
287
+ * Returns the first `Some` from a list of ExperimentalOptions, or `None` if all are `None`.
288
+ *
289
+ * Short-circuits on the first `Some` found, evaluating subsequent options lazily.
290
+ *
291
+ * @template T - The type of values in the ExperimentalOptions
292
+ * @param options - Variable number of ExperimentalOptions to check
293
+ * @returns The first `Some<T>` found, or `None`
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * // Returns first Some
298
+ * const first = ExperimentalOption.any(
299
+ * ExperimentalOption.None,
300
+ * ExperimentalOption.Some("First value"),
301
+ * ExperimentalOption.Some("Second value")
302
+ * );
303
+ * // Some("First value")
304
+ *
305
+ * // All None -> None
306
+ * const allNone = ExperimentalOption.any(
307
+ * ExperimentalOption.None,
308
+ * ExperimentalOption.None,
309
+ * ExperimentalOption.None
310
+ * );
311
+ * // None
312
+ *
313
+ * // Practical: Try multiple fallback sources
314
+ * const config = ExperimentalOption.any(
315
+ * ExperimentalOption.fromNullable(process.env.API_KEY),
316
+ * ExperimentalOption.fromNullable(getEnvVar("API_KEY")),
317
+ * ExperimentalOption.fromNullable(readConfigFile("API_KEY"))
318
+ * );
319
+ *
320
+ * // Empty -> None
321
+ * const empty = ExperimentalOption.any();
322
+ * // None
323
+ * ```
324
+ */
325
+ static any(...options) {
326
+ for (const opt of options) if (opt.isSome()) return opt;
327
+ return ExperimentalOption.None;
328
+ }
329
+ /**
330
+ * Type guard that narrows the type to `Some`.
331
+ *
332
+ * Returns `true` if this ExperimentalOption is in the `Some` state, containing a value.
333
+ *
334
+ * @returns `true` if this is a `Some`, `false` if `None`
335
+ *
336
+ * @example
337
+ * ```ts
338
+ * const opt: ExperimentalOption<number> = ExperimentalOption.Some(42);
339
+ *
340
+ * if (opt.isSome()) {
341
+ * // TypeScript knows opt is ExperimentalOption<number> & { _tag: "Some" }
342
+ * console.log(opt.unwrap()); // 42
343
+ * }
344
+ * ```
345
+ */
346
+ isSome() {
347
+ return this._tag === "Some" && this.#val !== NONE_VAL;
348
+ }
349
+ /**
350
+ * Type guard that narrows the type to `None`.
351
+ *
352
+ * Returns `true` if this ExperimentalOption is in the `None` state (absence of value).
353
+ *
354
+ * @returns `true` if this is `None`, `false` if `Some`
355
+ *
356
+ * @example
357
+ * ```ts
358
+ * const opt: ExperimentalOption<number> = ExperimentalOption.None;
359
+ *
360
+ * if (opt.isNone()) {
361
+ * // TypeScript knows opt is ExperimentalOption<never> & { _tag: "None" }
362
+ * console.log("No value present");
363
+ * }
364
+ * ```
365
+ */
366
+ isNone() {
367
+ return this._tag === "None" || this.#val === NONE_VAL;
368
+ }
369
+ /**
370
+ * Type guard that checks if the contained value is the `Unit` type.
371
+ *
372
+ * Returns `true` if this ExperimentalOption contains the special `UNIT` value,
373
+ * used to represent operations that produce no meaningful output.
374
+ *
375
+ * @returns `true` if this contains `UNIT`, `false` otherwise
376
+ *
377
+ * @example
378
+ * ```ts
379
+ * const unitOpt: ExperimentalOption<UNIT> = ExperimentalOption.Some(UNIT);
380
+ * unitOpt.isUnit(); // true
381
+ *
382
+ * const normalOpt: ExperimentalOption<number> = ExperimentalOption.Some(42);
383
+ * normalOpt.isUnit(); // false
384
+ * ```
385
+ */
386
+ isUnit() {
387
+ return this.#val === require_unit.UNIT;
388
+ }
389
+ /**
390
+ * Returns the contained value or throws {@link UnwrappedNone} if `None`.
391
+ *
392
+ * This is an unsafe operation that throws on `None`. Use only when you are
393
+ * certain the ExperimentalOption contains a value, or use safe alternatives like
394
+ * {@link unwrapOr} or {@link safeUnwrap}.
395
+ *
396
+ * @throws {@link UnwrappedNone} if this is `None`
397
+ * @returns The contained value
398
+ *
399
+ * @example
400
+ * ```ts
401
+ * ExperimentalOption.Some(42).unwrap(); // 42
402
+ * ExperimentalOption.None.unwrap(); // throws UnwrappedNone
403
+ *
404
+ * // Safe usage pattern
405
+ * const opt = ExperimentalOption.Some(42);
406
+ * if (opt.isSome()) {
407
+ * const value = opt.unwrap(); // Safe: we checked isSome()
408
+ * }
409
+ * ```
410
+ *
411
+ * @see {@link unwrapOr} for safe unwrapping with a default value
412
+ * @see {@link unwrapOrElse} for lazy default computation
413
+ * @see {@link safeUnwrap} for null-returning safe unwrap
414
+ */
415
+ unwrap() {
416
+ if (this.isNone()) throw UNWRAPPED_NONE_ERR;
417
+ return this.#val;
418
+ }
419
+ /**
420
+ * Returns the contained value, or the provided default value if `None`.
421
+ *
422
+ * A safe alternative to {@link unwrap} that doesn't throw.
423
+ *
424
+ * @param defaultValue - The value to return if this is `None`
425
+ * @returns The contained value, or the default
426
+ *
427
+ * @example
428
+ * ```ts
429
+ * ExperimentalOption.Some(42).unwrapOr(0); // 42
430
+ * ExperimentalOption.None.unwrapOr(0); // 0
431
+ *
432
+ * // With user input
433
+ * const age = ExperimentalOption.fromNullable(user.age).unwrapOr(18);
434
+ *
435
+ * // With complex defaults
436
+ * const config = ExperimentalOption.fromNullable(getConfig())
437
+ * .unwrapOr({ port: 3000, host: "localhost" });
438
+ * ```
439
+ *
440
+ * @see {@link unwrapOrElse} for lazy default computation
441
+ * @see {@link safeUnwrap} for null-returning safe unwrap
442
+ */
443
+ unwrapOr(defaultValue) {
444
+ if (this.isNone()) return defaultValue;
445
+ return this.#val;
446
+ }
447
+ /**
448
+ * Returns the contained value, or computes a default via a function if `None`.
449
+ *
450
+ * Unlike {@link unwrapOr}, the default is computed lazily - only if this is `None`.
451
+ * Use this when the default value is expensive to compute.
452
+ *
453
+ * @param fn - A factory function that produces the default value
454
+ * @returns The contained value, or the computed default
455
+ *
456
+ * @example
457
+ * ```ts
458
+ * // Lazy default - only computed if needed
459
+ * const value = ExperimentalOption.None.unwrapOrElse(() => {
460
+ * console.log("Computing expensive default...");
461
+ * return expensivelyComputeDefault();
462
+ * });
463
+ * // Prints "Computing expensive default..."
464
+ *
465
+ * // Not computed when Some
466
+ * const some = ExperimentalOption.Some(42).unwrapOrElse(() => {
467
+ * console.log("This won't print");
468
+ * return expensivelyComputeDefault();
469
+ * });
470
+ * // Returns 42 without printing
471
+ *
472
+ * // Practical: cache lookup with fallback
473
+ * const cached = ExperimentalOption.fromNullable(cache.get(key))
474
+ * .unwrapOrElse(() => {
475
+ * const value = fetchFromDatabase(key);
476
+ * cache.set(key, value);
477
+ * return value;
478
+ * });
479
+ * ```
480
+ *
481
+ * @see {@link unwrapOr} for simple default values
482
+ * @see {@link unwrap} for unsafe unwrapping
483
+ */
484
+ unwrapOrElse(fn) {
485
+ if (this.isNone()) return fn();
486
+ return this.#val;
487
+ }
488
+ /**
489
+ * Returns the contained value, or `null` if `None`.
490
+ *
491
+ * Provides a safe way to extract values that integrates with TypeScript's
492
+ * null checking while maintaining the ExperimentalOption semantics.
493
+ *
494
+ * @returns The contained value, or `null`
495
+ *
496
+ * @example
497
+ * ```ts
498
+ * ExperimentalOption.Some(42).safeUnwrap(); // 42
499
+ * ExperimentalOption.None.safeUnwrap(); // null
500
+ *
501
+ * // Integration with existing null-handling code
502
+ * const value = ExperimentalOption.fromNullable(user.name).safeUnwrap();
503
+ * if (value !== null) {
504
+ * console.log(value.toUpperCase());
505
+ * }
506
+ *
507
+ * // With nullish coalescing
508
+ * const displayName = ExperimentalOption.fromNullable(user.displayName)
509
+ * .safeUnwrap() ?? user.email;
510
+ * ```
511
+ *
512
+ * @see {@link unwrapOr} for typed defaults
513
+ * @see {@link unwrapOrElse} for lazy defaults
514
+ */
515
+ safeUnwrap() {
516
+ if (this.isNone()) return null;
517
+ return this.#val;
518
+ }
519
+ /**
520
+ * Pattern matches on both states of the ExperimentalOption, providing exhaustive handling.
521
+ *
522
+ * This is the most explicit way to handle ExperimentalOptions, ensuring both `Some` and `None`
523
+ * cases are handled at compile time.
524
+ *
525
+ * @template U - The result type of both branches
526
+ * @param cases - Object containing handlers for both states
527
+ * @returns The result of calling the appropriate handler
528
+ *
529
+ * @example
530
+ * ```ts
531
+ * const result = ExperimentalOption.Some(42).match({
532
+ * Some: (value) => `Got: ${value}`,
533
+ * None: () => "Got nothing"
534
+ * });
535
+ * // "Got: 42"
536
+ *
537
+ * const noneResult = ExperimentalOption.None.match({
538
+ * Some: (value) => `Got: ${value}`,
539
+ * None: () => "Got nothing"
540
+ * });
541
+ * // "Got nothing"
542
+ *
543
+ * // Practical: user lookup
544
+ * const message = findUser(userId).match({
545
+ * Some: (user) => `Welcome, ${user.name}!`,
546
+ * None: () => "User not found"
547
+ * });
548
+ * ```
549
+ *
550
+ * @see {@link isSome} and {@link isNone} for conditional handling
551
+ */
552
+ match(cases) {
553
+ if (this.isNone()) return cases.None();
554
+ return cases.Some(this.#val);
555
+ }
556
+ /**
557
+ * Pattern matches using positional arguments (FP convention alias for `match`).
558
+ *
559
+ * This is a more concise alternative to `match` using positional arguments
560
+ * instead of an object. Familiar to developers from Scala, Haskell, and other FP languages.
561
+ *
562
+ * @template U - The result type of both branches
563
+ * @param onSome - Handler called with the value if `Some`
564
+ * @param onNone - Handler called if `None`
565
+ * @returns The result of calling the appropriate handler
566
+ *
567
+ * @example
568
+ * ```ts
569
+ * const result = ExperimentalOption.Some(42).fold(
570
+ * (value) => `Got: ${value}`,
571
+ * () => "Got nothing"
572
+ * );
573
+ * // "Got: 42"
574
+ *
575
+ * const noneResult = ExperimentalOption.None.fold(
576
+ * (value) => `Got: ${value}`,
577
+ * () => "Got nothing"
578
+ * );
579
+ * // "Got nothing"
580
+ *
581
+ * // Equivalent to match:
582
+ * experimentalOption.fold(onSome, onNone);
583
+ * experimentalOption.match({ Some: onSome, None: onNone });
584
+ * ```
585
+ *
586
+ * @see {@link match} for object-based pattern matching
587
+ * @see {@link foldAsync} for async handlers
588
+ */
589
+ fold(onSome, onNone) {
590
+ if (this.isNone()) return onNone();
591
+ return onSome(this.#val);
592
+ }
593
+ /**
594
+ * Async pattern matching using positional arguments.
595
+ *
596
+ * Async variant of `fold` for when handlers return Promises.
597
+ * Rejections from `onSome` are propagated (not converted to `None`).
598
+ *
599
+ * @template U - The result type of both branches
600
+ * @param onSome - Async handler called with the value if `Some`
601
+ * @param onNone - Async handler called if `None`
602
+ * @returns Promise resolving to the result of the appropriate handler
603
+ *
604
+ * @example
605
+ * ```ts
606
+ * const result = await ExperimentalOption.Some(userId).foldAsync(
607
+ * async (id) => await fetchUser(id),
608
+ * async () => await getDefaultUser()
609
+ * );
610
+ *
611
+ * // With database lookup
612
+ * const user = await ExperimentalOption.fromNullable(sessionUserId).foldAsync(
613
+ * async (id) => await db.users.findById(id),
614
+ * async () => ({ id: 0, name: "Guest" })
615
+ * );
616
+ * ```
617
+ *
618
+ * @see {@link fold} for synchronous handlers
619
+ * @see {@link matchAsync} for object-based async pattern matching
620
+ */
621
+ async foldAsync(onSome, onNone) {
622
+ if (this.isNone()) return onNone();
623
+ return onSome(this.#val);
624
+ }
625
+ /**
626
+ * Async pattern matching on both states of the ExperimentalOption.
627
+ *
628
+ * Async variant of `match` for when handlers return Promises.
629
+ * Rejections from `Some` handler are propagated (not converted to `None`).
630
+ *
631
+ * @template U - The result type of both branches
632
+ * @param cases - Object containing async handlers for both states
633
+ * @returns Promise resolving to the result of the appropriate handler
634
+ *
635
+ * @example
636
+ * ```ts
637
+ * const result = await ExperimentalOption.Some(userId).matchAsync({
638
+ * Some: async (id) => await fetchUser(id),
639
+ * None: async () => await getDefaultUser()
640
+ * });
641
+ *
642
+ * // With API calls
643
+ * const data = await maybeConfig.matchAsync({
644
+ * Some: async (config) => await loadWithConfig(config),
645
+ * None: async () => await loadDefaults()
646
+ * });
647
+ * ```
648
+ *
649
+ * @see {@link match} for synchronous pattern matching
650
+ * @see {@link foldAsync} for positional-argument async matching
651
+ */
652
+ async matchAsync(cases) {
653
+ if (this.isNone()) return cases.None();
654
+ return cases.Some(this.#val);
655
+ }
656
+ /**
657
+ * Pattern matches with a subset of cases, using a default for unhandled cases.
658
+ *
659
+ * Unlike `match` which requires all cases, `matchPartial` allows handling
660
+ * only specific cases with a fallback default function.
661
+ *
662
+ * @template U - The result type
663
+ * @param cases - Partial object with optional `Some` and `None` handlers
664
+ * @param getDefault - Lazy default function for unhandled cases
665
+ * @returns The result of the matching handler or the default
666
+ *
667
+ * @example
668
+ * ```ts
669
+ * // Only handle Some, default for None
670
+ * ExperimentalOption.Some(42).matchPartial({ Some: (v) => v * 2 }, () => 0); // 84
671
+ * ExperimentalOption.None.matchPartial({ Some: (v) => v * 2 }, () => 0); // 0
672
+ *
673
+ * // Only handle None
674
+ * ExperimentalOption.Some(42).matchPartial({ None: () => -1 }, () => 100); // 100
675
+ * ExperimentalOption.None.matchPartial({ None: () => -1 }, () => 100); // -1
676
+ *
677
+ * // Lazy default with function
678
+ * experimentalOption.matchPartial(
679
+ * { Some: (v) => process(v) },
680
+ * () => computeExpensiveDefault()
681
+ * );
682
+ * ```
683
+ *
684
+ * @see {@link match} for exhaustive pattern matching
685
+ */
686
+ matchPartial(cases, getDefault) {
687
+ if (this.isNone()) return cases.None ? cases.None() : getDefault();
688
+ return cases.Some ? cases.Some(this.#val) : getDefault();
689
+ }
690
+ map(fn) {
691
+ if (this.isNone()) return ExperimentalOption.None;
692
+ return ExperimentalOption.Some(fn(this.#val));
693
+ }
694
+ /**
695
+ * Transforms the contained value using an async function.
696
+ *
697
+ * If `Some`, awaits the async function and wraps the result in a new `Some`.
698
+ * If `None`, returns a resolved `None` Promise without calling the function.
699
+ *
700
+ * @template U - The type of the transformed value
701
+ * @param fn - An async function that transforms the contained value
702
+ * @returns `Promise<Some<U>>` with the transformed value, or `Promise<None>`
703
+ *
704
+ * @example
705
+ * ```ts
706
+ * // Simple async transform
707
+ * const result = await ExperimentalOption.Some(5).mapAsync(async x => {
708
+ * return x * 2;
709
+ * });
710
+ * // Some(10)
711
+ *
712
+ * // Chaining with .then()
713
+ * ExperimentalOption.Some(5)
714
+ * .map(x => x * 2) // Some(10)
715
+ * .mapAsync(async x => {
716
+ * return await fetchData(x); // Some<Data>
717
+ * })
718
+ * .then(o => o.map(d => d.name)); // Promise<Some<string>>
719
+ *
720
+ * // Practical: API call after validation
721
+ * const userData = await ExperimentalOption.Some(userId)
722
+ * .mapAsync(async id => await fetchUser(id))
723
+ * .then(o => o.mapAsync(async user => await fetchProfile(user.id)));
724
+ * // Promise<ExperimentalOption<Profile>>
725
+ * ```
726
+ *
727
+ * @see {@link map} for synchronous transformations
728
+ */
729
+ async mapAsync(fn) {
730
+ if (this.isNone()) return Promise.resolve(ExperimentalOption.None);
731
+ return fn(this.#val).then((u) => ExperimentalOption.Some(u));
732
+ }
733
+ /**
734
+ * Maps the value or returns a default value (unwrapped).
735
+ *
736
+ * Unlike {@link map}, this returns the value directly (not wrapped in ExperimentalOption).
737
+ * If `Some`, applies the function and returns the result.
738
+ * If `None`, returns the default value without calling the function.
739
+ *
740
+ * @template U - The type of the result
741
+ * @param defaultValue - The value to return if `None`
742
+ * @param fn - A function to apply if `Some`
743
+ * @returns The mapped value or the default
744
+ *
745
+ * @example
746
+ * ```ts
747
+ * ExperimentalOption.Some(5).mapOr(0, x => x * 2); // 10
748
+ * ExperimentalOption.None.mapOr(0, x => x * 2); // 0
749
+ *
750
+ * // Practical: formatting with fallback
751
+ * const displayName = ExperimentalOption.fromNullable(user.name)
752
+ * .mapOr("Anonymous", name => name.toUpperCase());
753
+ * // "JOHN" or "Anonymous"
754
+ *
755
+ * // With complex defaults
756
+ * const balance = ExperimentalOption.fromNullable(account?.balance)
757
+ * .mapOr(0, b => b.toFixed(2));
758
+ * ```
759
+ *
760
+ * @see {@link map} for wrapped results
761
+ * @see {@link mapOrAsync} for async transformations
762
+ */
763
+ mapOr(defaultValue, fn) {
764
+ if (this.isNone()) return defaultValue;
765
+ return fn(this.#val);
766
+ }
767
+ /**
768
+ * Maps the value using an async function or returns a default value.
769
+ *
770
+ * @template U - The type of the result
771
+ * @param defaultValue - The value to return if `None`
772
+ * @param fn - An async function to apply if `Some`
773
+ * @returns `Promise<U>` with the mapped value or the default
774
+ *
775
+ * @example
776
+ * ```ts
777
+ * const result = await ExperimentalOption.Some(5).mapOrAsync(0, async x => {
778
+ * return x * 2;
779
+ * });
780
+ * // 10
781
+ *
782
+ * const noneResult = await ExperimentalOption.None.mapOrAsync(0, async x => {
783
+ * return x * 2;
784
+ * });
785
+ * // 0
786
+ *
787
+ * // Practical: async fetch with fallback
788
+ * const balance = await ExperimentalOption.fromNullable(userId)
789
+ * .mapOrAsync(0, async id => await getAccountBalance(id));
790
+ * ```
791
+ *
792
+ * @see {@link mapOr} for synchronous transformations
793
+ */
794
+ async mapOrAsync(defaultValue, fn) {
795
+ if (this.isNone()) return defaultValue;
796
+ return fn(this.#val);
797
+ }
798
+ /**
799
+ * Chains operations that return ExperimentalOptions, flattening nested ExperimentalOptions.
800
+ *
801
+ * Also known as `bind` or `chain`. Use this to sequence operations where
802
+ * each step depends on the previous value and may fail.
803
+ *
804
+ * If `Some`, applies the function and returns its result directly.
805
+ * If `None`, returns `None` without calling the function.
806
+ *
807
+ * @template U - The type of the value in the returned ExperimentalOption
808
+ * @param fn - A function that returns an ExperimentalOption
809
+ * @returns The result of the function, or `None`
810
+ *
811
+ * @example
812
+ * ```ts
813
+ * // Basic chaining
814
+ * ExperimentalOption.Some(5).flatMap(x => ExperimentalOption.Some(x + 1)); // Some(6)
815
+ * ExperimentalOption.Some(5).flatMap(x => ExperimentalOption.None); // None
816
+ * ExperimentalOption.None.flatMap(x => ExperimentalOption.Some(x + 1)); // None
817
+ *
818
+ * // Chaining optional operations
819
+ * const email = findUser(userId)
820
+ * .flatMap(user => ExperimentalOption.fromNullable(user.profile))
821
+ * .flatMap(profile => ExperimentalOption.fromNullable(profile.avatar))
822
+ * .flatMap(avatar => ExperimentalOption.fromNullable(avatar.url));
823
+ *
824
+ * // Practical: multi-step validation
825
+ * const valid = ExperimentalOption.Some(input)
826
+ * .flatMap(s => parseEmail(s))
827
+ * .flatMap(email => checkDomain(email))
828
+ * .flatMap(domain => resolveDomain(domain));
829
+ * ```
830
+ *
831
+ * @see {@link flatMapAsync} for async operations
832
+ * @see {@link map} for simple transformations
833
+ * @see {@link gen} for imperative-style chaining
834
+ */
835
+ flatMap(fn) {
836
+ if (this.isNone()) return ExperimentalOption.None;
837
+ return fn(this.#val);
838
+ }
839
+ /**
840
+ * Chains async operations that return ExperimentalOptions.
841
+ *
842
+ * @template U - The type of the value in the returned ExperimentalOption
843
+ * @param fn - An async function that returns an ExperimentalOption
844
+ * @returns `Promise<ExperimentalOption<U>>` with the result, or `Promise<None>`
845
+ *
846
+ * @example
847
+ * ```ts
848
+ * // Async database lookup chain
849
+ * const profile = await ExperimentalOption.Some(userId)
850
+ * .flatMapAsync(async id => await findUser(id))
851
+ * .flatMapAsync(async user => await getProfile(user.id))
852
+ * .flatMapAsync(async p => await getAvatar(p.avatarId));
853
+ * // Promise<ExperimentalOption<Avatar>>
854
+ *
855
+ * // Practical: multi-step async pipeline
856
+ * const result = await ExperimentalOption.Some(email)
857
+ * .flatMapAsync(validateEmail)
858
+ * .flatMapAsync(e => fetchUserByEmail(e))
859
+ * .flatMapAsync(u => getUserPermissions(u.id));
860
+ * ```
861
+ *
862
+ * @see {@link flatMap} for synchronous operations
863
+ * @see {@link asyncGen} for imperative-style async chaining
864
+ */
865
+ async flatMapAsync(fn) {
866
+ if (this.isNone()) return ExperimentalOption.None;
867
+ return fn(this.#val);
868
+ }
869
+ /**
870
+ * Converts `Some` to `None` if the predicate returns `false`.
871
+ *
872
+ * If `Some`, tests the predicate and returns `Some` if true, `None` if false.
873
+ * If `None`, returns `None` without calling the predicate.
874
+ *
875
+ * @param pred - A predicate function to test the contained value
876
+ * @returns `Some<T>` if predicate passes, else `None`
877
+ *
878
+ * @example
879
+ * ```ts
880
+ * ExperimentalOption.Some(5).filter(x => x > 3); // Some(5)
881
+ * ExperimentalOption.Some(5).filter(x => x > 10); // None
882
+ * ExperimentalOption.None.filter(x => x > 3); // None
883
+ *
884
+ * // Age validation
885
+ * const adult = ExperimentalOption.Some(age)
886
+ * .filter(a => a >= 18);
887
+ *
888
+ * // Multiple filters
889
+ * const valid = ExperimentalOption.Some(password)
890
+ * .filter(pw => pw.length >= 8)
891
+ * .filter(pw => /[A-Z]/.test(pw))
892
+ * .filter(pw => /[0-9]/.test(pw));
893
+ * ```
894
+ *
895
+ * @see {@link filterAsync} for async predicates
896
+ * @see {@link fromPredicate} for creating ExperimentalOptions from predicates
897
+ */
898
+ filter(pred) {
899
+ if (this.isNone()) return ExperimentalOption.None;
900
+ return pred(this.#val) ? this : ExperimentalOption.None;
901
+ }
902
+ /**
903
+ * Converts `Some` to `None` if the async predicate returns `false`.
904
+ *
905
+ * @param pred - An async predicate function to test the contained value
906
+ * @returns `Promise<Some<T>>` if predicate passes, else `Promise<None>`
907
+ *
908
+ * @example
909
+ * ```ts
910
+ * // Async validation
911
+ * const valid = await ExperimentalOption.Some(email)
912
+ * .filterAsync(async e => await checkEmailDomain(e));
913
+ *
914
+ * // Practical: database validation
915
+ * const unique = await ExperimentalOption.Some(username)
916
+ * .filterAsync(async u => await isUsernameAvailable(u));
917
+ * ```
918
+ *
919
+ * @see {@link filter} for synchronous predicates
920
+ */
921
+ async filterAsync(pred) {
922
+ if (this.isNone()) return Promise.resolve(ExperimentalOption.None);
923
+ return pred(this.#val).then((passed) => passed ? this : ExperimentalOption.None);
924
+ }
925
+ zip(fn) {
926
+ if (this.isNone()) return ExperimentalOption.None;
927
+ return ExperimentalOption.Some([this.#val, fn(this.#val)]);
928
+ }
929
+ /**
930
+ * Pairs the original value with an async derived value.
931
+ *
932
+ * @template U - The type of the derived value
933
+ * @param fn - An async function that derives a value from the contained value
934
+ * @returns `Promise<Some<[T, U]>>` with the tuple, or `Promise<None>`
935
+ *
936
+ * @example
937
+ * ```ts
938
+ * const result = await ExperimentalOption.Some(user)
939
+ * .zipAsync(async u => await fetchCount(u));
940
+ * // Some([user, number])
941
+ *
942
+ * // Practical: user with additional data
943
+ * const enriched = await ExperimentalOption.Some(userId)
944
+ * .zipAsync(async id => await fetchUser(id))
945
+ * .then(o => o.zipAsync(async ([id, user]) => await getStats(user.id)));
946
+ * // Promise<ExperimentalOption<[userId, User, Stats]>>
947
+ * ```
948
+ *
949
+ * @see {@link zip} for synchronous derivation
950
+ */
951
+ async zipAsync(fn) {
952
+ if (this.isNone()) return Promise.resolve(ExperimentalOption.None);
953
+ return fn(this.#val).then((u) => ExperimentalOption.Some([this.#val, u]));
954
+ }
955
+ /**
956
+ * Pairs the original value with a value from another ExperimentalOption.
957
+ *
958
+ * Combines two independent ExperimentalOptions into a tuple. Both must be `Some` to succeed.
959
+ *
960
+ * @template U - The type of the value in the other ExperimentalOption
961
+ * @param fn - A function that returns an ExperimentalOption to combine with
962
+ * @returns `Some<[T, U]>` with both values, or `None`
963
+ *
964
+ * @example
965
+ * ```ts
966
+ * // Basic usage
967
+ * ExperimentalOption.Some(5).flatZip(x => ExperimentalOption.Some(10)); // Some([5, 10])
968
+ * ExperimentalOption.Some(5).flatZip(x => ExperimentalOption.None); // None
969
+ * ExperimentalOption.None.flatZip(x => ExperimentalOption.Some(10)); // None
970
+ *
971
+ * // Practical: combine independent lookups
972
+ * const productData = ExperimentalOption.Some(productId)
973
+ * .flatZip(id => fetchProductPrice(id));
974
+ * // ExperimentalOption<[productId, price]>
975
+ *
976
+ * // Multiple dependencies
977
+ * const complete = ExperimentalOption.Some(userId)
978
+ * .flatZip(id => fetchUser(id))
979
+ * .flatMap(([id, user]) => ExperimentalOption.Some([id, user.profileId]))
980
+ * .flatZip(([id, profileId]) => fetchProfile(profileId));
981
+ * // ExperimentalOption<[userId, profileId, profile]>
982
+ * ```
983
+ *
984
+ * @see {@link flatZipAsync} for async ExperimentalOptions
985
+ * @see {@link zip} for derived values
986
+ * @see {@link all} for combining multiple ExperimentalOptions at once
987
+ */
988
+ flatZip(fn) {
989
+ if (this.isNone()) return ExperimentalOption.None;
990
+ const other = fn(this.#val);
991
+ if (other.isNone()) return ExperimentalOption.None;
992
+ return ExperimentalOption.Some([this.#val, other.unwrap()]);
993
+ }
994
+ /**
995
+ * Pairs the original value with a value from an async ExperimentalOption.
996
+ *
997
+ * @template U - The type of the value in the other ExperimentalOption
998
+ * @param fn - An async function that returns an ExperimentalOption
999
+ * @returns `Promise<Some<[T, U]>>` with both values, or `Promise<None>`
1000
+ *
1001
+ * @example
1002
+ * ```ts
1003
+ * const result = await ExperimentalOption.Some(userId)
1004
+ * .flatZipAsync(async id => await fetchUser(id));
1005
+ * // ExperimentalOption<[userId, User]>
1006
+ *
1007
+ * // Practical: async pipeline
1008
+ * const enriched = await ExperimentalOption.Some(userId)
1009
+ * .flatZipAsync(async id => await fetchUser(id))
1010
+ * .then(o => o.flatMapAsync(async ([id, user]) =>
1011
+ * ExperimentalOption.Some([id, user, await fetchProfile(user.profileId)])
1012
+ * ));
1013
+ * ```
1014
+ *
1015
+ * @see {@link flatZip} for synchronous ExperimentalOptions
1016
+ */
1017
+ async flatZipAsync(fn) {
1018
+ if (this.isNone()) return Promise.resolve(ExperimentalOption.None);
1019
+ return fn(this.#val).then((other) => {
1020
+ if (other.isNone()) return ExperimentalOption.None;
1021
+ return ExperimentalOption.Some([this.#val, other.unwrap()]);
1022
+ });
1023
+ }
1024
+ /**
1025
+ * Executes a side effect function if `Some`, returning `this` unchanged.
1026
+ *
1027
+ * Useful for logging, debugging, or executing effects without breaking the chain.
1028
+ *
1029
+ * @param fn - A side effect function to execute with the contained value
1030
+ * @returns `this` (unchanged, for chaining)
1031
+ *
1032
+ * @example
1033
+ * ```ts
1034
+ * // Logging without breaking chain
1035
+ * ExperimentalOption.Some(user)
1036
+ * .tap(u => console.log(`Processing ${u.name}`))
1037
+ * .map(u => u.email);
1038
+ *
1039
+ * // Debugging
1040
+ * ExperimentalOption.Some(input)
1041
+ * .tap(val => console.log("Before:", val))
1042
+ * .map(transform)
1043
+ * .tap(val => console.log("After:", val));
1044
+ *
1045
+ * // Practical: audit trail
1046
+ * const result = ExperimentalOption.Some(transaction)
1047
+ * .tap((t) => auditLog("start", t.id))
1048
+ * .flatMap((t) => process(t))
1049
+ * .tap((r) => auditLog("success", r.id));
1050
+ * ```
1051
+ *
1052
+ * @see {@link tapAsync} for async side effects
1053
+ */
1054
+ tap(fn) {
1055
+ if (this.isSome()) fn(this.#val);
1056
+ return this;
1057
+ }
1058
+ /**
1059
+ * Executes an async side effect function if `Some`, returning `this` unchanged.
1060
+ *
1061
+ * @param fn - An async side effect function to execute
1062
+ * @returns `Promise<ExperimentalOption<T>>` that resolves to `this`
1063
+ *
1064
+ * @example
1065
+ * ```ts
1066
+ * // Async logging
1067
+ * await ExperimentalOption.Some(user)
1068
+ * .tapAsync(async u => await logAnalytics(u.id))
1069
+ * .map(u => u.email);
1070
+ *
1071
+ * // Practical: metrics collection
1072
+ * const result = await ExperimentalOption.Some(request)
1073
+ * .tapAsync(async req => await trackMetric("request_start", req.id))
1074
+ * .flatMapAsync(async req => await processRequest(req))
1075
+ * .tapAsync(async res => await trackMetric("request_success", res.id));
1076
+ * ```
1077
+ *
1078
+ * @see {@link tap} for synchronous side effects
1079
+ */
1080
+ async tapAsync(fn) {
1081
+ if (this.isSome()) return fn(this.#val).then(() => this);
1082
+ return Promise.resolve(this);
1083
+ }
1084
+ /**
1085
+ * Converts this ExperimentalOption to a Result.
1086
+ *
1087
+ * `Some` becomes `Ok`, `None` becomes `Err` with the provided error.
1088
+ *
1089
+ * @template E - The type of the error for the `Err` variant
1090
+ * @param error - The error value to use if this is `None`
1091
+ * @returns `Result<T, E>` with the value or error
1092
+ *
1093
+ * @example
1094
+ * ```ts
1095
+ * ExperimentalOption.Some(42).toResult("was none"); // Ok(42)
1096
+ * ExperimentalOption.None.toResult("was none"); // Err("was none")
1097
+ *
1098
+ * // Practical: convert to Result for error handling
1099
+ * const user = ExperimentalOption.fromNullable(getUser())
1100
+ * .toResult(new Error("User not found"));
1101
+ *
1102
+ * // With custom error types
1103
+ * const config = ExperimentalOption.fromNullable(loadConfig())
1104
+ * .toResult({ code: "CONFIG_MISSING", message: "Config file not found" });
1105
+ * ```
1106
+ *
1107
+ * @see {@link ExperimentalResult} for the Result type
1108
+ */
1109
+ toResult(error) {
1110
+ if (this.isNone()) return require_result_experimental.ExperimentalResult.Err(error);
1111
+ return require_result_experimental.ExperimentalResult.Ok(this.#val);
1112
+ }
1113
+ /**
1114
+ * Maps over array elements inside an `ExperimentalOption<Array<T>>`.
1115
+ *
1116
+ * Only callable when `T` is an array type. Applies the mapper to each
1117
+ * element if `Some`, or returns `None`.
1118
+ *
1119
+ * @template Inner - The element type of the array
1120
+ * @template Out - The output element type
1121
+ * @param mapper - A function to transform each array element
1122
+ * @returns `Some<Out[]>` with mapped array, or `None`
1123
+ * @throws {TypeError} if called on a non-array ExperimentalOption
1124
+ *
1125
+ * @example
1126
+ * ```ts
1127
+ * ExperimentalOption.Some([1, 2, 3]).innerMap(n => n * 2);
1128
+ * // Some([2, 4, 6])
1129
+ *
1130
+ * ExperimentalOption.None.innerMap(n => n * 2);
1131
+ * // None
1132
+ *
1133
+ * // Practical: transform list results
1134
+ * const users = await fetchUsers() // ExperimentalOption<User[]>
1135
+ * .innerMap(u => u.name)
1136
+ * .innerMap(names => names.sort());
1137
+ * // Some<string[]>
1138
+ *
1139
+ * // Error case
1140
+ * ExperimentalOption.Some(42).innerMap(x => x * 2);
1141
+ * // throws TypeError: innerMap can only be called on ExperimentalOption<Array<T>>
1142
+ * ```
1143
+ *
1144
+ * @see {@link map} for transforming the contained value directly
1145
+ */
1146
+ innerMap(mapper) {
1147
+ if (this.isNone()) return ExperimentalOption.None;
1148
+ if (!Array.isArray(this.#val)) throw new TypeError("innerMap can only be called on ExperimentalOption<Array<T>>");
1149
+ return ExperimentalOption.Some(this.#val.map(mapper));
1150
+ }
1151
+ /**
1152
+ * Returns a string representation of the ExperimentalOption.
1153
+ *
1154
+ * @returns A string describing the ExperimentalOption state and value
1155
+ *
1156
+ * @example
1157
+ * ```ts
1158
+ * ExperimentalOption.Some(42).toString(); // "ExperimentalOption::Some(42)"
1159
+ * ExperimentalOption.None.toString(); // "ExperimentalOption::None"
1160
+ * ExperimentalOption.Some("hello").toString(); // "ExperimentalOption::Some(hello)"
1161
+ * ```
1162
+ */
1163
+ toString() {
1164
+ if (this.isNone()) return "ExperimentalOption::None";
1165
+ return `ExperimentalOption::Some(${String(this.#val)})`;
1166
+ }
1167
+ /**
1168
+ * Makes ExperimentalOption iterable for use with generator-based syntax.
1169
+ *
1170
+ * Yields self and returns the unwrapped value when resumed.
1171
+ * Used internally by {@link ExperimentalOption.gen} for `yield*` syntax.
1172
+ *
1173
+ * @example
1174
+ * ```ts
1175
+ * const result = ExperimentalOption.gen(function* () {
1176
+ * const value = yield* ExperimentalOption.Some(42);
1177
+ * return value * 2;
1178
+ * });
1179
+ * // Some(84)
1180
+ * ```
1181
+ *
1182
+ * @internal
1183
+ */
1184
+ *[Symbol.iterator]() {
1185
+ const trace = (/* @__PURE__ */ new Error()).stack;
1186
+ return yield new require_utils.CapturedTrace(this, trace);
1187
+ }
1188
+ /**
1189
+ * Makes ExperimentalOption iterable for use with async generator-based syntax.
1190
+ *
1191
+ * Yields self and returns the unwrapped value when resumed.
1192
+ * Used internally by {@link ExperimentalOption.asyncGen} for `yield*` syntax.
1193
+ *
1194
+ * @example
1195
+ * ```ts
1196
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1197
+ * const value = yield* await Promise.resolve(ExperimentalOption.Some(42));
1198
+ * return value * 2;
1199
+ * });
1200
+ * // Some(84)
1201
+ * ```
1202
+ *
1203
+ * @internal
1204
+ */
1205
+ async *[Symbol.asyncIterator]() {
1206
+ const trace = (/* @__PURE__ */ new Error()).stack;
1207
+ return yield new require_utils.CapturedTrace(this, trace);
1208
+ }
1209
+ /**
1210
+ * Generator-based syntax for chaining ExperimentalOption operations (simplified, no adapter).
1211
+ *
1212
+ * Provides imperative-style code while maintaining functional ExperimentalOption handling.
1213
+ * Use `yield*` to unwrap ExperimentalOptions or short-circuit on `None`.
1214
+ *
1215
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
1216
+ * instead of recursion to avoid stack overflow on deep chains.
1217
+ *
1218
+ * @template T - The return type of the generator
1219
+ * @param genFn - A generator function that yields ExperimentalOptions
1220
+ * @returns `Some<T>` with the generator's return value, or `None`
1221
+ *
1222
+ * @example
1223
+ * ```ts
1224
+ * // Simple sync chain
1225
+ * const result = ExperimentalOption.gen(function* () {
1226
+ * const a = yield* ExperimentalOption.Some(1);
1227
+ * const b = yield* ExperimentalOption.Some(2);
1228
+ * return a + b;
1229
+ * });
1230
+ * // Some(3)
1231
+ *
1232
+ * // None short-circuit
1233
+ * const shortCircuit = ExperimentalOption.gen(function* () {
1234
+ * const a = yield* ExperimentalOption.Some(1);
1235
+ * const b = yield* ExperimentalOption.None; // Short-circuits here
1236
+ * const c = yield* ExperimentalOption.Some(3); // Never executes
1237
+ * return a + b + c;
1238
+ * });
1239
+ * // None
1240
+ *
1241
+ * // Chaining optional operations
1242
+ * const email = ExperimentalOption.gen(function* () {
1243
+ * const user = yield* findUser(userId);
1244
+ * const profile = yield* ExperimentalOption.fromNullable(user.profile);
1245
+ * const settings = yield* ExperimentalOption.fromNullable(profile.settings);
1246
+ * return settings.email;
1247
+ * });
1248
+ *
1249
+ * // Nested optional access
1250
+ * const city = ExperimentalOption.gen(function* () {
1251
+ * const user = yield* ExperimentalOption.fromNullable(getUser());
1252
+ * const address = yield* ExperimentalOption.fromNullable(user?.address);
1253
+ * const city = yield* ExperimentalOption.fromNullable(address?.city);
1254
+ * return city;
1255
+ * });
1256
+ * ```
1257
+ *
1258
+ * @see {@link genAdapter} for better type inference in complex chains
1259
+ * @see {@link asyncGen} for async operations
1260
+ */
1261
+ static gen(genFn) {
1262
+ const iterator = genFn();
1263
+ let nextArg;
1264
+ let currentResult;
1265
+ let naturalCompleted = false;
1266
+ try {
1267
+ while (true) {
1268
+ const next = iterator.next(nextArg);
1269
+ if (next.done) {
1270
+ naturalCompleted = true;
1271
+ currentResult = ExperimentalOption.Some(next.value);
1272
+ break;
1273
+ }
1274
+ let yielded = next.value;
1275
+ if (require_utils.isCapturedTrace(yielded)) yielded = yielded.value;
1276
+ if (yielded.isNone()) {
1277
+ currentResult = ExperimentalOption.None;
1278
+ break;
1279
+ }
1280
+ nextArg = yielded.unwrap();
1281
+ }
1282
+ } finally {
1283
+ if (!naturalCompleted) require_utils.closeGenerator(iterator, (yielded) => {
1284
+ let effect = yielded;
1285
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1286
+ const option = effect;
1287
+ return option.isNone() ? void 0 : option.unwrap();
1288
+ });
1289
+ }
1290
+ return currentResult;
1291
+ }
1292
+ /**
1293
+ * Generator-based syntax for chaining ExperimentalOption operations with an adapter function.
1294
+ *
1295
+ * Provides better type inference and IDE support compared to {@link gen}.
1296
+ * The `$` adapter wraps ExperimentalOptions and enables clearer type tracking.
1297
+ *
1298
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
1299
+ * instead of recursion to avoid stack overflow on deep chains.
1300
+ *
1301
+ * @template Eff - The effect type (inferred from usage)
1302
+ * @template T - The return type of the generator
1303
+ * @param genFn - A generator function receiving an adapter `$`
1304
+ * @returns `Some<T>` with the generator's return value, or `None`
1305
+ *
1306
+ * @example
1307
+ * ```ts
1308
+ * // Better type inference
1309
+ * const result = ExperimentalOption.genAdapter(function* ($) {
1310
+ * const a = yield* $(ExperimentalOption.Some(1));
1311
+ * const b = yield* $(ExperimentalOption.Some(2));
1312
+ * return a + b;
1313
+ * });
1314
+ * // Some(3)
1315
+ *
1316
+ * // Deep nested access with better type safety
1317
+ * const email = ExperimentalOption.genAdapter(function* ($) {
1318
+ * const user = yield* $(ExperimentalOption.fromNullable(apiResponse?.user));
1319
+ * const profile = yield* $(ExperimentalOption.fromNullable(user?.profile));
1320
+ * const contact = yield* $(ExperimentalOption.fromNullable(profile?.contact));
1321
+ * return contact?.email;
1322
+ * });
1323
+ *
1324
+ * // Complex validation chain
1325
+ * const validConfig = ExperimentalOption.genAdapter(function* ($) {
1326
+ * const raw = yield* $(loadConfig());
1327
+ * const parsed = yield* $(parseConfig(raw));
1328
+ * const validated = yield* $(validateConfig(parsed));
1329
+ * return validated;
1330
+ * });
1331
+ * ```
1332
+ *
1333
+ * @see {@link gen} for simpler chains without adapter
1334
+ * @see {@link asyncGenAdapter} for async operations
1335
+ */
1336
+ static genAdapter(genFn) {
1337
+ const adapter = (option) => new OptionYieldWrap(option);
1338
+ const iterator = genFn(adapter);
1339
+ let nextArg;
1340
+ let currentResult;
1341
+ let naturalCompleted = false;
1342
+ try {
1343
+ while (true) {
1344
+ const next = iterator.next(nextArg);
1345
+ if (next.done) {
1346
+ naturalCompleted = true;
1347
+ currentResult = ExperimentalOption.Some(next.value);
1348
+ break;
1349
+ }
1350
+ let option = next.value.option;
1351
+ if (require_utils.isCapturedTrace(option)) option = option.value;
1352
+ if (option.isNone()) {
1353
+ currentResult = ExperimentalOption.None;
1354
+ break;
1355
+ }
1356
+ nextArg = option.unwrap();
1357
+ }
1358
+ } finally {
1359
+ if (!naturalCompleted) require_utils.closeGenerator(iterator, (yielded) => {
1360
+ let effect = yielded;
1361
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1362
+ effect = effect.option;
1363
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1364
+ const option = effect;
1365
+ return option.isNone() ? void 0 : option.unwrap();
1366
+ });
1367
+ }
1368
+ return currentResult;
1369
+ }
1370
+ /**
1371
+ * Async generator-based syntax for chaining ExperimentalOption operations (simplified, no adapter).
1372
+ *
1373
+ * Use `yield*` with `ExperimentalOption<T>` values directly. For `Promise<ExperimentalOption<T>>`,
1374
+ * await first then `yield*`.
1375
+ *
1376
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
1377
+ * instead of recursion to avoid stack overflow on deep chains.
1378
+ *
1379
+ * @template T - The return type of the generator
1380
+ * @param genFn - An async generator function that yields ExperimentalOptions
1381
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
1382
+ *
1383
+ * @example
1384
+ * ```ts
1385
+ * // Simple async chain
1386
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1387
+ * const a = yield* ExperimentalOption.Some(1);
1388
+ * const b = yield* await asyncOperation(a); // await Promise<ExperimentalOption> first
1389
+ * const c = yield* ExperimentalOption.Some(3);
1390
+ * return a + b + c;
1391
+ * });
1392
+ * // Some(result)
1393
+ *
1394
+ * // None short-circuit in async
1395
+ * const shortCircuit = await ExperimentalOption.asyncGen(async function* () {
1396
+ * const data = yield* await fetchOptionalData();
1397
+ * const parsed = yield* parse(data); // Short-circuits on None
1398
+ * const validated = yield* validate(parsed); // Never executes
1399
+ * return validated;
1400
+ * });
1401
+ * // None
1402
+ *
1403
+ * // Mixed sync/async workflow
1404
+ * const result = await ExperimentalOption.asyncGen(async function* () {
1405
+ * const id = yield* ExperimentalOption.Some(parseInt(input)); // sync
1406
+ * const user = yield* await fetchUser(id); // async
1407
+ * const profile = yield* ExperimentalOption.fromNullable(user?.profile); // sync
1408
+ * const enriched = yield* await enrichProfile(profile); // async
1409
+ * return enriched;
1410
+ * });
1411
+ *
1412
+ * // Complex pipeline with multiple optional steps
1413
+ * async function processLead(email: string): Promise<ExperimentalOption<Lead>> {
1414
+ * return await ExperimentalOption.asyncGen(async function* () {
1415
+ * const normalized = yield* ExperimentalOption.Some(normalizeEmail(email));
1416
+ * const existing = yield* await findExistingLead(normalized);
1417
+ * const enriched = yield* await enrichLeadData(existing);
1418
+ * const validated = yield* validateLead(enriched);
1419
+ * return validated;
1420
+ * });
1421
+ * }
1422
+ * ```
1423
+ *
1424
+ * @see {@link asyncGenAdapter} for better type inference and cleaner syntax
1425
+ * @see {@link gen} for synchronous operations
1426
+ */
1427
+ static async asyncGen(genFn) {
1428
+ const iterator = genFn();
1429
+ let nextArg;
1430
+ let currentResult;
1431
+ let naturalCompleted = false;
1432
+ try {
1433
+ while (true) {
1434
+ const next = await iterator.next(nextArg);
1435
+ if (next.done) {
1436
+ naturalCompleted = true;
1437
+ currentResult = ExperimentalOption.Some(next.value);
1438
+ break;
1439
+ }
1440
+ let option = next.value;
1441
+ if (require_utils.isCapturedTrace(option)) option = option.value;
1442
+ if (option.isNone()) {
1443
+ currentResult = ExperimentalOption.None;
1444
+ break;
1445
+ }
1446
+ nextArg = await option.unwrap();
1447
+ }
1448
+ } finally {
1449
+ if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, async (yielded) => {
1450
+ let effect = await yielded;
1451
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1452
+ const option = effect;
1453
+ return option.isNone() ? void 0 : option.unwrap();
1454
+ });
1455
+ }
1456
+ return currentResult;
1457
+ }
1458
+ /**
1459
+ * Async generator-based syntax for chaining ExperimentalOption operations with an adapter function.
1460
+ *
1461
+ * The `$` adapter handles both sync `ExperimentalOption<T>` and async `Promise<ExperimentalOption<T>>`,
1462
+ * automatically awaiting promises. This provides cleaner syntax for mixed sync/async workflows.
1463
+ *
1464
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
1465
+ * instead of recursion to avoid stack overflow on deep chains.
1466
+ *
1467
+ * @template Eff - The effect type (inferred from usage)
1468
+ * @template T - The return type of the generator
1469
+ * @param genFn - An async generator function receiving an adapter `$`
1470
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
1471
+ *
1472
+ * @example
1473
+ * ```ts
1474
+ * // No need to manually await - adapter handles it
1475
+ * const result = await ExperimentalOption.asyncGenAdapter(async function* ($) {
1476
+ * const a = yield* $(ExperimentalOption.Some(1)); // sync ExperimentalOption
1477
+ * const b = yield* $(asyncOperation(a)); // Promise<ExperimentalOption> - auto-awaited
1478
+ * const c = yield* $(ExperimentalOption.Some(3));
1479
+ * return a + b + c;
1480
+ * });
1481
+ *
1482
+ * // Complex workflow with database and API calls
1483
+ * const userData = await ExperimentalOption.asyncGenAdapter(async function* ($) {
1484
+ * const session = yield* $(getSession()); // Promise<ExperimentalOption<Session>>
1485
+ * const userId = yield* $(ExperimentalOption.fromNullable(session?.userId));
1486
+ * const profile = yield* $(await fetchProfile(userId)); // Promise<ExperimentalOption<Profile>>
1487
+ * const preferences = yield* $(ExperimentalOption.fromNullable(profile?.preferences));
1488
+ * const enriched = yield* $(await enrichPreferences(preferences));
1489
+ * return enriched;
1490
+ * });
1491
+ *
1492
+ * // Real-world: Optional data enrichment
1493
+ * async function getProductDetails(productId: string): Promise<ExperimentalOption<ProductDetails>> {
1494
+ * return await ExperimentalOption.asyncGenAdapter(async function* ($) {
1495
+ * const product = yield* $(await fetchProduct(productId));
1496
+ * const pricing = yield* $(await fetchPricing(product.sku));
1497
+ * const inventory = yield* $(await checkInventory(product.id));
1498
+ * const reviews = yield* $(await fetchReviews(product.id));
1499
+ * const related = yield* $(await fetchRelatedProducts(product.category));
1500
+ *
1501
+ * return { product, pricing, inventory, reviews, related };
1502
+ * });
1503
+ * }
1504
+ * ```
1505
+ *
1506
+ * @see {@link asyncGen} when you want explicit `await` for async operations
1507
+ * @see {@link genAdapter} for synchronous operations
1508
+ */
1509
+ static async asyncGenAdapter(genFn) {
1510
+ const adapter = (option) => new AsyncOptionYieldWrap(option);
1511
+ const iterator = genFn(adapter);
1512
+ let nextArg;
1513
+ let currentResult;
1514
+ let naturalCompleted = false;
1515
+ try {
1516
+ while (true) {
1517
+ const next = await iterator.next(nextArg);
1518
+ if (next.done) {
1519
+ naturalCompleted = true;
1520
+ currentResult = ExperimentalOption.Some(next.value);
1521
+ break;
1522
+ }
1523
+ let option = await next.value.option;
1524
+ if (require_utils.isCapturedTrace(option)) option = option.value;
1525
+ if (option.isNone()) {
1526
+ currentResult = ExperimentalOption.None;
1527
+ break;
1528
+ }
1529
+ nextArg = await option.unwrap();
1530
+ }
1531
+ } finally {
1532
+ if (!naturalCompleted) await require_utils.closeAsyncGenerator(iterator, async (yielded) => {
1533
+ let effect = await yielded;
1534
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1535
+ effect = await effect.option;
1536
+ if (require_utils.isCapturedTrace(effect)) effect = effect.value;
1537
+ const option = effect;
1538
+ return option.isNone() ? void 0 : option.unwrap();
1539
+ });
1540
+ }
1541
+ return currentResult;
1542
+ }
1543
+ };
1544
+
1545
+ exports.ExperimentalOption = ExperimentalOption;
1546
+ exports.UnwrappedNone = UnwrappedNone;
1547
+ //# sourceMappingURL=option-experimental.cjs.map