@lee-zg/melange 1.0.0

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 (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +256 -0
  3. package/dist/chunk-2PXWQDZC.js +659 -0
  4. package/dist/chunk-2PXWQDZC.js.map +1 -0
  5. package/dist/chunk-352XNR3C.js +716 -0
  6. package/dist/chunk-352XNR3C.js.map +1 -0
  7. package/dist/chunk-7QVYU63E.js +6 -0
  8. package/dist/chunk-7QVYU63E.js.map +1 -0
  9. package/dist/chunk-ALBD5XC5.js +285 -0
  10. package/dist/chunk-ALBD5XC5.js.map +1 -0
  11. package/dist/chunk-O7K662J5.cjs +842 -0
  12. package/dist/chunk-O7K662J5.cjs.map +1 -0
  13. package/dist/chunk-PK6SKIKE.cjs +8 -0
  14. package/dist/chunk-PK6SKIKE.cjs.map +1 -0
  15. package/dist/chunk-Q73NOVWX.cjs +789 -0
  16. package/dist/chunk-Q73NOVWX.cjs.map +1 -0
  17. package/dist/chunk-Q7XG6YN6.cjs +682 -0
  18. package/dist/chunk-Q7XG6YN6.cjs.map +1 -0
  19. package/dist/chunk-YGMBCZJQ.js +833 -0
  20. package/dist/chunk-YGMBCZJQ.js.map +1 -0
  21. package/dist/chunk-ZT6HVG4G.cjs +330 -0
  22. package/dist/chunk-ZT6HVG4G.cjs.map +1 -0
  23. package/dist/core/index.cjs +97 -0
  24. package/dist/core/index.cjs.map +1 -0
  25. package/dist/core/index.d.cts +718 -0
  26. package/dist/core/index.d.ts +718 -0
  27. package/dist/core/index.js +4 -0
  28. package/dist/core/index.js.map +1 -0
  29. package/dist/fp/index.cjs +185 -0
  30. package/dist/fp/index.cjs.map +1 -0
  31. package/dist/fp/index.d.cts +913 -0
  32. package/dist/fp/index.d.ts +913 -0
  33. package/dist/fp/index.js +4 -0
  34. package/dist/fp/index.js.map +1 -0
  35. package/dist/index.cjs +608 -0
  36. package/dist/index.cjs.map +1 -0
  37. package/dist/index.d.cts +39 -0
  38. package/dist/index.d.ts +39 -0
  39. package/dist/index.js +33 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/plugins/index.cjs +41 -0
  42. package/dist/plugins/index.cjs.map +1 -0
  43. package/dist/plugins/index.d.cts +643 -0
  44. package/dist/plugins/index.d.ts +643 -0
  45. package/dist/plugins/index.js +4 -0
  46. package/dist/plugins/index.js.map +1 -0
  47. package/dist/types-BtOUCLB-.d.cts +293 -0
  48. package/dist/types-BtOUCLB-.d.ts +293 -0
  49. package/dist/utils/index.cjs +297 -0
  50. package/dist/utils/index.cjs.map +1 -0
  51. package/dist/utils/index.d.cts +1179 -0
  52. package/dist/utils/index.d.ts +1179 -0
  53. package/dist/utils/index.js +4 -0
  54. package/dist/utils/index.js.map +1 -0
  55. package/package.json +132 -0
@@ -0,0 +1,913 @@
1
+ import { U as UnaryFunction, d as Ok, E as Err, e as Result, S as Some, f as None, g as Option, A as AnyFunction } from '../types-BtOUCLB-.js';
2
+
3
+ /**
4
+ * @fileoverview 函数组合工具
5
+ * @module melange/fp/compose
6
+ * @description 提供组合和连接函数的工具,
7
+ * 实现无点风格编程和函数链式调用。
8
+ */
9
+
10
+ /**
11
+ * 从右到左组合多个函数。
12
+ * 最右边的函数可以接受多个参数,其他所有函数必须是一元的。
13
+ *
14
+ * @description
15
+ * 函数组合是函数式编程中的基本概念。
16
+ * `compose(f, g, h)(x)` 等价于 `f(g(h(x)))`。
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const addOne = (x: number) => x + 1;
21
+ * const double = (x: number) => x * 2;
22
+ * const square = (x: number) => x * x;
23
+ *
24
+ * const composed = compose(square, double, addOne);
25
+ * composed(2); // ((2 + 1) * 2)² = 36
26
+ * ```
27
+ *
28
+ * @param fns - 要组合的函数
29
+ * @returns 一个新函数,从右到左应用所有函数
30
+ */
31
+ declare function compose<A, B>(fn1: UnaryFunction<A, B>): UnaryFunction<A, B>;
32
+ declare function compose<A, B, C>(fn2: UnaryFunction<B, C>, fn1: UnaryFunction<A, B>): UnaryFunction<A, C>;
33
+ declare function compose<A, B, C, D>(fn3: UnaryFunction<C, D>, fn2: UnaryFunction<B, C>, fn1: UnaryFunction<A, B>): UnaryFunction<A, D>;
34
+ declare function compose<A, B, C, D, E>(fn4: UnaryFunction<D, E>, fn3: UnaryFunction<C, D>, fn2: UnaryFunction<B, C>, fn1: UnaryFunction<A, B>): UnaryFunction<A, E>;
35
+ declare function compose<A, B, C, D, E, F>(fn5: UnaryFunction<E, F>, fn4: UnaryFunction<D, E>, fn3: UnaryFunction<C, D>, fn2: UnaryFunction<B, C>, fn1: UnaryFunction<A, B>): UnaryFunction<A, F>;
36
+ declare function compose(...fns: UnaryFunction<unknown, unknown>[]): UnaryFunction<unknown, unknown>;
37
+ /**
38
+ * 从左到右连接多个函数。
39
+ * 第一个函数可以接受多个参数,其他所有函数必须是一元的。
40
+ *
41
+ * @description
42
+ * Pipe 是 compose 的反向操作,从左到右应用函数。
43
+ * `pipe(f, g, h)(x)` 等价于 `h(g(f(x)))`。
44
+ * 这通常更易读,因为它遵循自然的阅读顺序。
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const addOne = (x: number) => x + 1;
49
+ * const double = (x: number) => x * 2;
50
+ * const square = (x: number) => x * x;
51
+ *
52
+ * const piped = pipe(addOne, double, square);
53
+ * piped(2); // ((2 + 1) * 2)² = 36
54
+ * ```
55
+ *
56
+ * @param fns - 要连接的函数
57
+ * @returns 一个新函数,从左到右应用所有函数
58
+ */
59
+ declare function pipe<A, B>(fn1: UnaryFunction<A, B>): UnaryFunction<A, B>;
60
+ declare function pipe<A, B, C>(fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>): UnaryFunction<A, C>;
61
+ declare function pipe<A, B, C, D>(fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>): UnaryFunction<A, D>;
62
+ declare function pipe<A, B, C, D, E>(fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>, fn4: UnaryFunction<D, E>): UnaryFunction<A, E>;
63
+ declare function pipe<A, B, C, D, E, F>(fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>, fn4: UnaryFunction<D, E>, fn5: UnaryFunction<E, F>): UnaryFunction<A, F>;
64
+ declare function pipe(...fns: UnaryFunction<unknown, unknown>[]): UnaryFunction<unknown, unknown>;
65
+ /**
66
+ * 创建函数流,类似于 pipe 但立即返回最终结果。
67
+ * 适用于通过一系列函数转换值。
68
+ *
69
+ * @description
70
+ * Flow 类似于 pipe,但不是返回函数,
71
+ * 而是立即将值通过所有函数应用。
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const result = flow(
76
+ * 5,
77
+ * x => x + 1,
78
+ * x => x * 2,
79
+ * x => x.toString()
80
+ * );
81
+ * // result = "12"
82
+ * ```
83
+ *
84
+ * @param value - 初始值
85
+ * @param fns - 要应用的函数
86
+ * @returns 将所有函数应用于值的结果
87
+ */
88
+ declare function flow<A>(value: A): A;
89
+ declare function flow<A, B>(value: A, fn1: UnaryFunction<A, B>): B;
90
+ declare function flow<A, B, C>(value: A, fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>): C;
91
+ declare function flow<A, B, C, D>(value: A, fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>): D;
92
+ declare function flow<A, B, C, D, E>(value: A, fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>, fn4: UnaryFunction<D, E>): E;
93
+ declare function flow<A, B, C, D, E, F>(value: A, fn1: UnaryFunction<A, B>, fn2: UnaryFunction<B, C>, fn3: UnaryFunction<C, D>, fn4: UnaryFunction<D, E>, fn5: UnaryFunction<E, F>): F;
94
+
95
+ /**
96
+ * @fileoverview 柯里化和部分应用工具
97
+ * @module melange/fp/curry
98
+ * @description 提供柯里化函数和部分应用的工具,
99
+ * 实现更灵活的函数组合和重用。
100
+ */
101
+ /**
102
+ * 柯里化二元函数。
103
+ * 将接受两个参数的函数转换为接受一个参数并返回接受第二个参数的函数。
104
+ *
105
+ * @description
106
+ * 柯里化以数学家 Haskell Curry 命名。它将具有多个参数的函数转换为一系列函数,每个函数接受一个参数。
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * const add = (a: number, b: number) => a + b;
111
+ * const curriedAdd = curry(add);
112
+ *
113
+ * curriedAdd(1)(2); // 3
114
+ * const addFive = curriedAdd(5);
115
+ * addFive(10); // 15
116
+ * ```
117
+ *
118
+ * @template A - 第一个参数的类型
119
+ * @template B - 第二个参数的类型
120
+ * @template R - 返回类型
121
+ * @param fn - 要柯里化的二元函数
122
+ * @returns 函数的柯里化版本
123
+ */
124
+ declare function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R;
125
+ /**
126
+ * 柯里化三元函数。
127
+ *
128
+ * @template A - 第一个参数的类型
129
+ * @template B - 第二个参数的类型
130
+ * @template C - 第三个参数的类型
131
+ * @template R - 返回类型
132
+ * @param fn - 要柯里化的三元函数
133
+ * @returns 函数的柯里化版本
134
+ */
135
+ declare function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (a: A) => (b: B) => (c: C) => R;
136
+ /**
137
+ * 柯里化四元函数。
138
+ *
139
+ * @template A - 第一个参数的类型
140
+ * @template B - 第二个参数的类型
141
+ * @template C - 第三个参数的类型
142
+ * @template D - 第四个参数的类型
143
+ * @template R - 返回类型
144
+ * @param fn - 要柯里化的四元函数
145
+ * @returns 函数的柯里化版本
146
+ */
147
+ declare function curry<A, B, C, D, R>(fn: (a: A, b: B, c: C, d: D) => R): (a: A) => (b: B) => (c: C) => (d: D) => R;
148
+ /**
149
+ * 反柯里化函数。
150
+ * 将一元函数序列转换回接受多个参数的单个函数。
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const curriedAdd = (a: number) => (b: number) => a + b;
155
+ * const add = uncurry(curriedAdd);
156
+ *
157
+ * add(1, 2); // 3
158
+ * ```
159
+ *
160
+ * @template A - 第一个参数的类型
161
+ * @template B - 第二个参数的类型
162
+ * @template R - 返回类型
163
+ * @param fn - 要反柯里化的函数
164
+ * @returns 函数的反柯里化版本
165
+ */
166
+ declare function uncurry<A, B, R>(fn: (a: A) => (b: B) => R): (a: A, b: B) => R;
167
+ declare function uncurry<A, B, C, R>(fn: (a: A) => (b: B) => (c: C) => R): (a: A, b: B, c: C) => R;
168
+ /**
169
+ * 从左侧部分应用参数到函数。
170
+ * 返回一个接受剩余参数的新函数。
171
+ *
172
+ * @description
173
+ * 部分应用不同于柯里化。柯里化将函数转换为一元函数序列,
174
+ * 而部分应用固定一些参数并返回接受剩余参数的函数。
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const greet = (greeting: string, name: string, punctuation: string) =>
179
+ * `${greeting}, ${name}${punctuation}`;
180
+ *
181
+ * const sayHello = partial(greet, 'Hello');
182
+ * sayHello('World', '!'); // "Hello, World!"
183
+ *
184
+ * const sayHelloWorld = partial(greet, 'Hello', 'World');
185
+ * sayHelloWorld('!'); // "Hello, World!"
186
+ * ```
187
+ *
188
+ * @template T - 部分参数的元组类型
189
+ * @template R - 剩余参数的元组类型
190
+ * @template Result - 返回类型
191
+ * @param fn - 要部分应用的函数
192
+ * @param partialArgs - 要固定的参数
193
+ * @returns 接受剩余参数的新函数
194
+ */
195
+ declare function partial<T extends unknown[], R extends unknown[], Result>(fn: (...args: [...T, ...R]) => Result, ...partialArgs: T): (...remainingArgs: R) => Result;
196
+ /**
197
+ * 从右侧部分应用参数到函数。
198
+ * 返回一个接受剩余参数的新函数。
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const greet = (greeting: string, name: string, punctuation: string) =>
203
+ * `${greeting}, ${name}${punctuation}`;
204
+ *
205
+ * const greetExcitedly = partialRight(greet, '!');
206
+ * greetExcitedly('Hello', 'World'); // "Hello, World!"
207
+ * ```
208
+ *
209
+ * @template L - 前导参数的元组类型
210
+ * @template T - 部分参数的元组类型
211
+ * @template Result - 返回类型
212
+ * @param fn - 要部分应用的函数
213
+ * @param partialArgs - 从右侧固定的参数
214
+ * @returns 接受前导参数的新函数
215
+ */
216
+ declare function partialRight<L extends unknown[], T extends unknown[], Result>(fn: (...args: [...L, ...T]) => Result, ...partialArgs: T): (...leadingArgs: L) => Result;
217
+
218
+ /**
219
+ * @fileoverview 用于函数式错误处理的 Result 类型
220
+ * @module melange/fp/result
221
+ * @description 提供 Result 类型和工具,用于处理可能成功或失败的操作,
222
+ * 而无需抛出异常。
223
+ */
224
+
225
+ /**
226
+ * 创建包含给定值的成功 Result。
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const result = ok(42);
231
+ * // { _tag: 'Ok', value: 42 }
232
+ * ```
233
+ *
234
+ * @template T - 成功值的类型
235
+ * @param value - 成功值
236
+ * @returns 包含该值的 Ok Result
237
+ */
238
+ declare function ok<T>(value: T): Ok<T>;
239
+ /**
240
+ * 创建包含给定错误的失败 Result。
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * const result = err(new Error('出错了'));
245
+ * // { _tag: 'Err', error: Error('出错了') }
246
+ * ```
247
+ *
248
+ * @template E - 错误的类型
249
+ * @param error - 错误值
250
+ * @returns 包含该错误的 Err Result
251
+ */
252
+ declare function err<E>(error: E): Err<E>;
253
+ /**
254
+ * 类型守卫,用于检查 Result 是否为 Ok。
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const result = ok(42);
259
+ * if (isOk(result)) {
260
+ * console.log(result.value); // 42
261
+ * }
262
+ * ```
263
+ *
264
+ * @template T - 成功类型
265
+ * @template E - 错误类型
266
+ * @param result - 要检查的 Result
267
+ * @returns 如果 Result 是 Ok 则返回 true
268
+ */
269
+ declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
270
+ /**
271
+ * 类型守卫,用于检查 Result 是否为 Err。
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * const result = err('error');
276
+ * if (isErr(result)) {
277
+ * console.log(result.error); // 'error'
278
+ * }
279
+ * ```
280
+ *
281
+ * @template T - 成功类型
282
+ * @template E - 错误类型
283
+ * @param result - 要检查的 Result
284
+ * @returns 如果 Result 是 Err 则返回 true
285
+ */
286
+ declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
287
+ /**
288
+ * 在 Result 的成功值上映射函数。
289
+ * 如果 Result 是 Err,则错误将原样传递。
290
+ *
291
+ * @example
292
+ * ```typescript
293
+ * const result = ok(5);
294
+ * const doubled = mapResult(result, x => x * 2);
295
+ * // { _tag: 'Ok', value: 10 }
296
+ *
297
+ * const error = err('error');
298
+ * const still = mapResult(error, x => x * 2);
299
+ * // { _tag: 'Err', error: 'error' }
300
+ * ```
301
+ *
302
+ * @template T - 原始成功类型
303
+ * @template U - 映射后的成功类型
304
+ * @template E - 错误类型
305
+ * @param result - 要映射的 Result
306
+ * @param fn - 要应用于成功值的函数
307
+ * @returns 带有映射值的新 Result
308
+ */
309
+ declare function mapResult<T, U, E>(result: Result<T, E>, fn: UnaryFunction<T, U>): Result<U, E>;
310
+ /**
311
+ * 在 Result 的成功值上映射返回 Result 的函数。
312
+ * 在其他库中这也被称为 `chain` 或 `bind`。
313
+ *
314
+ * @example
315
+ * ```typescript
316
+ * const safeDivide = (a: number, b: number): Result<number, string> =>
317
+ * b === 0 ? err('除零错误') : ok(a / b);
318
+ *
319
+ * const result = ok(10);
320
+ * const divided = flatMapResult(result, x => safeDivide(x, 2));
321
+ * // { _tag: 'Ok', value: 5 }
322
+ *
323
+ * const divideByZero = flatMapResult(result, x => safeDivide(x, 0));
324
+ * // { _tag: 'Err', error: '除零错误' }
325
+ * ```
326
+ *
327
+ * @template T - 原始成功类型
328
+ * @template U - 映射后的成功类型
329
+ * @template E - 错误类型
330
+ * @param result - 要 flatMap 的 Result
331
+ * @param fn - 应用的返回 Result 的函数
332
+ * @returns 展平后的 Result
333
+ */
334
+ declare function flatMapResult<T, U, E>(result: Result<T, E>, fn: UnaryFunction<T, Result<U, E>>): Result<U, E>;
335
+ /**
336
+ * 解包 Result,返回成功值或如果为 Err 则返回默认值。
337
+ *
338
+ * @example
339
+ * ```typescript
340
+ * const success = ok(42);
341
+ * unwrapOr(success, 0); // 42
342
+ *
343
+ * const failure = err('error');
344
+ * unwrapOr(failure, 0); // 0
345
+ * ```
346
+ *
347
+ * @template T - 成功类型
348
+ * @template E - 错误类型
349
+ * @param result - 要解包的 Result
350
+ * @param defaultValue - 如果为 Err 的默认值
351
+ * @returns 成功值或默认值
352
+ */
353
+ declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
354
+ /**
355
+ * 解包 Result,返回成功值或如果为 Err 则调用函数。
356
+ *
357
+ * @example
358
+ * ```typescript
359
+ * const success = ok(42);
360
+ * unwrapOrElse(success, () => 0); // 42
361
+ *
362
+ * const failure = err('error');
363
+ * unwrapOrElse(failure, () => 0); // 0
364
+ * ```
365
+ *
366
+ * @template T - 成功类型
367
+ * @template E - 错误类型
368
+ * @param result - 要解包的 Result
369
+ * @param fn - 如果为 Err 要调用的函数
370
+ * @returns 成功值或函数的结果
371
+ */
372
+ declare function unwrapOrElse<T, E>(result: Result<T, E>, fn: UnaryFunction<E, T>): T;
373
+ /**
374
+ * 将 Result 与成功和错误处理程序匹配。
375
+ *
376
+ * @example
377
+ * ```typescript
378
+ * const result = ok(42);
379
+ * const message = match(result, {
380
+ * ok: value => `得到 ${value}`,
381
+ * err: error => `错误: ${error}`
382
+ * });
383
+ * // "得到 42"
384
+ * ```
385
+ *
386
+ * @template T - 成功类型
387
+ * @template E - 错误类型
388
+ * @template R - 返回类型
389
+ * @param result - 要匹配的 Result
390
+ * @param handlers - 带有 ok 和 err 处理程序的对象
391
+ * @returns 匹配处理程序的结果
392
+ */
393
+ declare function matchResult<T, E, R>(result: Result<T, E>, handlers: {
394
+ ok: UnaryFunction<T, R>;
395
+ err: UnaryFunction<E, R>;
396
+ }): R;
397
+ /**
398
+ * 尝试执行函数并将结果包装在 Result 类型中。
399
+ *
400
+ * @example
401
+ * ```typescript
402
+ * const result = tryCatch(() => JSON.parse('{"a": 1}'));
403
+ * // { _tag: 'Ok', value: { a: 1 } }
404
+ *
405
+ * const error = tryCatch(() => JSON.parse('invalid'));
406
+ * // { _tag: 'Err', error: SyntaxError(...) }
407
+ * ```
408
+ *
409
+ * @template T - 函数的返回类型
410
+ * @param fn - 要执行的函数
411
+ * @returns 包含返回值或错误的 Result
412
+ */
413
+ declare function tryCatch<T>(fn: () => T): Result<T, unknown>;
414
+ /**
415
+ * 尝试执行异步函数并将结果包装在 Result 类型中。
416
+ *
417
+ * @example
418
+ * ```typescript
419
+ * const result = await tryCatchAsync(async () => {
420
+ * const response = await fetch('/api/data');
421
+ * return response.json();
422
+ * });
423
+ * ```
424
+ *
425
+ * @template T - 异步函数的返回类型
426
+ * @param fn - 要执行的异步函数
427
+ * @returns 包含返回值或错误的 Result 的 Promise
428
+ */
429
+ declare function tryCatchAsync<T>(fn: () => Promise<T>): Promise<Result<T, unknown>>;
430
+
431
+ /**
432
+ * @fileoverview 用于可空值处理的 Option 类型
433
+ * @module melange/fp/option
434
+ * @description 提供 Option 类型和工具,用于处理可能存在或不存在的值,
435
+ * 而不直接使用 null/undefined。
436
+ */
437
+
438
+ /**
439
+ * 创建包含给定值的 Some Option。
440
+ *
441
+ * @example
442
+ * ```typescript
443
+ * const option = some(42);
444
+ * // { _tag: 'Some', value: 42 }
445
+ * ```
446
+ *
447
+ * @template T - 值的类型
448
+ * @param value - 要包装的值
449
+ * @returns 包含该值的 Some Option
450
+ */
451
+ declare function some<T>(value: T): Some<T>;
452
+ /**
453
+ * 创建表示缺失值的 None Option。
454
+ *
455
+ * @example
456
+ * ```typescript
457
+ * const option = none();
458
+ * // { _tag: 'None' }
459
+ * ```
460
+ *
461
+ * @returns None Option
462
+ */
463
+ declare function none(): None;
464
+ /**
465
+ * 类型守卫,用于检查 Option 是否为 Some。
466
+ *
467
+ * @example
468
+ * ```typescript
469
+ * const option = some(42);
470
+ * if (isSome(option)) {
471
+ * console.log(option.value); // 42
472
+ * }
473
+ * ```
474
+ *
475
+ * @template T - 值的类型
476
+ * @param option - 要检查的 Option
477
+ * @returns 如果 Option 是 Some 则返回 true
478
+ */
479
+ declare function isSome<T>(option: Option<T>): option is Some<T>;
480
+ /**
481
+ * 类型守卫,用于检查 Option 是否为 None。
482
+ *
483
+ * @example
484
+ * ```typescript
485
+ * const option = none();
486
+ * if (isNone(option)) {
487
+ * console.log('无值');
488
+ * }
489
+ * ```
490
+ *
491
+ * @template T - 值的类型
492
+ * @param option - 要检查的 Option
493
+ * @returns 如果 Option 是 None 则返回 true
494
+ */
495
+ declare function isNone<T>(option: Option<T>): option is None;
496
+ /**
497
+ * 从可空值创建 Option。
498
+ * 如果值为 null 或 undefined,返回 None;否则返回 Some。
499
+ *
500
+ * @example
501
+ * ```typescript
502
+ * fromNullable(42); // Some(42)
503
+ * fromNullable(null); // None
504
+ * fromNullable(undefined);// None
505
+ * ```
506
+ *
507
+ * @template T - 值的类型
508
+ * @param value - 可空值
509
+ * @returns 包装该值的 Option
510
+ */
511
+ declare function fromNullable<T>(value: T | null | undefined): Option<T>;
512
+ /**
513
+ * 在 Option 的值上映射函数。
514
+ * 如果 Option 是 None,则原样返回 None。
515
+ *
516
+ * @example
517
+ * ```typescript
518
+ * const option = some(5);
519
+ * const doubled = mapOption(option, x => x * 2);
520
+ * // Some(10)
521
+ *
522
+ * const empty = none();
523
+ * const still = mapOption(empty, x => x * 2);
524
+ * // None
525
+ * ```
526
+ *
527
+ * @template T - 原始值类型
528
+ * @template U - 映射后的值类型
529
+ * @param option - 要映射的 Option
530
+ * @param fn - 要应用于值的函数
531
+ * @returns 带有映射值的新 Option
532
+ */
533
+ declare function mapOption<T, U>(option: Option<T>, fn: UnaryFunction<T, U>): Option<U>;
534
+ /**
535
+ * 在 Option 的值上映射返回 Option 的函数。
536
+ * 在其他库中这也被称为 `chain` 或 `bind`。
537
+ *
538
+ * @example
539
+ * ```typescript
540
+ * const safeDivide = (n: number): Option<number> =>
541
+ * n === 0 ? none() : some(100 / n);
542
+ *
543
+ * const option = some(10);
544
+ * const divided = flatMapOption(option, safeDivide);
545
+ * // Some(10)
546
+ *
547
+ * const zero = some(0);
548
+ * const divideByZero = flatMapOption(zero, safeDivide);
549
+ * // None
550
+ * ```
551
+ *
552
+ * @template T - 原始值类型
553
+ * @template U - 映射后的值类型
554
+ * @param option - 要 flatMap 的 Option
555
+ * @param fn - 应用的返回 Option 的函数
556
+ * @returns 展平后的 Option
557
+ */
558
+ declare function flatMapOption<T, U>(option: Option<T>, fn: UnaryFunction<T, Option<U>>): Option<U>;
559
+ /**
560
+ * 从 Option 获取值,如果为 None 则返回默认值。
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * const present = some(42);
565
+ * getOrElse(present, 0); // 42
566
+ *
567
+ * const absent = none();
568
+ * getOrElse(absent, 0); // 0
569
+ * ```
570
+ *
571
+ * @template T - 值的类型
572
+ * @param option - 要解包的 Option
573
+ * @param defaultValue - 如果为 None 的默认值
574
+ * @returns 值或默认值
575
+ */
576
+ declare function getOrElse<T>(option: Option<T>, defaultValue: T): T;
577
+ /**
578
+ * 从 Option 获取值,如果为 None 则调用函数。
579
+ *
580
+ * @example
581
+ * ```typescript
582
+ * const present = some(42);
583
+ * getOrElseL(present, () => 0); // 42
584
+ *
585
+ * const absent = none();
586
+ * getOrElseL(absent, () => computeDefault()); // computeDefault() 的结果
587
+ * ```
588
+ *
589
+ * @template T - 值的类型
590
+ * @param option - 要解包的 Option
591
+ * @param fn - 如果为 None 要调用的函数
592
+ * @returns 值或函数的结果
593
+ */
594
+ declare function getOrElseL<T>(option: Option<T>, fn: () => T): T;
595
+ /**
596
+ * 将 Option 与 Some 和 None 处理程序匹配。
597
+ *
598
+ * @example
599
+ * ```typescript
600
+ * const option = some(42);
601
+ * const message = matchOption(option, {
602
+ * some: value => `得到 ${value}`,
603
+ * none: () => '无'
604
+ * });
605
+ * // "得到 42"
606
+ * ```
607
+ *
608
+ * @template T - 值的类型
609
+ * @template R - 返回类型
610
+ * @param option - 要匹配的 Option
611
+ * @param handlers - 带有 some 和 none 处理程序的对象
612
+ * @returns 匹配处理程序的结果
613
+ */
614
+ declare function matchOption<T, R>(option: Option<T>, handlers: {
615
+ some: UnaryFunction<T, R>;
616
+ none: () => R;
617
+ }): R;
618
+ /**
619
+ * 将 Option 转换为可空值。
620
+ *
621
+ * @example
622
+ * ```typescript
623
+ * toNullable(some(42)); // 42
624
+ * toNullable(none()); // null
625
+ * ```
626
+ *
627
+ * @template T - 值的类型
628
+ * @param option - 要转换的 Option
629
+ * @returns 值或 null
630
+ */
631
+ declare function toNullable<T>(option: Option<T>): T | null;
632
+ /**
633
+ * 基于谓词过滤 Option。
634
+ * 如果 Option 是 Some 且谓词返回 false,则返回 None。
635
+ *
636
+ * @example
637
+ * ```typescript
638
+ * const option = some(10);
639
+ * filter(option, x => x > 5); // Some(10)
640
+ * filter(option, x => x > 15); // None
641
+ * ```
642
+ *
643
+ * @template T - 值的类型
644
+ * @param option - 要过滤的 Option
645
+ * @param predicate - 谓词函数
646
+ * @returns 如果谓词通过则返回 Option,否则返回 None
647
+ */
648
+ declare function filterOption<T>(option: Option<T>, predicate: (value: T) => boolean): Option<T>;
649
+ /**
650
+ * 返回第一个为 Some 的 Option,如果都是 None 则返回 None。
651
+ *
652
+ * @example
653
+ * ```typescript
654
+ * alt(none(), some(42)); // Some(42)
655
+ * alt(some(1), some(2)); // Some(1)
656
+ * alt(none(), none()); // None
657
+ * ```
658
+ *
659
+ * @template T - 值的类型
660
+ * @param first - 第一个 Option
661
+ * @param second - 第二个 Option
662
+ * @returns 第一个 Some Option,或 None
663
+ */
664
+ declare function alt<T>(first: Option<T>, second: Option<T>): Option<T>;
665
+
666
+ /**
667
+ * @fileoverview 高阶函数和工具
668
+ * @module melange/fp/hof
669
+ * @description 提供高阶函数用于记忆化、函数控制
670
+ * 和其他函数式编程工具。
671
+ */
672
+
673
+ /**
674
+ * 创建函数的记忆化版本。
675
+ * 记忆化函数根据提供的参数缓存结果。
676
+ *
677
+ * @description
678
+ * 记忆化是一种优化技术,存储昂贵函数调用的结果,
679
+ * 当相同输入再次出现时返回缓存结果。
680
+ *
681
+ * @example
682
+ * ```typescript
683
+ * const expensiveCalculation = (n: number) => {
684
+ * console.log('计算中...');
685
+ * return n * n;
686
+ * };
687
+ *
688
+ * const memoized = memoize(expensiveCalculation);
689
+ * memoized(5); // 记录 '计算中...', 返回 25
690
+ * memoized(5); // 从缓存返回 25, 无记录
691
+ * memoized(3); // 记录 '计算中...', 返回 9
692
+ * ```
693
+ *
694
+ * @template T - 函数类型
695
+ * @param fn - 要记忆化的函数
696
+ * @param keyFn - 生成缓存键的可选函数
697
+ * @returns 函数的记忆化版本
698
+ */
699
+ declare function memoize<T extends AnyFunction>(fn: T, keyFn?: (...args: Parameters<T>) => string): T;
700
+ /**
701
+ * 创建只能调用一次的函数。
702
+ * 后续调用返回第一次调用的结果。
703
+ *
704
+ * @example
705
+ * ```typescript
706
+ * const initialize = once(() => {
707
+ * console.log('初始化中...');
708
+ * return { initialized: true };
709
+ * });
710
+ *
711
+ * initialize(); // 记录 '初始化中...', 返回 { initialized: true }
712
+ * initialize(); // 返回 { initialized: true }, 无记录
713
+ * ```
714
+ *
715
+ * @template T - 函数类型
716
+ * @param fn - 要包装的函数
717
+ * @returns 只能调用一次的函数
718
+ */
719
+ declare function once<T extends AnyFunction>(fn: T): T;
720
+ /**
721
+ * 创建调用原始函数然后执行副作用的函数,返回原始结果。
722
+ *
723
+ * @description
724
+ * Tap 适用于在管道中调试或记录日志
725
+ * 而不影响数据流。
726
+ *
727
+ * @example
728
+ * ```typescript
729
+ * const addOne = (x: number) => x + 1;
730
+ * const logValue = tap((x: number) => console.log('值:', x));
731
+ *
732
+ * pipe(
733
+ * 5,
734
+ * addOne,
735
+ * logValue, // 记录 '值: 6', 返回 6
736
+ * x => x * 2
737
+ * ); // 返回 12
738
+ * ```
739
+ *
740
+ * @template T - 值类型
741
+ * @param effect - 要执行的副作用函数
742
+ * @returns 执行副作用并返回输入的函数
743
+ */
744
+ declare function tap<T>(effect: (value: T) => void): (value: T) => T;
745
+ /**
746
+ * 返回传入的值而不改变。
747
+ * 恒等函数作为默认转换器很有用。
748
+ *
749
+ * @example
750
+ * ```typescript
751
+ * identity(42); // 42
752
+ * identity('hello'); // 'hello'
753
+ * [1, 2, 3].map(identity); // [1, 2, 3]
754
+ * ```
755
+ *
756
+ * @template T - 值类型
757
+ * @param value - 要返回的值
758
+ * @returns 相同的值
759
+ */
760
+ declare function identity<T>(value: T): T;
761
+ /**
762
+ * 创建总是返回相同值的函数。
763
+ * 适用于创建占位符函数。
764
+ *
765
+ * @example
766
+ * ```typescript
767
+ * const alwaysFive = constant(5);
768
+ * alwaysFive(); // 5
769
+ * alwaysFive(100); // 5
770
+ *
771
+ * [1, 2, 3].map(constant('x')); // ['x', 'x', 'x']
772
+ * ```
773
+ *
774
+ * @template T - 值类型
775
+ * @param value - 总是返回的值
776
+ * @returns 总是返回值的函数
777
+ */
778
+ declare function constant<T>(value: T): () => T;
779
+ /**
780
+ * 不执行任何操作并返回 undefined 的函数。
781
+ * 适合作为默认回调或占位符。
782
+ *
783
+ * @example
784
+ * ```typescript
785
+ * const callback = maybeCallback || noop;
786
+ * callback();
787
+ * ```
788
+ */
789
+ declare function noop(): void;
790
+ /**
791
+ * 否定谓词函数。
792
+ *
793
+ * @example
794
+ * ```typescript
795
+ * const isEven = (n: number) => n % 2 === 0;
796
+ * const isOdd = not(isEven);
797
+ *
798
+ * isOdd(3); // true
799
+ * isOdd(4); // false
800
+ * ```
801
+ *
802
+ * @template T - 参数类型
803
+ * @param predicate - 要否定的谓词
804
+ * @returns 否定的谓词
805
+ */
806
+ declare function not<T>(predicate: (value: T) => boolean): (value: T) => boolean;
807
+ /**
808
+ * 创建当所有谓词都返回 true 时返回 true 的函数。
809
+ *
810
+ * @example
811
+ * ```typescript
812
+ * const isPositive = (n: number) => n > 0;
813
+ * const isEven = (n: number) => n % 2 === 0;
814
+ * const isPositiveEven = allPass([isPositive, isEven]);
815
+ *
816
+ * isPositiveEven(4); // true
817
+ * isPositiveEven(-4); // false
818
+ * isPositiveEven(3); // false
819
+ * ```
820
+ *
821
+ * @template T - 参数类型
822
+ * @param predicates - 要检查的谓词数组
823
+ * @returns 当所有谓词都通过时返回 true 的函数
824
+ */
825
+ declare function allPass<T>(predicates: Array<(value: T) => boolean>): (value: T) => boolean;
826
+ /**
827
+ * 创建当任何谓词返回 true 时返回 true 的函数。
828
+ *
829
+ * @example
830
+ * ```typescript
831
+ * const isZero = (n: number) => n === 0;
832
+ * const isNegative = (n: number) => n < 0;
833
+ * const isNotPositive = anyPass([isZero, isNegative]);
834
+ *
835
+ * isNotPositive(0); // true
836
+ * isNotPositive(-1); // true
837
+ * isNotPositive(1); // false
838
+ * ```
839
+ *
840
+ * @template T - 参数类型
841
+ * @param predicates - 要检查的谓词数组
842
+ * @returns 当任何谓词通过时返回 true 的函数
843
+ */
844
+ declare function anyPass<T>(predicates: Array<(value: T) => boolean>): (value: T) => boolean;
845
+ /**
846
+ * 翻转二元函数的参数。
847
+ *
848
+ * @example
849
+ * ```typescript
850
+ * const divide = (a: number, b: number) => a / b;
851
+ * const flippedDivide = flip(divide);
852
+ *
853
+ * divide(10, 2); // 5
854
+ * flippedDivide(10, 2); // 0.2
855
+ * ```
856
+ *
857
+ * @template A - 第一个参数类型
858
+ * @template B - 第二个参数类型
859
+ * @template R - 返回类型
860
+ * @param fn - 要翻转的函数
861
+ * @returns 参数翻转后的函数
862
+ */
863
+ declare function flip<A, B, R>(fn: (a: A, b: B) => R): (b: B, a: A) => R;
864
+ /**
865
+ * 将值应用到函数。
866
+ * 适用于无点编程。
867
+ *
868
+ * @example
869
+ * ```typescript
870
+ * const addOne = (x: number) => x + 1;
871
+ * apply(5, addOne); // 6
872
+ *
873
+ * const funcs = [x => x + 1, x => x * 2];
874
+ * funcs.map(f => apply(5, f)); // [6, 10]
875
+ * ```
876
+ *
877
+ * @template T - 值类型
878
+ * @template R - 返回类型
879
+ * @param value - 要应用的值
880
+ * @param fn - 要应用的函数
881
+ * @returns 将值应用到函数的结果
882
+ */
883
+ declare function apply<T, R>(value: T, fn: (value: T) => R): R;
884
+ /**
885
+ * 从值创建 thunk(延迟计算)。
886
+ *
887
+ * @example
888
+ * ```typescript
889
+ * const lazyValue = thunk(expensiveComputation);
890
+ * // ... 稍后
891
+ * const result = lazyValue(); // 计算在此处发生
892
+ * ```
893
+ *
894
+ * @template T - 返回类型
895
+ * @param fn - 要延迟的函数
896
+ * @returns 调用时执行函数的 thunk
897
+ */
898
+ declare function thunk<T>(fn: () => T): () => T;
899
+ /**
900
+ * 反转布尔值。
901
+ *
902
+ * @example
903
+ * ```typescript
904
+ * invert(true); // false
905
+ * invert(false); // true
906
+ * ```
907
+ *
908
+ * @param value - 要反转的布尔值
909
+ * @returns 反转后的布尔值
910
+ */
911
+ declare function invert(value: boolean): boolean;
912
+
913
+ export { allPass, alt, anyPass, apply, compose, constant, curry, err, filterOption, flatMapOption, flatMapResult, flip, flow, fromNullable, getOrElse, getOrElseL, identity, invert, isErr, isNone, isOk, isSome, mapOption, mapResult, matchOption, matchResult, memoize, none, noop, not, ok, once, partial, partialRight, pipe, some, tap, thunk, toNullable, tryCatch, tryCatchAsync, uncurry, unwrapOr, unwrapOrElse };