@fncts/base 0.0.29 → 0.0.30

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 (96) hide show
  1. package/_cjs/collection/immutable/HashSet/api.cjs +18 -7
  2. package/_cjs/collection/immutable/HashSet/api.cjs.map +1 -1
  3. package/_cjs/collection/immutable/HashSet/definition.cjs +2 -2
  4. package/_cjs/collection/immutable/HashSet/definition.cjs.map +1 -1
  5. package/_cjs/control/Eval/api.cjs +4 -1
  6. package/_cjs/control/Eval/api.cjs.map +1 -1
  7. package/_cjs/control/Eval/constructors.cjs +9 -2
  8. package/_cjs/control/Eval/constructors.cjs.map +1 -1
  9. package/_cjs/control/Eval/definition.cjs +10 -28
  10. package/_cjs/control/Eval/definition.cjs.map +1 -1
  11. package/_cjs/control/Eval/run.cjs +7 -7
  12. package/_cjs/control/Eval/run.cjs.map +1 -1
  13. package/_cjs/control/{Z → Pure}/api.cjs +146 -115
  14. package/_cjs/control/Pure/api.cjs.map +1 -0
  15. package/_cjs/control/Pure/definition.cjs +78 -0
  16. package/_cjs/control/Pure/definition.cjs.map +1 -0
  17. package/_cjs/control/{Z → Pure}/instances.cjs +1 -1
  18. package/_cjs/control/Pure/instances.cjs.map +1 -0
  19. package/_cjs/control/{Z → Pure}/runtime.cjs +67 -66
  20. package/_cjs/control/Pure/runtime.cjs.map +1 -0
  21. package/_cjs/control/{Z.cjs → Pure.cjs} +5 -5
  22. package/_cjs/control/Pure.cjs.map +1 -0
  23. package/_cjs/data/Showable/show.cjs +31 -31
  24. package/_cjs/data/Showable/show.cjs.map +1 -1
  25. package/_mjs/collection/immutable/HashSet/api.mjs +17 -7
  26. package/_mjs/collection/immutable/HashSet/api.mjs.map +1 -1
  27. package/_mjs/collection/immutable/HashSet/definition.mjs +2 -2
  28. package/_mjs/collection/immutable/HashSet/definition.mjs.map +1 -1
  29. package/_mjs/control/Eval/api.mjs +5 -2
  30. package/_mjs/control/Eval/api.mjs.map +1 -1
  31. package/_mjs/control/Eval/constructors.mjs +10 -3
  32. package/_mjs/control/Eval/constructors.mjs.map +1 -1
  33. package/_mjs/control/Eval/definition.mjs +7 -25
  34. package/_mjs/control/Eval/definition.mjs.map +1 -1
  35. package/_mjs/control/Eval/run.mjs +7 -7
  36. package/_mjs/control/Eval/run.mjs.map +1 -1
  37. package/_mjs/control/{Z → Pure}/api.mjs +134 -104
  38. package/_mjs/control/Pure/api.mjs.map +1 -0
  39. package/_mjs/control/Pure/definition.mjs +60 -0
  40. package/_mjs/control/Pure/definition.mjs.map +1 -0
  41. package/_mjs/control/{Z → Pure}/instances.mjs +1 -1
  42. package/_mjs/control/Pure/instances.mjs.map +1 -0
  43. package/_mjs/control/{Z → Pure}/runtime.mjs +68 -67
  44. package/_mjs/control/Pure/runtime.mjs.map +1 -0
  45. package/_mjs/control/Pure.mjs +6 -0
  46. package/_mjs/control/Pure.mjs.map +1 -0
  47. package/_mjs/data/Showable/show.mjs +31 -31
  48. package/_mjs/data/Showable/show.mjs.map +1 -1
  49. package/_src/collection/immutable/HashSet/api.ts +12 -1
  50. package/_src/collection/immutable/HashSet/definition.ts +2 -2
  51. package/_src/control/Eval/api.ts +5 -2
  52. package/_src/control/Eval/constructors.ts +10 -3
  53. package/_src/control/Eval/definition.ts +39 -27
  54. package/_src/control/Eval/run.ts +7 -7
  55. package/_src/control/Pure/api.ts +629 -0
  56. package/_src/control/Pure/definition.ts +222 -0
  57. package/_src/control/{Z → Pure}/instances.ts +3 -3
  58. package/_src/control/{Z → Pure}/runtime.ts +73 -76
  59. package/_src/control/Pure.ts +6 -0
  60. package/_src/control.ts +1 -1
  61. package/_src/data/Showable/show.ts +55 -53
  62. package/_src/global.ts +1 -1
  63. package/collection/immutable/HashSet/api.d.ts +5 -0
  64. package/collection/immutable/HashSet/definition.d.ts +1 -1
  65. package/control/Eval/constructors.d.ts +4 -0
  66. package/control/Eval/definition.d.ts +25 -28
  67. package/control/Pure/api.d.ts +345 -0
  68. package/control/Pure/definition.d.ts +156 -0
  69. package/control/Pure/instances.d.ts +8 -0
  70. package/control/Pure/runtime.d.ts +56 -0
  71. package/control/Pure.d.ts +4 -0
  72. package/control.d.ts +1 -1
  73. package/data/Showable/show.d.ts +2 -2
  74. package/global.d.ts +1 -1
  75. package/package.json +1 -1
  76. package/_cjs/control/Z/api.cjs.map +0 -1
  77. package/_cjs/control/Z/definition.cjs +0 -163
  78. package/_cjs/control/Z/definition.cjs.map +0 -1
  79. package/_cjs/control/Z/instances.cjs.map +0 -1
  80. package/_cjs/control/Z/runtime.cjs.map +0 -1
  81. package/_cjs/control/Z.cjs.map +0 -1
  82. package/_mjs/control/Z/api.mjs.map +0 -1
  83. package/_mjs/control/Z/definition.mjs +0 -145
  84. package/_mjs/control/Z/definition.mjs.map +0 -1
  85. package/_mjs/control/Z/instances.mjs.map +0 -1
  86. package/_mjs/control/Z/runtime.mjs.map +0 -1
  87. package/_mjs/control/Z.mjs +0 -6
  88. package/_mjs/control/Z.mjs.map +0 -1
  89. package/_src/control/Z/api.ts +0 -590
  90. package/_src/control/Z/definition.ts +0 -199
  91. package/_src/control/Z.ts +0 -6
  92. package/control/Z/api.d.ts +0 -345
  93. package/control/Z/definition.d.ts +0 -151
  94. package/control/Z/instances.d.ts +0 -8
  95. package/control/Z/runtime.d.ts +0 -56
  96. package/control/Z.d.ts +0 -4
