/
api.ts
82 lines (73 loc) · 2 KB
/
api.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
import type { Comparator, Fn, IObjectOf } from "@thi.ng/api";
import type { Reduced } from "./reduced";
export type Transducer<A, B> = (rfn: Reducer<any, B>) => Reducer<any, A>;
export type TxLike<A, B> = Transducer<A, B> | IXform<A, B>;
export type ReductionFn<A, B> = (acc: A, x: B) => A | Reduced<A>;
export interface Reducer<A, B> extends Array<any> {
[0]: () => A;
[1]: (acc: A) => A;
[2]: ReductionFn<A, B>;
}
/**
* Interface for types able to provide some internal functionality (or
* derive some related transformation) as {@link Transducer}.
* Implementations of this interface can be directly passed to all
* functions in this package where a `Transducer` arg is expected.
*
* @example
* ```
* class Mul implements IXform<number, number> {
* constructor(public factor = 10) {}
*
* xform() { return map((x) => this.factor * x); }
* }
*
* transduce(new Mul(11), push(), range(4))
* // [0, 11, 22, 33, 44]
*
* // also usable w/ comp()
* transduce(
* comp(
* drop(1),
* new Mul(11),
* takeNth(2)
* ),
* push(),
* range(4)
* )
* // [11, 33]
* ```
*/
export interface IXform<A, B> {
/**
* Returns type specific operation as transducer. Internally called
* by functions in this package which expect transducer args. Users
* don't need to call this manually.
*/
xform(): Transducer<A, B>;
}
export interface IReducible<A, B> {
$reduce(rfn: ReductionFn<A, B>, acc: A): A | Reduced<A>;
}
export type TransformFn = (x: any) => any;
export type TransformSubSpec = IObjectOf<TransformSpec | TransformFn>;
export interface TransformSpec extends Array<any> {
[0]: TransformFn;
[1]?: TransformSubSpec;
}
export interface SortOpts<A, B> {
/**
* Sort key lookup function.
* Default: `identity`
*/
key: Fn<A, B>;
/**
* Comparator.
* Default: `thi.ng/compare/compare`
*/
compare: Comparator<B>;
}
export interface GroupByOpts<SRC, KEY, GROUP> {
key: Fn<SRC, KEY>;
group: Reducer<GROUP, SRC>;
}