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