@barnum/barnum 0.2.3 → 0.4.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.
- package/artifacts/linux-arm64/barnum +0 -0
- package/artifacts/linux-x64/barnum +0 -0
- package/artifacts/macos-arm64/barnum +0 -0
- package/artifacts/macos-x64/barnum +0 -0
- package/artifacts/win-x64/barnum.exe +0 -0
- package/cli.cjs +33 -0
- package/dist/all.d.ts +43 -0
- package/dist/all.d.ts.map +1 -0
- package/dist/all.js +8 -0
- package/dist/ast.d.ts +476 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +419 -0
- package/dist/bind.d.ts +59 -0
- package/dist/bind.d.ts.map +1 -0
- package/dist/bind.js +69 -0
- package/dist/builtins/array.d.ts +36 -0
- package/dist/builtins/array.d.ts.map +1 -0
- package/dist/builtins/array.js +93 -0
- package/dist/builtins/index.d.ts +6 -0
- package/dist/builtins/index.d.ts.map +1 -0
- package/dist/builtins/index.js +5 -0
- package/dist/builtins/scalar.d.ts +12 -0
- package/dist/builtins/scalar.d.ts.map +1 -0
- package/dist/builtins/scalar.js +41 -0
- package/dist/builtins/struct.d.ts +25 -0
- package/dist/builtins/struct.d.ts.map +1 -0
- package/dist/builtins/struct.js +67 -0
- package/dist/builtins/tagged-union.d.ts +54 -0
- package/dist/builtins/tagged-union.d.ts.map +1 -0
- package/dist/builtins/tagged-union.js +81 -0
- package/dist/builtins/with-resource.d.ts +23 -0
- package/dist/builtins/with-resource.d.ts.map +1 -0
- package/dist/builtins/with-resource.js +35 -0
- package/dist/chain.d.ts +3 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +8 -0
- package/dist/effect-id.d.ts +15 -0
- package/dist/effect-id.d.ts.map +1 -0
- package/dist/effect-id.js +16 -0
- package/dist/handler.d.ts +51 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +130 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/iterator.d.ts +32 -0
- package/dist/iterator.d.ts.map +1 -0
- package/dist/iterator.js +123 -0
- package/dist/option.d.ts +74 -0
- package/dist/option.d.ts.map +1 -0
- package/dist/option.js +141 -0
- package/dist/pipe.d.ts +12 -0
- package/dist/pipe.d.ts.map +1 -0
- package/dist/pipe.js +12 -0
- package/dist/race.d.ts +54 -0
- package/dist/race.d.ts.map +1 -0
- package/dist/race.js +116 -0
- package/dist/recursive.d.ts +40 -0
- package/dist/recursive.d.ts.map +1 -0
- package/dist/recursive.js +58 -0
- package/dist/result.d.ts +50 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +117 -0
- package/dist/run.d.ts +14 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +160 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +7 -0
- package/dist/schema.d.ts +9 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +95 -0
- package/dist/schemas.d.ts +5 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +13 -0
- package/dist/try-catch.d.ts +24 -0
- package/dist/try-catch.d.ts.map +1 -0
- package/dist/try-catch.js +37 -0
- package/dist/values.d.ts +6 -0
- package/dist/values.d.ts.map +1 -0
- package/dist/values.js +12 -0
- package/dist/worker.d.ts +15 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +58 -0
- package/package.json +42 -16
- package/src/all.ts +133 -0
- package/src/ast.ts +1301 -0
- package/src/bind.ts +162 -0
- package/src/builtins/array.ts +121 -0
- package/src/builtins/index.ts +17 -0
- package/src/builtins/scalar.ts +49 -0
- package/src/builtins/struct.ts +111 -0
- package/src/builtins/tagged-union.ts +142 -0
- package/src/builtins/with-resource.ts +69 -0
- package/src/chain.ts +17 -0
- package/src/effect-id.ts +30 -0
- package/src/handler.ts +263 -0
- package/src/index.ts +37 -0
- package/src/iterator.ts +243 -0
- package/src/option.ts +199 -0
- package/src/pipe.ts +138 -0
- package/src/race.ts +173 -0
- package/src/recursive.ts +129 -0
- package/src/result.ts +168 -0
- package/src/run.ts +209 -0
- package/src/runtime.ts +16 -0
- package/src/schema.ts +118 -0
- package/src/schemas.ts +21 -0
- package/src/try-catch.ts +57 -0
- package/src/values.ts +21 -0
- package/src/worker.ts +71 -0
- package/README.md +0 -19
- package/barnum-config-schema.json +0 -408
- package/cli.js +0 -20
- package/index.js +0 -23
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./ast.js";
|
|
2
|
+
export { allObject, asOption, constant, drop, flatten, getField, getIndex, identity, panic, pick, range, splitFirst, splitLast, tag, taggedUnionSchema, withResource, wrapInField, } from "./builtins/index.js";
|
|
3
|
+
export { Option, first, last } from "./option.js";
|
|
4
|
+
export { Result } from "./result.js";
|
|
5
|
+
export { Iterator } from "./iterator.js";
|
|
6
|
+
export { runPipeline } from "./run.js";
|
|
7
|
+
export { zodToCheckedJsonSchema } from "./schema.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Iterator as IteratorT, type Option as OptionT, type Pipeable, type Result as ResultT, type TypedAction } from "./ast.js";
|
|
2
|
+
export declare const Iterator: {
|
|
3
|
+
/** Wrap an array as Iterator. `T[] → Iterator<T>` */
|
|
4
|
+
readonly fromArray: <TElement>() => TypedAction<TElement[], IteratorT<TElement>>;
|
|
5
|
+
/** Wrap an Option as Iterator. `Option<T> → Iterator<T>` */
|
|
6
|
+
readonly fromOption: <TElement>() => TypedAction<OptionT<TElement>, IteratorT<TElement>>;
|
|
7
|
+
/** Wrap a Result as Iterator (Ok kept, Err dropped). `Result<T, E> → Iterator<T>` */
|
|
8
|
+
readonly fromResult: <TElement, TError>() => TypedAction<ResultT<TElement, TError>, IteratorT<TElement>>;
|
|
9
|
+
/** Unwrap Iterator to array. `Iterator<T> → T[]` */
|
|
10
|
+
readonly collect: <TElement>() => TypedAction<IteratorT<TElement>, TElement[]>;
|
|
11
|
+
/** Transform each element. `Iterator<T> → Iterator<U>` */
|
|
12
|
+
readonly map: <TIn, TOut>(action: Pipeable<TIn, TOut>) => TypedAction<IteratorT<TIn>, IteratorT<TOut>>;
|
|
13
|
+
/** Flat-map each element. `f` returns any IntoIterator type. `Iterator<T> → Iterator<U>` */
|
|
14
|
+
readonly flatMap: <TIn, TOut>(action: Pipeable<TIn, unknown>) => TypedAction<IteratorT<TIn>, IteratorT<TOut>>;
|
|
15
|
+
/** Keep elements where predicate returns true. `Iterator<T> → Iterator<T>` */
|
|
16
|
+
readonly filter: <TElement>(predicate: Pipeable<TElement, boolean>) => TypedAction<IteratorT<TElement>, IteratorT<TElement>>;
|
|
17
|
+
/** Head/tail decomposition. `Iterator<T> → Option<[T, Iterator<T>]>` */
|
|
18
|
+
readonly splitFirst: <TElement>() => TypedAction<IteratorT<TElement>, OptionT<[TElement, IteratorT<TElement>]>>;
|
|
19
|
+
/** Init/last decomposition. `Iterator<T> → Option<[Iterator<T>, T]>` */
|
|
20
|
+
readonly splitLast: <TElement>() => TypedAction<IteratorT<TElement>, OptionT<[IteratorT<TElement>, TElement]>>;
|
|
21
|
+
/** Fold elements with accumulator. `Iterator<T> → TAcc` */
|
|
22
|
+
readonly fold: <TElement, TAcc>(init: Pipeable<void, TAcc>, body: Pipeable<[TAcc, TElement], TAcc>) => TypedAction<IteratorT<TElement>, TAcc>;
|
|
23
|
+
/** Slice elements from start to end. `Iterator<T> → Iterator<T>` */
|
|
24
|
+
readonly slice: <TElement>(start: number, end?: number) => TypedAction<IteratorT<TElement>, IteratorT<TElement>>;
|
|
25
|
+
/** First n elements. `Iterator<T> → Iterator<T>` */
|
|
26
|
+
readonly take: <TElement>(n: number) => TypedAction<IteratorT<TElement>, IteratorT<TElement>>;
|
|
27
|
+
/** Drop first n elements. `Iterator<T> → Iterator<T>` */
|
|
28
|
+
readonly skip: <TElement>(n: number) => TypedAction<IteratorT<TElement>, IteratorT<TElement>>;
|
|
29
|
+
/** Check if iterator is empty. `Iterator<T> → boolean` */
|
|
30
|
+
readonly isEmpty: <TElement>() => TypedAction<IteratorT<TElement>, boolean>;
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=iterator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iterator.d.ts","sourceRoot":"","sources":["../src/iterator.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,IAAI,SAAS,EAE1B,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,QAAQ,EACb,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,EAOjB,MAAM,UAAU,CAAC;AA6ClB,eAAO,MAAM,QAAQ;IACnB,qDAAqD;yBAC3C,QAAQ,OAAK,WAAW,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC;IAOnE,4DAA4D;0BACjD,QAAQ,OAAK,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC;IAO3E,qFAAqF;0BAC1E,QAAQ,EAAE,MAAM,OAAK,WAAW,CACzC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,EACzB,SAAS,CAAC,QAAQ,CAAC,CACpB;IAOD,oDAAoD;uBAC5C,QAAQ,OAAK,WAAW,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IAIjE,0DAA0D;mBACtD,GAAG,EAAE,IAAI,UACH,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,KAC1B,WAAW,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;IAO/C,4FAA4F;uBACpF,GAAG,EAAE,IAAI,UACP,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,KAC7B,WAAW,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;IAU/C,8EAA8E;sBACvE,QAAQ,aAC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
2
|
+
import { chain } from "./chain.js";
|
|
3
|
+
import { constant, drop, flatten, getField, getIndex, identity, slice, splitFirst, splitLast, tag, } from "./builtins/index.js";
|
|
4
|
+
import { all } from "./all.js";
|
|
5
|
+
import { Option } from "./option.js";
|
|
6
|
+
import { bindInput } from "./bind.js";
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Helpers
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
/**
|
|
11
|
+
* Wrap a single value in an array. `T → T[]`
|
|
12
|
+
* Implemented as `all(identity())`. May warrant a dedicated builtin later.
|
|
13
|
+
*/
|
|
14
|
+
function wrapInArray() {
|
|
15
|
+
return all(identity());
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Normalize any IntoIterator return type to a plain array.
|
|
19
|
+
* Used inside `.flatMap()` to handle Iterator, Option, Result, and Array returns.
|
|
20
|
+
*/
|
|
21
|
+
const intoIteratorNormalize = branchFamily({
|
|
22
|
+
Iterator: branch({ Iterator: identity() }),
|
|
23
|
+
Option: branch({ Some: wrapInArray(), None: constant([]) }),
|
|
24
|
+
Result: branch({ Ok: wrapInArray(), Err: constant([]) }),
|
|
25
|
+
Array: identity(),
|
|
26
|
+
});
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Iterator namespace
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
export const Iterator = {
|
|
31
|
+
/** Wrap an array as Iterator. `T[] → Iterator<T>` */
|
|
32
|
+
fromArray() {
|
|
33
|
+
return tag("Iterator", "Iterator");
|
|
34
|
+
},
|
|
35
|
+
/** Wrap an Option as Iterator. `Option<T> → Iterator<T>` */
|
|
36
|
+
fromOption() {
|
|
37
|
+
return branch({
|
|
38
|
+
Some: chain(wrapInArray(), Iterator.fromArray()),
|
|
39
|
+
None: chain(constant([]), Iterator.fromArray()),
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
/** Wrap a Result as Iterator (Ok kept, Err dropped). `Result<T, E> → Iterator<T>` */
|
|
43
|
+
fromResult() {
|
|
44
|
+
return branch({
|
|
45
|
+
Ok: chain(wrapInArray(), Iterator.fromArray()),
|
|
46
|
+
Err: chain(constant([]), Iterator.fromArray()),
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
/** Unwrap Iterator to array. `Iterator<T> → T[]` */
|
|
50
|
+
collect() {
|
|
51
|
+
return getField("value");
|
|
52
|
+
},
|
|
53
|
+
/** Transform each element. `Iterator<T> → Iterator<U>` */
|
|
54
|
+
map(action) {
|
|
55
|
+
return chain(toAction(getField("value")), chain(toAction(forEach(action)), Iterator.fromArray()));
|
|
56
|
+
},
|
|
57
|
+
/** Flat-map each element. `f` returns any IntoIterator type. `Iterator<T> → Iterator<U>` */
|
|
58
|
+
flatMap(action) {
|
|
59
|
+
return chain(toAction(getField("value")), chain(toAction(forEach(chain(action, intoIteratorNormalize))), chain(toAction(flatten()), Iterator.fromArray())));
|
|
60
|
+
},
|
|
61
|
+
/** Keep elements where predicate returns true. `Iterator<T> → Iterator<T>` */
|
|
62
|
+
filter(predicate) {
|
|
63
|
+
return Iterator.flatMap(bindInput((element) => element
|
|
64
|
+
.then(predicate)
|
|
65
|
+
.asOption()
|
|
66
|
+
.branch({
|
|
67
|
+
Some: element.some(),
|
|
68
|
+
None: chain(drop, Option.none()),
|
|
69
|
+
})));
|
|
70
|
+
},
|
|
71
|
+
/** Head/tail decomposition. `Iterator<T> → Option<[T, Iterator<T>]>` */
|
|
72
|
+
splitFirst() {
|
|
73
|
+
return Iterator.collect()
|
|
74
|
+
.then(splitFirst())
|
|
75
|
+
.then(Option.map(all(getIndex(0).unwrap(), getIndex(1).unwrap().iterate())));
|
|
76
|
+
},
|
|
77
|
+
/** Init/last decomposition. `Iterator<T> → Option<[Iterator<T>, T]>` */
|
|
78
|
+
splitLast() {
|
|
79
|
+
return Iterator.collect()
|
|
80
|
+
.then(splitLast())
|
|
81
|
+
.then(Option.map(all(getIndex(0).unwrap().iterate(), getIndex(1).unwrap())));
|
|
82
|
+
},
|
|
83
|
+
/** Fold elements with accumulator. `Iterator<T> → TAcc` */
|
|
84
|
+
fold(init, body) {
|
|
85
|
+
return Iterator.collect().then(bindInput((elements) => all(init, elements).then(loop((recur, done) => {
|
|
86
|
+
// Re-wrap done to bridge VoidToNull<TAcc> → TAcc (TypeScript
|
|
87
|
+
// can't simplify the conditional type for generic TAcc).
|
|
88
|
+
const doneTAcc = typedAction(toAction(done));
|
|
89
|
+
// Wrap return with typedAction — branch output inference fails
|
|
90
|
+
// for generic types inside loop bodies.
|
|
91
|
+
return typedAction(toAction(bindInput((state) => {
|
|
92
|
+
const acc = state.getIndex(0).unwrap();
|
|
93
|
+
const remaining = state.getIndex(1).unwrap();
|
|
94
|
+
return remaining.splitFirst().branch({
|
|
95
|
+
None: acc.then(doneTAcc),
|
|
96
|
+
Some: bindInput((headTail) => {
|
|
97
|
+
const head = headTail.getIndex(0).unwrap();
|
|
98
|
+
const tail = headTail.getIndex(1).unwrap();
|
|
99
|
+
return all(acc, head)
|
|
100
|
+
.then(body)
|
|
101
|
+
.then(bindInput((newAcc) => all(newAcc, tail).then(recur)));
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
})));
|
|
105
|
+
}))));
|
|
106
|
+
},
|
|
107
|
+
/** Slice elements from start to end. `Iterator<T> → Iterator<T>` */
|
|
108
|
+
slice(start, end) {
|
|
109
|
+
return chain(Iterator.collect(), chain(toAction(slice(start, end)), Iterator.fromArray()));
|
|
110
|
+
},
|
|
111
|
+
/** First n elements. `Iterator<T> → Iterator<T>` */
|
|
112
|
+
take(n) {
|
|
113
|
+
return Iterator.slice(0, n);
|
|
114
|
+
},
|
|
115
|
+
/** Drop first n elements. `Iterator<T> → Iterator<T>` */
|
|
116
|
+
skip(n) {
|
|
117
|
+
return Iterator.slice(n);
|
|
118
|
+
},
|
|
119
|
+
/** Check if iterator is empty. `Iterator<T> → boolean` */
|
|
120
|
+
isEmpty() {
|
|
121
|
+
return Iterator.collect().splitFirst().isNone();
|
|
122
|
+
},
|
|
123
|
+
};
|
package/dist/option.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Option as OptionT, type Pipeable, type Result as ResultT, type TypedAction } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Option namespace. All combinators produce TypedAction AST nodes that
|
|
4
|
+
* desugar to branch + existing builtins, except collect which uses the
|
|
5
|
+
* CollectSome builtin.
|
|
6
|
+
*/
|
|
7
|
+
export declare const Option: {
|
|
8
|
+
/** Tag combinator: wrap value as `Option.Some`. `T → Option<T>` */
|
|
9
|
+
readonly some: <T>() => TypedAction<T, OptionT<T>>;
|
|
10
|
+
/** Tag combinator: wrap value as `Option.None`. `void → Option<T>` */
|
|
11
|
+
readonly none: <T>() => TypedAction<void, OptionT<T>>;
|
|
12
|
+
/** Transform the Some value. `Option<T> → Option<U>` */
|
|
13
|
+
readonly map: <T, U>(action: Pipeable<T, U>) => TypedAction<OptionT<T>, OptionT<U>>;
|
|
14
|
+
/**
|
|
15
|
+
* Monadic bind (flatMap). If Some, pass the value to action which
|
|
16
|
+
* returns Option<U>. If None, stay None. `Option<T> → Option<U>`
|
|
17
|
+
*/
|
|
18
|
+
readonly andThen: <T, U>(action: Pipeable<T, OptionT<U>>) => TypedAction<OptionT<T>, OptionT<U>>;
|
|
19
|
+
/**
|
|
20
|
+
* Extract the Some value or panic. `Option<T> → T`
|
|
21
|
+
*
|
|
22
|
+
* Panics (fatal, not caught by tryCatch) if the value is None.
|
|
23
|
+
*/
|
|
24
|
+
readonly unwrap: <T>() => TypedAction<OptionT<T>, T>;
|
|
25
|
+
/**
|
|
26
|
+
* Extract the Some value or produce a default from an action.
|
|
27
|
+
* `Option<T> → T`
|
|
28
|
+
*/
|
|
29
|
+
readonly unwrapOr: <T>(defaultAction: Pipeable<void, T>) => TypedAction<OptionT<T>, T>;
|
|
30
|
+
/**
|
|
31
|
+
* Conditional keep. If Some, pass value to predicate which returns
|
|
32
|
+
* Option<T>. If None, stay None. `Option<T> → Option<T>`
|
|
33
|
+
*/
|
|
34
|
+
readonly filter: <T>(predicate: Pipeable<T, OptionT<T>>) => TypedAction<OptionT<T>, OptionT<T>>;
|
|
35
|
+
/**
|
|
36
|
+
* Collect Some values from an array, discarding Nones.
|
|
37
|
+
* `Option<T>[] → T[]`
|
|
38
|
+
*/
|
|
39
|
+
readonly collect: <T = any>() => TypedAction<OptionT<T>[], T[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Test if the value is Some. `Option<T> → boolean`
|
|
42
|
+
*/
|
|
43
|
+
readonly isSome: <T>() => TypedAction<OptionT<T>, boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* Test if the value is None. `Option<T> → boolean`
|
|
46
|
+
*/
|
|
47
|
+
readonly isNone: <T>() => TypedAction<OptionT<T>, boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Swap Option/Result nesting.
|
|
50
|
+
* `Option<Result<TValue, TError>> → Result<Option<TValue>, TError>`
|
|
51
|
+
*
|
|
52
|
+
* - Some(Ok(t)) → Ok(Some(t))
|
|
53
|
+
* - Some(Err(e)) → Err(e)
|
|
54
|
+
* - None → Ok(None)
|
|
55
|
+
*/
|
|
56
|
+
readonly transpose: <TValue, TError>() => TypedAction<OptionT<ResultT<TValue, TError>>, ResultT<OptionT<TValue>, TError>>;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Extract the first element of an array.
|
|
60
|
+
* `readonly TElement[] → Option<TElement>`
|
|
61
|
+
*
|
|
62
|
+
* Composes `splitFirst` (which returns `Option<[TElement, TElement[]]>`)
|
|
63
|
+
* with `Option.map(getIndex(0))` to extract just the element.
|
|
64
|
+
*/
|
|
65
|
+
export declare function first<TElement>(): TypedAction<readonly TElement[], OptionT<TElement>>;
|
|
66
|
+
/**
|
|
67
|
+
* Extract the last element of an array.
|
|
68
|
+
* `readonly TElement[] → Option<TElement>`
|
|
69
|
+
*
|
|
70
|
+
* Composes `splitLast` (which returns `Option<[TElement[], TElement]>`)
|
|
71
|
+
* with `Option.map(getIndex(1))` to extract just the element.
|
|
72
|
+
*/
|
|
73
|
+
export declare function last<TElement>(): TypedAction<readonly TElement[], OptionT<TElement>>;
|
|
74
|
+
//# sourceMappingURL=option.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,IAAI,OAAO,EAEtB,KAAK,QAAQ,EACb,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,EAIjB,MAAM,UAAU,CAAC;AAkBlB;;;;GAIG;AACH,eAAO,MAAM,MAAM;IACjB,mEAAmE;oBAC9D,CAAC,OAAK,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAGrC,sEAAsE;oBACjE,CAAC,OAAK,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAIxC,wDAAwD;mBACpD,CAAC,EAAE,CAAC,UAAU,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAOtE;;;OAGG;uBACK,CAAC,EAAE,CAAC,UACF,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,KAC9B,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAOtC;;;;OAIG;sBACI,CAAC,OAAK,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAOvC;;;OAGG;wBACM,CAAC,iBAAiB,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,KAAG,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAOzE;;;OAGG;sBACI,CAAC,aACK,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,KACjC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAOtC;;;OAGG;uBACK,CAAC,aAAW,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;IAOlD;;OAEG;sBACI,CAAC,OAAK,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;IAO7C;;OAEG;sBACI,CAAC,OAAK,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC;IAO7C;;;;;;;OAOG;yBACO,MAAM,EAAE,MAAM,OAAK,WAAW,CACtC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAChC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CACjC;CAeO,CAAC;AAMX;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,QAAQ,KAAK,WAAW,CAC5C,SAAS,QAAQ,EAAE,EACnB,OAAO,CAAC,QAAQ,CAAC,CAClB,CAKA;AAMD;;;;;;GAMG;AACH,wBAAgB,IAAI,CAAC,QAAQ,KAAK,WAAW,CAC3C,SAAS,QAAQ,EAAE,EACnB,OAAO,CAAC,QAAQ,CAAC,CAClB,CAKA"}
|
package/dist/option.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { toAction, typedAction, branch, } from "./ast.js";
|
|
2
|
+
import { chain } from "./chain.js";
|
|
3
|
+
import { constant, drop, getIndex, identity, panic, splitFirst, splitLast, tag, } from "./builtins/index.js";
|
|
4
|
+
import { Result } from "./result.js";
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Option namespace — combinators for Option<T> tagged unions
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
/**
|
|
9
|
+
* Option namespace. All combinators produce TypedAction AST nodes that
|
|
10
|
+
* desugar to branch + existing builtins, except collect which uses the
|
|
11
|
+
* CollectSome builtin.
|
|
12
|
+
*/
|
|
13
|
+
export const Option = {
|
|
14
|
+
/** Tag combinator: wrap value as `Option.Some`. `T → Option<T>` */
|
|
15
|
+
some() {
|
|
16
|
+
return tag("Some", "Option");
|
|
17
|
+
},
|
|
18
|
+
/** Tag combinator: wrap value as `Option.None`. `void → Option<T>` */
|
|
19
|
+
none() {
|
|
20
|
+
return tag("None", "Option");
|
|
21
|
+
},
|
|
22
|
+
/** Transform the Some value. `Option<T> → Option<U>` */
|
|
23
|
+
map(action) {
|
|
24
|
+
return branch({
|
|
25
|
+
Some: chain(action, Option.some()),
|
|
26
|
+
None: Option.none(),
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
/**
|
|
30
|
+
* Monadic bind (flatMap). If Some, pass the value to action which
|
|
31
|
+
* returns Option<U>. If None, stay None. `Option<T> → Option<U>`
|
|
32
|
+
*/
|
|
33
|
+
andThen(action) {
|
|
34
|
+
return branch({
|
|
35
|
+
Some: action,
|
|
36
|
+
None: Option.none(),
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Extract the Some value or panic. `Option<T> → T`
|
|
41
|
+
*
|
|
42
|
+
* Panics (fatal, not caught by tryCatch) if the value is None.
|
|
43
|
+
*/
|
|
44
|
+
unwrap() {
|
|
45
|
+
return branch({
|
|
46
|
+
Some: identity(),
|
|
47
|
+
None: panic("called unwrap on None"),
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* Extract the Some value or produce a default from an action.
|
|
52
|
+
* `Option<T> → T`
|
|
53
|
+
*/
|
|
54
|
+
unwrapOr(defaultAction) {
|
|
55
|
+
return branch({
|
|
56
|
+
Some: identity(),
|
|
57
|
+
None: defaultAction,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
/**
|
|
61
|
+
* Conditional keep. If Some, pass value to predicate which returns
|
|
62
|
+
* Option<T>. If None, stay None. `Option<T> → Option<T>`
|
|
63
|
+
*/
|
|
64
|
+
filter(predicate) {
|
|
65
|
+
return branch({
|
|
66
|
+
Some: predicate,
|
|
67
|
+
None: Option.none(),
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
/**
|
|
71
|
+
* Collect Some values from an array, discarding Nones.
|
|
72
|
+
* `Option<T>[] → T[]`
|
|
73
|
+
*/
|
|
74
|
+
collect() {
|
|
75
|
+
return typedAction({
|
|
76
|
+
kind: "Invoke",
|
|
77
|
+
handler: { kind: "Builtin", builtin: { kind: "CollectSome" } },
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
/**
|
|
81
|
+
* Test if the value is Some. `Option<T> → boolean`
|
|
82
|
+
*/
|
|
83
|
+
isSome() {
|
|
84
|
+
return branch({
|
|
85
|
+
Some: constant(true),
|
|
86
|
+
None: constant(false),
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
/**
|
|
90
|
+
* Test if the value is None. `Option<T> → boolean`
|
|
91
|
+
*/
|
|
92
|
+
isNone() {
|
|
93
|
+
return branch({
|
|
94
|
+
Some: constant(false),
|
|
95
|
+
None: constant(true),
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
/**
|
|
99
|
+
* Swap Option/Result nesting.
|
|
100
|
+
* `Option<Result<TValue, TError>> → Result<Option<TValue>, TError>`
|
|
101
|
+
*
|
|
102
|
+
* - Some(Ok(t)) → Ok(Some(t))
|
|
103
|
+
* - Some(Err(e)) → Err(e)
|
|
104
|
+
* - None → Ok(None)
|
|
105
|
+
*/
|
|
106
|
+
transpose() {
|
|
107
|
+
return branch({
|
|
108
|
+
Some: branch({
|
|
109
|
+
Ok: chain(Option.some(), Result.ok()),
|
|
110
|
+
Err: Result.err(),
|
|
111
|
+
}),
|
|
112
|
+
None: chain(chain(drop, Option.none()), Result.ok()),
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// First — extract the first element of an array as Option<TElement>
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
/**
|
|
120
|
+
* Extract the first element of an array.
|
|
121
|
+
* `readonly TElement[] → Option<TElement>`
|
|
122
|
+
*
|
|
123
|
+
* Composes `splitFirst` (which returns `Option<[TElement, TElement[]]>`)
|
|
124
|
+
* with `Option.map(getIndex(0))` to extract just the element.
|
|
125
|
+
*/
|
|
126
|
+
export function first() {
|
|
127
|
+
return chain(toAction(splitFirst()), toAction(Option.map(toAction(getIndex(0).unwrap()))));
|
|
128
|
+
}
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
// Last — extract the last element of an array as Option<TElement>
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
/**
|
|
133
|
+
* Extract the last element of an array.
|
|
134
|
+
* `readonly TElement[] → Option<TElement>`
|
|
135
|
+
*
|
|
136
|
+
* Composes `splitLast` (which returns `Option<[TElement[], TElement]>`)
|
|
137
|
+
* with `Option.map(getIndex(1))` to extract just the element.
|
|
138
|
+
*/
|
|
139
|
+
export function last() {
|
|
140
|
+
return chain(toAction(splitLast()), toAction(Option.map(toAction(getIndex(1).unwrap()))));
|
|
141
|
+
}
|
package/dist/pipe.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type PipeIn, type Pipeable, type TypedAction } from "./ast.js";
|
|
2
|
+
export declare function pipe<TStep1, TStep2>(a1: Pipeable<TStep1, TStep2>): TypedAction<PipeIn<TStep1>, TStep2>;
|
|
3
|
+
export declare function pipe<TStep1, TStep2, TStep3>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>): TypedAction<PipeIn<TStep1>, TStep3>;
|
|
4
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>): TypedAction<PipeIn<TStep1>, TStep4>;
|
|
5
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>): TypedAction<PipeIn<TStep1>, TStep5>;
|
|
6
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>): TypedAction<PipeIn<TStep1>, TStep6>;
|
|
7
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6, TStep7>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>, a6: Pipeable<TStep6, TStep7>): TypedAction<PipeIn<TStep1>, TStep7>;
|
|
8
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6, TStep7, TStep8>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>, a6: Pipeable<TStep6, TStep7>, a7: Pipeable<TStep7, TStep8>): TypedAction<PipeIn<TStep1>, TStep8>;
|
|
9
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6, TStep7, TStep8, TStep9>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>, a6: Pipeable<TStep6, TStep7>, a7: Pipeable<TStep7, TStep8>, a8: Pipeable<TStep8, TStep9>): TypedAction<PipeIn<TStep1>, TStep9>;
|
|
10
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6, TStep7, TStep8, TStep9, TStep10>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>, a6: Pipeable<TStep6, TStep7>, a7: Pipeable<TStep7, TStep8>, a8: Pipeable<TStep8, TStep9>, a9: Pipeable<TStep9, TStep10>): TypedAction<PipeIn<TStep1>, TStep10>;
|
|
11
|
+
export declare function pipe<TStep1, TStep2, TStep3, TStep4, TStep5, TStep6, TStep7, TStep8, TStep9, TStep10, TStep11>(a1: Pipeable<TStep1, TStep2>, a2: Pipeable<TStep2, TStep3>, a3: Pipeable<TStep3, TStep4>, a4: Pipeable<TStep4, TStep5>, a5: Pipeable<TStep5, TStep6>, a6: Pipeable<TStep6, TStep7>, a7: Pipeable<TStep7, TStep8>, a8: Pipeable<TStep8, TStep9>, a9: Pipeable<TStep9, TStep10>, a10: Pipeable<TStep10, TStep11>): TypedAction<PipeIn<TStep1>, TStep11>;
|
|
12
|
+
//# sourceMappingURL=pipe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipe.d.ts","sourceRoot":"","sources":["../src/pipe.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,WAAW,EAEjB,MAAM,UAAU,CAAC;AAIlB,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EACjC,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,GAC3B,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;AACvC,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EACzC,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,GAC3B,WAAW,CAAC,MAAM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N,MAAM,EACN,OAAO,EACP,OAAO,EAEP,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAC5B,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,GAAG,EAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,GAC9B,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC"}
|
package/dist/pipe.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { toAction, } from "./ast.js";
|
|
2
|
+
import { chain } from "./chain.js";
|
|
3
|
+
import { identity } from "./builtins/index.js";
|
|
4
|
+
export function pipe(...actions) {
|
|
5
|
+
if (actions.length === 0) {
|
|
6
|
+
return identity();
|
|
7
|
+
}
|
|
8
|
+
if (actions.length === 1) {
|
|
9
|
+
return actions[0];
|
|
10
|
+
}
|
|
11
|
+
return actions.reduceRight((rest, first) => toAction(chain(toAction(first), toAction(rest))));
|
|
12
|
+
}
|
package/dist/race.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Pipeable, type Result as ResultT, type TypedAction } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Run multiple actions concurrently. The first to complete wins; losers
|
|
4
|
+
* are cancelled during `RestartHandle` frame teardown.
|
|
5
|
+
*
|
|
6
|
+
* All branches must have the same input and output type (since either
|
|
7
|
+
* could win).
|
|
8
|
+
*
|
|
9
|
+
* Compiled form (restart+Branch, same substrate as loop/earlyReturn):
|
|
10
|
+
* `Chain(Tag("Continue"),`
|
|
11
|
+
* `RestartHandle(id, GetIndex(0),`
|
|
12
|
+
* `Branch({`
|
|
13
|
+
* `Continue: All(Chain(a, breakPerform), Chain(b, breakPerform), ...),`
|
|
14
|
+
* `Break: identity,`
|
|
15
|
+
* `})))`
|
|
16
|
+
*
|
|
17
|
+
* First branch to complete tags Break → `RestartPerform` → handler restarts →
|
|
18
|
+
* Branch takes Break arm → identity → `RestartHandle` exits with winner's value.
|
|
19
|
+
*/
|
|
20
|
+
export declare function race<TIn, TOut>(...actions: Pipeable<TIn, TOut>[]): TypedAction<TIn, TOut>;
|
|
21
|
+
/**
|
|
22
|
+
* Sleep for a fixed duration, ignoring input and returning void.
|
|
23
|
+
*
|
|
24
|
+
* `ms` is baked into the AST at construction time. Executed by the Rust
|
|
25
|
+
* scheduler via `tokio::time::sleep` — no subprocess spawned.
|
|
26
|
+
*
|
|
27
|
+
* To preserve data across a sleep, use `bindInput`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sleep(ms: number): TypedAction<any, void>;
|
|
30
|
+
/**
|
|
31
|
+
* @internal TypeScript handler that takes ms as pipeline input and returns
|
|
32
|
+
* void after the timer fires. Used by `withTimeout` where the duration
|
|
33
|
+
* comes from a runtime pipeline, not a build-time constant.
|
|
34
|
+
*/
|
|
35
|
+
export declare function dynamicSleep(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Race the body against a sleep timer. Returns `Result<TOut, void>`:
|
|
38
|
+
* - Ok(value) if the body completed first
|
|
39
|
+
* - Err(void) if the timeout fired first
|
|
40
|
+
*
|
|
41
|
+
* The `ms` parameter is an AST node that evaluates to the timeout duration
|
|
42
|
+
* in milliseconds. Use `constant(5000)` for a fixed timeout, or any action
|
|
43
|
+
* that computes a duration from the pipeline input.
|
|
44
|
+
*
|
|
45
|
+
* Built as raw AST rather than through `race()` because each branch wraps
|
|
46
|
+
* its result differently (Ok vs Err) before the Break+Perform. `race()`
|
|
47
|
+
* requires homogeneous output types, but withTimeout needs heterogeneous
|
|
48
|
+
* tagging.
|
|
49
|
+
*
|
|
50
|
+
* Same restart+Branch substrate as race: each branch tags Break after
|
|
51
|
+
* wrapping its result as Ok or Err.
|
|
52
|
+
*/
|
|
53
|
+
export declare function withTimeout<TIn, TOut>(ms: Pipeable<TIn, number>, body: Pipeable<TIn, TOut>): TypedAction<TIn, ResultT<TOut, void>>;
|
|
54
|
+
//# sourceMappingURL=race.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"race.d.ts","sourceRoot":"","sources":["../src/race.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,EAIjB,MAAM,UAAU,CAAC;AA2BlB;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,IAAI,EAC5B,GAAG,OAAO,EAAE,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,GAChC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAaxB;AAMD;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAKxD;AAgBD;;;;GAIG;AAEH,wBAAgB,YAAY,IAAI,IAAI,CAAG;AAavC;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,IAAI,EACnC,EAAE,EAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,EACzB,IAAI,EAAE,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,GACxB,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CA8BvC"}
|
package/dist/race.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { toAction, typedAction, buildRestartBranchAction, } from "./ast.js";
|
|
2
|
+
import { chain } from "./chain.js";
|
|
3
|
+
import { identity, tag } from "./builtins/index.js";
|
|
4
|
+
import { Result } from "./result.js";
|
|
5
|
+
import { allocateRestartHandlerId, } from "./effect-id.js";
|
|
6
|
+
/**
|
|
7
|
+
* `Chain(Tag("Break"), RestartPerform(id))` — shared by race branches.
|
|
8
|
+
* The winning branch tags its result as Break, then performs. The handler
|
|
9
|
+
* restarts the body; Branch takes the Break arm (identity), `RestartHandle` exits.
|
|
10
|
+
*/
|
|
11
|
+
function breakPerform(restartHandlerId) {
|
|
12
|
+
return toAction(chain(toAction(tag("Break", "LoopResult")), {
|
|
13
|
+
kind: "RestartPerform",
|
|
14
|
+
restart_handler_id: restartHandlerId,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// race — first branch to complete wins, losers cancelled
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
/**
|
|
21
|
+
* Run multiple actions concurrently. The first to complete wins; losers
|
|
22
|
+
* are cancelled during `RestartHandle` frame teardown.
|
|
23
|
+
*
|
|
24
|
+
* All branches must have the same input and output type (since either
|
|
25
|
+
* could win).
|
|
26
|
+
*
|
|
27
|
+
* Compiled form (restart+Branch, same substrate as loop/earlyReturn):
|
|
28
|
+
* `Chain(Tag("Continue"),`
|
|
29
|
+
* `RestartHandle(id, GetIndex(0),`
|
|
30
|
+
* `Branch({`
|
|
31
|
+
* `Continue: All(Chain(a, breakPerform), Chain(b, breakPerform), ...),`
|
|
32
|
+
* `Break: identity,`
|
|
33
|
+
* `})))`
|
|
34
|
+
*
|
|
35
|
+
* First branch to complete tags Break → `RestartPerform` → handler restarts →
|
|
36
|
+
* Branch takes Break arm → identity → `RestartHandle` exits with winner's value.
|
|
37
|
+
*/
|
|
38
|
+
export function race(...actions) {
|
|
39
|
+
const restartHandlerId = allocateRestartHandlerId();
|
|
40
|
+
const perform = breakPerform(restartHandlerId);
|
|
41
|
+
const branches = actions.map((action) => toAction(chain(toAction(action), toAction(perform))));
|
|
42
|
+
const allAction = { kind: "All", actions: branches };
|
|
43
|
+
return typedAction(buildRestartBranchAction(restartHandlerId, allAction, toAction(identity())));
|
|
44
|
+
}
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// sleep — Rust builtin that delays for a fixed duration (passthrough)
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Sleep for a fixed duration, ignoring input and returning void.
|
|
50
|
+
*
|
|
51
|
+
* `ms` is baked into the AST at construction time. Executed by the Rust
|
|
52
|
+
* scheduler via `tokio::time::sleep` — no subprocess spawned.
|
|
53
|
+
*
|
|
54
|
+
* To preserve data across a sleep, use `bindInput`.
|
|
55
|
+
*/
|
|
56
|
+
export function sleep(ms) {
|
|
57
|
+
return typedAction({
|
|
58
|
+
kind: "Invoke",
|
|
59
|
+
handler: { kind: "Builtin", builtin: { kind: "Sleep", ms } },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// dynamicSleep — TypeScript handler for withTimeout (takes ms as input)
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
/** The raw Invoke node for the dynamic sleep handler. */
|
|
66
|
+
const DYNAMIC_SLEEP_INVOKE = {
|
|
67
|
+
kind: "Invoke",
|
|
68
|
+
handler: {
|
|
69
|
+
kind: "TypeScript",
|
|
70
|
+
module: import.meta.url,
|
|
71
|
+
func: "dynamicSleep",
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @internal TypeScript handler that takes ms as pipeline input and returns
|
|
76
|
+
* void after the timer fires. Used by `withTimeout` where the duration
|
|
77
|
+
* comes from a runtime pipeline, not a build-time constant.
|
|
78
|
+
*/
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
80
|
+
export function dynamicSleep() { }
|
|
81
|
+
Object.defineProperty(dynamicSleep, "__definition", {
|
|
82
|
+
value: {
|
|
83
|
+
handle: ({ value }) => new Promise((resolve) => setTimeout(resolve, value)),
|
|
84
|
+
},
|
|
85
|
+
enumerable: false,
|
|
86
|
+
});
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// withTimeout — race body against sleep, return Result
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
/**
|
|
91
|
+
* Race the body against a sleep timer. Returns `Result<TOut, void>`:
|
|
92
|
+
* - Ok(value) if the body completed first
|
|
93
|
+
* - Err(void) if the timeout fired first
|
|
94
|
+
*
|
|
95
|
+
* The `ms` parameter is an AST node that evaluates to the timeout duration
|
|
96
|
+
* in milliseconds. Use `constant(5000)` for a fixed timeout, or any action
|
|
97
|
+
* that computes a duration from the pipeline input.
|
|
98
|
+
*
|
|
99
|
+
* Built as raw AST rather than through `race()` because each branch wraps
|
|
100
|
+
* its result differently (Ok vs Err) before the Break+Perform. `race()`
|
|
101
|
+
* requires homogeneous output types, but withTimeout needs heterogeneous
|
|
102
|
+
* tagging.
|
|
103
|
+
*
|
|
104
|
+
* Same restart+Branch substrate as race: each branch tags Break after
|
|
105
|
+
* wrapping its result as Ok or Err.
|
|
106
|
+
*/
|
|
107
|
+
export function withTimeout(ms, body) {
|
|
108
|
+
const restartHandlerId = allocateRestartHandlerId();
|
|
109
|
+
const perform = breakPerform(restartHandlerId);
|
|
110
|
+
// Branch 1: body → Tag("Ok") → Break → RestartPerform
|
|
111
|
+
const bodyBranch = toAction(chain(toAction(chain(toAction(body), toAction(Result.ok()))), toAction(perform)));
|
|
112
|
+
// Branch 2: ms → sleep() → Tag("Err") → Break → RestartPerform
|
|
113
|
+
const sleepBranch = toAction(chain(toAction(chain(toAction(chain(toAction(ms), toAction(DYNAMIC_SLEEP_INVOKE))), toAction(Result.err()))), toAction(perform)));
|
|
114
|
+
const allAction = { kind: "All", actions: [bodyBranch, sleepBranch] };
|
|
115
|
+
return typedAction(buildRestartBranchAction(restartHandlerId, allAction, toAction(identity())));
|
|
116
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Action, type Pipeable, type TypedAction } from "./ast.js";
|
|
2
|
+
type FunctionDef = [input: unknown, output: unknown];
|
|
3
|
+
type FunctionRefs<TDefs extends FunctionDef[]> = {
|
|
4
|
+
[K in keyof TDefs]: TypedAction<TDefs[K][0], TDefs[K][1]>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Constraint for the entry-point callback return type. Only requires the
|
|
8
|
+
* output phantom field — omits __in and __in_co so that actions with
|
|
9
|
+
* any input type (e.g. pipelines starting from a call token) are assignable.
|
|
10
|
+
*/
|
|
11
|
+
type BodyResult<TOut> = Action & {
|
|
12
|
+
__out?: () => TOut;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Define mutually recursive functions that can call each other.
|
|
16
|
+
*
|
|
17
|
+
* The type parameter is an array of [In, Out] tuples — one per function.
|
|
18
|
+
* TypeScript can't infer these from circular definitions, so they must be
|
|
19
|
+
* explicit.
|
|
20
|
+
*
|
|
21
|
+
* Returns a curried combinator: the first callback defines function bodies,
|
|
22
|
+
* the second receives the same call tokens and returns the workflow entry
|
|
23
|
+
* point.
|
|
24
|
+
*
|
|
25
|
+
* Desugars to a ResumeHandle with a Branch-based handler. Each call token
|
|
26
|
+
* is Chain(Tag("CallN"), ResumePerform(id)). The handler dispatches to the
|
|
27
|
+
* correct function body by tag. The caller's pipeline is preserved as a
|
|
28
|
+
* ResumePerformFrame across each call.
|
|
29
|
+
*
|
|
30
|
+
* **Known limitation:** concurrent calls to the same function do not work
|
|
31
|
+
* as expected. `all(f(x), f(x))` will NOT call `f` twice — both branches
|
|
32
|
+
* perform on the same ResumeHandle, and the second perform will not
|
|
33
|
+
* execute independently. Use sequential calls (chain/then) instead of
|
|
34
|
+
* concurrent calls (all) when calling recursive functions multiple times.
|
|
35
|
+
*/
|
|
36
|
+
export declare function defineRecursiveFunctions<TDefs extends FunctionDef[]>(bodiesFn: (...fns: FunctionRefs<TDefs>) => {
|
|
37
|
+
[K in keyof TDefs]: Pipeable<TDefs[K][0], TDefs[K][1]>;
|
|
38
|
+
}): <TOut>(entryFn: (...fns: FunctionRefs<TDefs>) => BodyResult<TOut>) => TypedAction<any, TOut>;
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=recursive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recursive.d.ts","sourceRoot":"","sources":["../src/recursive.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,WAAW,EAIjB,MAAM,UAAU,CAAC;AAgBlB,KAAK,WAAW,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAErD,KAAK,YAAY,CAAC,KAAK,SAAS,WAAW,EAAE,IAAI;KAC9C,CAAC,IAAI,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC1D,CAAC;AAEF;;;;GAIG;AACH,KAAK,UAAU,CAAC,IAAI,IAAI,MAAM,GAAG;IAC/B,KAAK,CAAC,EAAE,MAAM,IAAI,CAAC;CACpB,CAAC;AAQF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,SAAS,WAAW,EAAE,EAClE,QAAQ,EAAE,CAAC,GAAG,GAAG,EAAE,YAAY,CAAC,KAAK,CAAC,KAAK;KACxC,CAAC,IAAI,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACvD,GACA,CAAC,IAAI,EACN,OAAO,EAAE,CAAC,GAAG,GAAG,EAAE,YAAY,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,KACvD,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAwD1B"}
|