@@ -1,590 +0,0 @@
1
- import { identity, tuple } from "../../data/function.js";
2
- import { Access, Chain, Defer, Fail, MapLog, Match, Modify, Provide, Succeed, SucceedNow, Tell } from "./definition.js";
3
-
4
- /**
5
- * @tsplus getter fncts.control.Z absolve
6
- */
7
- export function absolve<W, S1, S2, R, E, E1, A>(fa: Z<W, S1, S2, R, E, Either<E1, A>>): Z<W, S1, S2, R, E | E1, A> {
8
- return fa.flatMap((ea) => ea.match(Z.failNow, Z.succeedNow));
9
- }
10
-
11
- /**
12
- * @tsplus pipeable fncts.control.Z ap
13
- */
14
- export function ap<W, S, A, R1, E1>(fb: Z<W, S, S, R1, E1, A>) {
15
- return <R, E, B>(self: Z<W, S, S, R, E, (a: A) => B>): Z<W, S, S, R & R1, E | E1, B> => {
16
- return self.crossWith(fb, (f, a) => f(a));
17
- };
18
- }
19
-
20
- /**
21
- * @tsplus pipeable fncts.control.Z zipLeft
22
- */
23
- export function zipLeft<W, S, R1, E1, B>(fb: Z<W, S, S, R1, E1, B>) {
24
- return <R, E, A>(fa: Z<W, S, S, R, E, A>): Z<W, S, S, R & R1, E | E1, A> => {
25
- return fa.crossWith(fb, (a, _) => a);
26
- };
27
- }
28
-
29
- /**
30
- * @tsplus pipeable fncts.control.Z zipRight
31
- */
32
- export function zipRight<W, S, R1, E1, B>(fb: Z<W, S, S, R1, E1, B>) {
33
- return <R, E, A>(fa: Z<W, S, S, R, E, A>): Z<W, S, S, R & R1, E | E1, B> => {
34
- return fa.crossWith(fb, (_, b) => b);
35
- };
36
- }
37
-
38
- /**
39
- * @tsplus pipeable fncts.control.Z bimap
40
- */
41
- export function bimap<E, A, G, B>(f: (e: E) => G, g: (a: A) => B) {
42
- return <W, S1, S2, R>(pab: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, G, B> => {
43
- return pab.matchZ(
44
- (e) => Z.failNow(f(e)),
45
- (a) => Z.succeedNow(g(a)),
46
- );
47
- };
48
- }
49
-
50
- /**
51
- * Recovers from all errors.
52
- *
53
- * @tsplus pipeable fncts.control.Z catchAll
54
- */
55
- export function catchAll<W, S1, E, S3, R1, E1, B>(onFailure: (e: E) => Z<W, S1, S3, R1, E1, B>) {
56
- return <S2, R, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S3, R | R1, E1, A | B> => {
57
- return fa.matchZ(onFailure, Z.succeedNow);
58
- };
59
- }
60
-
61
- /**
62
- * @tsplus pipeable fncts.control.Z catchJust
63
- */
64
- export function catchJust<W, S1, E, S3, R1, E1, B>(f: (e: E) => Maybe<Z<W, S1, S3, R1, E1, B>>) {
65
- return <S2, R, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2 | S3, R | R1, E | E1, A | B> => {
66
- return fa.catchAll((e) => f(e).getOrElse(fa));
67
- };
68
- }
69
-
70
- /**
71
- * @tsplus pipeable fncts.control.Z flatMap
72
- */
73
- export function flatMap<S2, A, W1, S3, R1, E1, B>(f: (a: A) => Z<W1, S2, S3, R1, E1, B>) {
74
- return <W, S1, R, E>(ma: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, R1 & R, E1 | E, B> => {
75
- return new Chain(ma, f);
76
- };
77
- }
78
-
79
- /**
80
- * @tsplus pipeable fncts.control.Z contramapEnvironment
81
- */
82
- export function contramapEnvironment<R0, R>(f: (r0: Environment<R0>) => Environment<R>) {
83
- return <W, S1, S2, E, A>(ma: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R0, E, A> => {
84
- return Z.environmentWithZ((r) => ma.provideEnvironment(f(r)));
85
- };
86
- }
87
-
88
- /**
89
- * Transforms the initial state of this computation` with the specified
90
- * function.
91
- *
92
- * @tsplus pipeable fncts.control.Z contramapState
93
- */
94
- export function contramapState<S0, S1>(f: (s: S0) => S1) {
95
- return <W, S2, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S0, S2, R, E, A> => {
96
- return Z.update(f).flatMap(() => fa);
97
- };
98
- }
99
-
100
- /**
101
- * @tsplus pipeable fncts.control.Z cross
102
- */
103
- export function cross<W, S, R1, E1, B>(fb: Z<W, S, S, R1, E1, B>) {
104
- return <R, E, A>(fa: Z<W, S, S, R, E, A>): Z<W, S, S, R & R1, E | E1, readonly [A, B]> => {
105
- return fa.crossWith(fb, tuple);
106
- };
107
- }
108
-
109
- /**
110
- * @tsplus pipeable fncts.control.Z crossWith
111
- */
112
- export function crossWith<W, S, A, R1, E1, B, C>(fb: Z<W, S, S, R1, E1, B>, f: (a: A, b: B) => C) {
113
- return <R, E>(fa: Z<W, S, S, R, E, A>): Z<W, S, S, R & R1, E | E1, C> => {
114
- return fa.flatMap((a) => fb.map((b) => f(a, b)));
115
- };
116
- }
117
-
118
- /**
119
- * @tsplus static fncts.control.ZOps defer
120
- */
121
- export function defer<W, S1, S2, R, E, A>(
122
- ma: Lazy<Z<W, S1, S2, R, E, A>>,
123
- __tsplusTrace?: string,
124
- ): Z<W, S1, S2, R, E, A> {
125
- return new Defer(ma);
126
- }
127
-
128
- /**
129
- * Returns a computation whose failure and success have been lifted into an
130
- * `Either`. The resulting computation cannot fail, because the failure case
131
- * has been exposed as part of the `Either` success case.
132
- *
133
- * @tsplus getter fncts.control.Z either
134
- */
135
- export function either<W, S1, S2, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S1 | S2, R, never, Either<E, A>> {
136
- return fa.match(Either.left, Either.right);
137
- }
138
-
139
- /**
140
- * @tsplus static fncts.control.ZOps environment
141
- */
142
- export function environment<R>(): Z<never, unknown, never, R, never, Environment<R>> {
143
- return new Access((r) => Z.succeedNow(r));
144
- }
145
-
146
- /**
147
- * @tsplus static fncts.control.ZOps environmentWith
148
- */
149
- export function environmentWith<R0, A>(f: (r: Environment<R0>) => A): Z<never, unknown, never, R0, never, A> {
150
- return new Access((r) => succeed(f(r)));
151
- }
152
-
153
- /**
154
- * @tsplus static fncts.control.ZOps environmentWithZ
155
- */
156
- export function environmentWithZ<R0, W, S1, S2, R, E, A>(
157
- f: (r: Environment<R0>) => Z<W, S1, S2, R, E, A>,
158
- ): Z<W, S1, S2, R | R0, E, A> {
159
- return new Access(f);
160
- }
161
-
162
- /**
163
- * Erases the current log
164
- *
165
- * @tsplus getter fncts.control.Z erase
166
- */
167
- export function erase<W, S1, S2, R, E, A>(wa: Z<W, S1, S2, R, E, A>): Z<never, S1, S2, R, E, A> {
168
- return wa.mapLog(() => Conc.empty());
169
- }
170
-
171
- /**
172
- * @tsplus static fncts.control.ZOps fail
173
- */
174
- export function fail<E>(e: Lazy<E>, __tsplusTrace?: string): Z<never, unknown, never, never, E, never> {
175
- return Z.failCause(Cause.fail(e()));
176
- }
177
-
178
- /**
179
- * @tsplus static fncts.control.ZOps failNow
180
- */
181
- export function failNow<E>(e: E, __tsplusTrace?: string): Z<never, unknown, never, never, E, never> {
182
- return Z.failCauseNow(Cause.fail(e));
183
- }
184
-
185
- /**
186
- * @tsplus static fncts.control.ZOps failCause
187
- */
188
- export function failCause<E>(cause: Lazy<Cause<E>>, __tsplusTrace?: string): Z<never, unknown, never, never, E, never> {
189
- return Z.defer(Z.failCauseNow(cause()));
190
- }
191
-
192
- /**
193
- * @tsplus static fncts.control.ZOps failCauseNow
194
- */
195
- export function failCauseNow<E>(cause: Cause<E>, __tsplusTrace?: string): Z<never, unknown, never, never, E, never> {
196
- return new Fail(cause);
197
- }
198
-
199
- /**
200
- * @tsplus getter fncts.control.Z flatten
201
- */
202
- export function flatten<W, S1, S2, R, E, A, W1, S3, R1, E1>(
203
- mma: Z<W, S1, S2, R, E, Z<W1, S2, S3, R1, E1, A>>,
204
- ): Z<W | W1, S1, S3, R1 & R, E1 | E, A> {
205
- return mma.flatMap(identity);
206
- }
207
-
208
- /**
209
- * Constructs a computation that returns the initial state unchanged.
210
- *
211
- * @tsplus static fncts.control.ZOps get
212
- */
213
- export function get<S>(): Z<never, S, S, never, never, S> {
214
- return Z.modify((s) => [s, s]);
215
- }
216
-
217
- /**
218
- * @tsplus static fncts.control.ZOps gets
219
- */
220
- export function gets<S, A>(f: (s: S) => A): Z<never, S, S, never, never, A> {
221
- return Z.modify((s) => [f(s), s]);
222
- }
223
-
224
- /**
225
- * @tsplus static fncts.control.ZOps getsZ
226
- */
227
- export function getsZ<S, W, R, E, A>(f: (s: S) => Z<W, S, S, R, E, A>): Z<W, S, S, R, E, A> {
228
- return Z.get<S>().flatMap(f);
229
- }
230
-
231
- /**
232
- * @tsplus static fncts.control.ZOps halt
233
- */
234
- export function halt(defect: Lazy<unknown>, __tsplusTrace?: string): Z<never, unknown, never, never, never, never> {
235
- return Z.failCause(Cause.halt(defect()));
236
- }
237
-
238
- /**
239
- * @tsplus static fncts.control.ZOps haltNow
240
- */
241
- export function haltNow(defect: unknown, __tsplusTrace?: string): Z<never, unknown, never, never, never, never> {
242
- return Z.failCauseNow(Cause.halt(defect));
243
- }
244
-
245
- /**
246
- * @tsplus getter fncts.control.Z listen
247
- */
248
- export function listen<W, S1, S2, R, E, A>(wa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, E, readonly [A, Conc<W>]> {
249
- return wa.matchLogCauseZ(
250
- (_, e) => Z.failCauseNow(e),
251
- (ws, a) => Z.succeedNow([a, ws]),
252
- );
253
- }
254
-
255
- /**
256
- * @tsplus pipeable fncts.control.Z listens
257
- */
258
- export function listens<W, B>(f: (log: Conc<W>) => B) {
259
- return <S1, S2, R, E, A>(wa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, E, readonly [A, B]> => {
260
- return wa.listen.map(([a, ws]) => [a, f(ws)]);
261
- };
262
- }
263
-
264
- /**
265
- * @tsplus pipeable fncts.control.Z map
266
- */
267
- export function map<A, B>(f: (a: A) => B) {
268
- return <W, S1, S2, R, E>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, E, B> => {
269
- return fa.flatMap((a) => Z.succeedNow(f(a)));
270
- };
271
- }
272
-
273
- /**
274
- * @tsplus pipeable fncts.control.Z mapError
275
- */
276
- export function mapError<E, G>(f: (e: E) => G) {
277
- return <W, S1, S2, R, A>(pab: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, G, A> => {
278
- return pab.matchZ((e) => Z.failNow(f(e)), Z.succeedNow);
279
- };
280
- }
281
-
282
- /**
283
- * Modifies the current log with the specified function
284
- *
285
- * @tsplus pipeable fncts.control.Z mapLog
286
- */
287
- export function mapLog<W, W1>(f: (ws: Conc<W>) => Conc<W1>) {
288
- return <S1, S2, R, E, A>(wa: Z<W, S1, S2, R, E, A>): Z<W1, S1, S2, R, E, A> => {
289
- return new MapLog(wa, f);
290
- };
291
- }
292
-
293
- /**
294
- * Modifies the current state with the specified function
295
- *
296
- * @tsplus pipeable fncts.control.Z mapState
297
- */
298
- export function mapState<S2, S3>(f: (s: S2) => S3) {
299
- return <W, S1, R, E, A>(ma: Z<W, S1, S2, R, E, A>): Z<W, S1, S3, R, E, A> => {
300
- return ma.transform((s, a) => [a, f(s)]);
301
- };
302
- }
303
-
304
- /**
305
- * Folds over the failed or successful results of this computation to yield
306
- * a computation that does not fail, but succeeds with the value of the left
307
- * or right function passed to `match`.
308
- *
309
- * @tsplus pipeable fncts.control.Z match
310
- */
311
- export function match<E, A, B, C>(onFailure: (e: E) => B, onSuccess: (a: A) => C) {
312
- return <W, S1, S2, R>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, never, B | C> => {
313
- return fa.matchZ(
314
- (e) => Z.succeedNow(onFailure(e)),
315
- (a) => Z.succeedNow(onSuccess(a)),
316
- );
317
- };
318
- }
319
-
320
- /**
321
- * Recovers from errors by accepting one computation to execute for the case
322
- * of an error, and one computation to execute for the case of success.
323
- *
324
- * @tsplus pipeable fncts.control.Z matchZ
325
- */
326
- export function matchZ<S5, S2, E, A, W1, S3, R1, E1, B, W2, S4, R2, E2, C>(
327
- onFailure: (e: E) => Z<W1, S5, S3, R1, E1, B>,
328
- onSuccess: (a: A) => Z<W2, S2, S4, R2, E2, C>,
329
- ) {
330
- return <W, S1, R>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1 | W2, S1 & S5, S3 | S4, R | R1 | R2, E1 | E2, B | C> => {
331
- return fa.matchCauseZ((cause) => cause.failureOrCause.match(onFailure, Z.failCauseNow), onSuccess);
332
- };
333
- }
334
-
335
- /**
336
- * @tsplus pipeable fncts.control.Z matchCauseZ
337
- */
338
- export function matchCauseZ<S2, E, A, W1, S0, S3, R1, E1, B, W2, S4, R2, E2, C>(
339
- onFailure: (e: Cause<E>) => Z<W1, S0, S3, R1, E1, B>,
340
- onSuccess: (a: A) => Z<W2, S2, S4, R2, E2, C>,
341
- ) {
342
- return <W, S1, R>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1 | W2, S0 & S1, S3 | S4, R | R1 | R2, E1 | E2, B | C> => {
343
- return fa.matchLogCauseZ(
344
- (ws, e) => onFailure(e).mapLog((w1s) => ws.concat(w1s)),
345
- (ws, a) => onSuccess(a).mapLog((w2s) => ws.concat(w2s)),
346
- );
347
- };
348
- }
349
-
350
- /**
351
- * @tsplus pipeable fncts.control.Z matchLogZ
352
- */
353
- export function matchLogZ<W, S5, S2, E, A, W1, S3, R1, E1, B, W2, S4, R2, E2, C>(
354
- onFailure: (ws: Conc<W>, e: E) => Z<W1, S5, S3, R1, E1, B>,
355
- onSuccess: (ws: Conc<W>, a: A) => Z<W2, S2, S4, R2, E2, C>,
356
- ) {
357
- return <S1, R>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1 | W2, S1 & S5, S3 | S4, R | R1 | R2, E1 | E2, B | C> => {
358
- return fa.matchLogCauseZ(
359
- (ws, cause) => cause.failureOrCause.match((e) => onFailure(ws, e), Z.failCauseNow),
360
- onSuccess,
361
- );
362
- };
363
- }
364
-
365
- /**
366
- * Recovers from errors by accepting one computation to execute for the case
367
- * of an error, and one computation to execute for the case of success. More powerful
368
- * than `matchCauseM` by providing the current state of the log as an argument in
369
- * each case
370
- *
371
- * @note the log is cleared after being provided
372
- *
373
- * @tsplus pipeable fncts.control.Z matchLogCauseZ
374
- */
375
- export function matchLogCauseZ<W, S2, E, A, W1, S0, S3, R1, E1, B, W2, S4, R2, E2, C>(
376
- onFailure: (ws: Conc<W>, e: Cause<E>) => Z<W1, S0, S3, R1, E1, B>,
377
- onSuccess: (ws: Conc<W>, a: A) => Z<W2, S2, S4, R2, E2, C>,
378
- ) {
379
- return <S1, R>(fa: Z<W, S1, S2, R, E, A>): Z<W1 | W2, S0 & S1, S3 | S4, R & R1 & R2, E1 | E2, B | C> => {
380
- return new Match(fa, onFailure, onSuccess);
381
- };
382
- }
383
-
384
- /**
385
- * Constructs a computation from the specified modify function
386
- *
387
- * @tsplus static fncts.control.ZOps modify
388
- */
389
- export function modify<S1, S2, A>(f: (s: S1) => readonly [A, S2]): Z<never, S1, S2, never, never, A> {
390
- return new Modify(f);
391
- }
392
-
393
- /**
394
- * Constructs a computation that may fail from the specified modify function.
395
- *
396
- * @tsplus static fncts.control.ZOps modifyEither
397
- */
398
- export function modifyEither<S1, S2, E, A>(f: (s: S1) => Either<E, readonly [A, S2]>): Z<never, S1, S2, never, E, A> {
399
- return Z.get<S1>()
400
- .map(f)
401
- .flatMap((r) => r.match(Z.failNow, ([a, s2]) => Z.succeedNow(a).mapState(() => s2)));
402
- }
403
-
404
- /**
405
- * @tsplus pipeable fncts.control.Z orElse
406
- */
407
- export function orElse<S1, W1, S3, R1, E1, A1>(fb: Lazy<Z<W1, S1, S3, R1, E1, A1>>) {
408
- return <W, S2, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S2 | S3, R | R1, E | E1, A | A1> => {
409
- return fa.matchZ(() => fb(), Z.succeedNow);
410
- };
411
- }
412
-
413
- /**
414
- * Executes this computation and returns its value, if it succeeds, but
415
- * otherwise executes the specified computation.
416
- *
417
- * @tsplus pipeable fncts.control.Z orElseEither
418
- */
419
- export function orElseEither<W, S3, S4, R1, E1, A1>(that: Lazy<Z<W, S3, S4, R1, E1, A1>>) {
420
- return <S1, S2, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1 & S3, S2 | S4, R | R1, E1, Either<A, A1>> => {
421
- return fa.matchZ(
422
- () => that().map(Either.right),
423
- (a) => Z.succeedNow(Either.left(a)),
424
- );
425
- };
426
- }
427
-
428
- /**
429
- * @tsplus pipeable fncts.control.Z provideEnvironment
430
- */
431
- export function provideEnvironment<R>(r: Environment<R>) {
432
- return <W, S1, S2, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, never, E, A> => {
433
- return new Provide(fa, r);
434
- };
435
- }
436
-
437
- /**
438
- * Constructs a computation that sets the state to the specified value.
439
- *
440
- * @tsplus static fncts.control.ZOps put
441
- */
442
- export function put<S>(s: S): Z<never, unknown, S, never, never, void> {
443
- return Z.modify(() => [undefined, s]);
444
- }
445
-
446
- /**
447
- * Repeats this computation the specified number of times (or until the first failure)
448
- * passing the updated state to each successive repetition.
449
- *
450
- * @tsplus pipeable fncts.control.Z repeatN
451
- */
452
- export function repeatN(n: number) {
453
- return <W, S1, S2 extends S1, R, E, A>(ma: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, E, A> => {
454
- return ma.flatMap((a) => (n <= 0 ? Z.succeedNow(a) : ma.repeatN(n - 1)));
455
- };
456
- }
457
-
458
- /**
459
- * Repeats this computation until its value satisfies the specified predicate
460
- * (or until the first failure) passing the updated state to each successive repetition.
461
- *
462
- * @tsplus pipeable fncts.control.Z repeatUntil
463
- */
464
- export function repeatUntil<A>(p: Predicate<A>) {
465
- return <W, S1, S2 extends S1, R, E>(ma: Z<W, S1, S2, R, E, A>): Z<W, S1, S2, R, E, A> => {
466
- return ma.flatMap((a) => (p(a) ? Z.succeedNow(a) : ma.repeatUntil(p)));
467
- };
468
- }
469
-
470
- /**
471
- * @tsplus static fncts.control.ZOps succeed
472
- */
473
- export function succeed<A, W = never, S1 = unknown, S2 = never>(
474
- effect: Lazy<A>,
475
- __tsplusTrace?: string,
476
- ): Z<W, S1, S2, never, never, A> {
477
- return new Succeed(effect);
478
- }
479
-
480
- /**
481
- * @tsplus static fncts.control.ZOps succeedNow
482
- */
483
- export function succeedNow<A, W = never, S1 = unknown, S2 = never>(
484
- a: A,
485
- __tsplusTrace?: string,
486
- ): Z<W, S1, S2, never, never, A> {
487
- return new SucceedNow(a);
488
- }
489
-
490
- /**
491
- * @tsplus pipeable fncts.control.Z tap
492
- */
493
- export function tap<S2, A, W1, S3, R1, E1, B>(f: (a: A) => Z<W1, S2, S3, R1, E1, B>) {
494
- return <W, S1, R, E>(ma: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, R1 & R, E1 | E, A> => {
495
- return ma.flatMap((a) => f(a).map(() => a));
496
- };
497
- }
498
-
499
- /**
500
- * @tsplus static fncts.control.ZOps tell
501
- */
502
- export function tell<W>(w: W): Z<W, unknown, never, never, never, void> {
503
- return Z.tellAll(Conc.single(w));
504
- }
505
-
506
- /**
507
- * @tsplus static fncts.control.ZOps tellAll
508
- */
509
- export function tellAll<W>(ws: Conc<W>): Z<W, unknown, never, never, never, void> {
510
- return new Tell(ws);
511
- }
512
-
513
- /**
514
- * Like `map`, but also allows the state to be modified.
515
- *
516
- * @tsplus pipeable fncts.control.Z transform
517
- */
518
- export function transform<S2, A, S3, B>(f: (s: S2, a: A) => readonly [B, S3]) {
519
- return <W, S1, R, E>(ma: Z<W, S1, S2, R, E, A>): Z<W, S1, S3, R, E, B> => {
520
- return ma.flatMap((a) => Z.modify((s) => f(s, a)));
521
- };
522
- }
523
-
524
- /**
525
- * @tsplus static fncts.control.ZOps unit
526
- */
527
- export const unit: Z<never, unknown, never, never, never, void> = Z.succeedNow(undefined);
528
-
529
- /**
530
- * Constructs a computation from the specified update function.
531
- *
532
- * @tsplus static fncts.control.ZOps update
533
- */
534
- export function update<S1, S2>(f: (s: S1) => S2): Z<never, S1, S2, never, never, void> {
535
- return Z.modify((s) => [undefined, f(s)]);
536
- }
537
-
538
- /**
539
- * @tsplus pipeable fncts.control.Z write
540
- */
541
- export function write<W1>(w: W1) {
542
- return <W, S1, S2, R, E, A>(ma: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S2, R, E, A> => {
543
- return ma.writeAll(Conc.single(w));
544
- };
545
- }
546
-
547
- /**
548
- * @tsplus pipeable fncts.control.Z writeAll
549
- */
550
- export function writeAll<W1>(log: Conc<W1>) {
551
- return <W, S1, S2, R, E, A>(ma: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S2, R, E, A> => {
552
- return ma.mapLog((ws) => ws.concat(log));
553
- };
554
- }
555
-
556
- /**
557
- * @tsplus pipeable fncts.control.Z zip
558
- */
559
- export function zip<S2, W1, S3, Q, D, B>(fb: Z<W1, S2, S3, Q, D, B>) {
560
- return <W, S1, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, Q & R, D | E, Zipped.Make<A, B>> => {
561
- return fa.zipWith(fb, (a, b) => Zipped(a, b));
562
- };
563
- }
564
-
565
- /**
566
- * @tsplus pipeable fncts.control.Z zipFirst
567
- */
568
- export function zipFirst<S2, W1, S3, Q, D, B>(fb: Z<W1, S2, S3, Q, D, B>) {
569
- return <W, S1, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, Q & R, D | E, A> => {
570
- return fa.zipWith(fb, (a, _) => a);
571
- };
572
- }
573
-
574
- /**
575
- * @tsplus pipeable fncts.control.Z zipSecond
576
- */
577
- export function zipSecond<S2, W1, S3, Q, D, B>(fb: Z<W1, S2, S3, Q, D, B>) {
578
- return <W, S1, R, E, A>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, Q & R, D | E, B> => {
579
- return fa.zipWith(fb, (_, b) => b);
580
- };
581
- }
582
-
583
- /**
584
- * @tsplus pipeable fncts.control.Z zipWith
585
- */
586
- export function zipWith<S2, A, W1, S3, R1, E1, B, C>(fb: Z<W1, S2, S3, R1, E1, B>, f: (a: A, b: B) => C) {
587
- return <W, S1, R, E>(fa: Z<W, S1, S2, R, E, A>): Z<W | W1, S1, S3, R1 & R, E1 | E, C> => {
588
- return fa.flatMap((a) => fb.map((b) => f(a, b)));
589
- };
590
- }