-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstate.ts
369 lines (344 loc) · 7.66 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* The State module contains the State structure. The purpose of State is to
* have modifiable date in an immutable workflow. This structure must preserve
* purity, so that subsequent executions of a state workflow with the same
* initial conditions produce the exact same results.
*
* @module State
* @since 2.0.0
*/
import type { InOut, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Combinable } from "./combinable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { Initializable } from "./initializable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Wrappable } from "./wrappable.ts";
import { flow } from "./fn.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
/**
* The State<E, A> type represents the core State structure. The input/output
* variable E is invariant, and the output variable A is covariant.
*
* @since 2.0.0
*/
export type State<E, A> = (e: E) => [A, E];
/**
* Specifies State as a Higher Kinded Type, with covariant parameter A in the
* 0th index of any substitutions and invariant parameter E in the 0th parameter
* of any substitutions.
*
* @since 2.0.0
*/
export interface KindState extends Kind {
readonly kind: State<InOut<this, 1>, Out<this, 0>>;
}
/**
* An internal optimization over State identity
*
* @since 2.0.0
*/
// deno-lint-ignore no-explicit-any
const _identity: State<any, any> = (s) => [s, s];
/**
* Construct a trivial State<E, A> from values E and A.
*
* @example
* ```ts
* import * as S from "./state.ts";
*
* const state = S.state(1, 2);
*
* const result = state(10); // [2, 1]
* ```
*
* @since 2.0.0
*/
export function state<E, A>(a: A, e: E): State<E, A> {
return () => [a, e];
}
/**
* An instance of Id that makes the State structure a Category. This is often
* used at the beginning of a State workflow to specify the type of data within
* a State structure.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const num = pipe(
* S.id<number>(),
* S.map(n => n + 1),
* );
*
* const result = num(1); // [2, 1]
* ```
*
* @since 2.0.0
*/
export function id<E>(): State<E, E> {
return _identity;
}
/**
* Construct a State<E, A> from a function E => A.
*
* @example
* ```ts
* import * as S from "./state.ts";
*
* const length = S.gets((s: string) => s.length);
*
* const result = length("Hello World"); // [11, "Hello World"]
* ```
*
* @since 2.0.0
*/
export function gets<E, A>(fea: (e: E) => A): State<E, A> {
return (e) => [fea(e), e];
}
/**
* Construct a State<E, void> from a static state value E.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const state = pipe(
* S.id<number>(),
* S.flatmap(n => pipe(S.id<number>(), S.map(m => m + n))),
* );
*
* const result = state(100); // [2, 1]
* ```
*
* @since 2.0.0
*/
export function put<E>(e: E): State<E, void> {
return () => [undefined, e];
}
/**
* Construct a State<E, void> from a function E => E.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const state = S.modify((n: number) => n + 100);
*
* const result = state(1); // [undefined, 101]
* ```
*
* @since 2.0.0
*/
export function modify<E>(fee: (e: E) => E): State<E, void> {
return (e) => [undefined, fee(e)];
}
/**
* Construct a State<E, A> from a static value A.
*
* @example
* ```ts
* import * as S from "./state.ts";
*
* const state = S.wrap(1);
*
* const result = state(null); // [1, null]
* ```
*
* @since 2.0.0
*/
export function wrap<A, E = unknown>(a: A): State<E, A> {
return (e) => [a, e];
}
/**
* Map over the covariant value A in State<E, A>.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const work = pipe(
* S.id<number>(),
* S.map(n => A.range(n)),
* );
*
* const result1 = work(1); // [[0], 1]
* const result2 = work(3); // [[0, 1, 2], 3]
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <B>(ta: State<B, A>) => State<B, I> {
return (ta) => flow(ta, ([a, b]) => [fai(a), b]);
}
/**
* Apply the A value of State<E, A> to the (a: A) => I value of
* State<E, (a: A) => I>, producing a State<E, I>.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const work = pipe(
* S.id<string>(),
* S.map(s => (n: number) => s.repeat(n)),
* S.apply(S.gets(s => s.length))
* );
*
* const result1 = work("Hi"); // ["HiHi", "Hi"]
* const result2 = work("Hello");
* // ["HelloHelloHelloHelloHello", "Hello"]
* ```
*
* @since 2.0.0
*/
export function apply<E, A>(
ua: State<E, A>,
): <I>(ufai: State<E, (a: A) => I>) => State<E, I> {
return (ufai) => (s1) => {
const [fai, s2] = ufai(s1);
const [a, s3] = ua(s2);
return [fai(a), s3];
};
}
/**
* Pass the A value in a State<S, A> into a function (a: A) => State<S, I>. This
* results in a new State<S, I>.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const state = pipe(
* S.id<number>(),
* S.flatmap(n => S.wrap(n + 1)),
* );
*
* const result1 = state(1); // [2, 1]
* const result2 = state(2); // [3, 2]
* ```
*
* @since 2.0.0
*/
export function flatmap<A, E, I>(
fati: (a: A) => State<E, I>,
): (ta: State<E, A>) => State<E, I> {
return (ta) => flow(ta, ([a, s]) => fati(a)(s));
}
/**
* Extract the result value A by executing State<S, A> with an S value.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* S.id<number>(),
* S.map(n => n + 1),
* S.evaluate(10),
* ); // 11
* ```
*
* @since 2.0.0
*/
export function evaluate<S>(s: S): <A>(ta: State<S, A>) => A {
return (ta) => ta(s)[0];
}
/**
* Extract the ending state value S by executing State<S, A> with an S value.
*
* @example
* ```ts
* import * as S from "./state.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* S.id<number>(),
* S.map(n => n + 1),
* S.execute(10),
* ); // 10
* ```
*
* @since 2.0.0
*/
export function execute<S>(s: S): <A>(ta: State<S, A>) => S {
return (ta) => ta(s)[1];
}
/**
* @since 2.0.0
*/
export function getCombinableState<E, A>(
CE: Combinable<E>,
CA: Combinable<A>,
): Combinable<State<E, A>> {
return {
combine: (second) => (first) => (e) => {
const [fa, fe] = first(e);
const [sa, se] = second(e);
return [CA.combine(sa)(fa), CE.combine(se)(fe)];
},
};
}
/**
* @since 2.0.0
*/
export function getInitializableState<E, A>(
IE: Initializable<E>,
IA: Initializable<A>,
): Initializable<State<E, A>> {
return {
init: () => (e) => [IA.init(), IE.combine(e)(IE.init())],
...getCombinableState(IE, IA),
};
}
/**
* @since 2.0.0
*/
export const ApplicableState: Applicable<KindState> = {
apply,
map,
wrap,
};
/**
* The canonical implementation of Flatmappable for State. It contains
* the methods wrap, apply, map, join, and flatmap.
*
* @since 2.0.0
*/
export const FlatmappableState: Flatmappable<KindState> = {
apply,
flatmap,
map,
wrap,
};
/**
* @since 2.0.0
*/
export const MappableState: Mappable<KindState> = { map };
/**
* @since 2.0.0
*/
export const WrappableState: Wrappable<KindState> = { wrap };
/**
* @since 2.0.0
*/
export const tap: Tap<KindState> = createTap(FlatmappableState);
/**
* @since 2.0.0
*/
export const bind: Bind<KindState> = createBind(FlatmappableState);
/**
* @since 2.0.0
*/
export const bindTo: BindTo<KindState> = createBindTo(MappableState);