-
Notifications
You must be signed in to change notification settings - Fork 7
/
filterable.ts
217 lines (210 loc) · 5.35 KB
/
filterable.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
/**
* Filterable is a structure that allows one to remove or refine a data
* structure.
*
* @module Filterable
* @since 2.0.0
*/
import type { $, Hold, Kind } from "./kind.ts";
import type { Either } from "./either.ts";
import type { Mappable } from "./mappable.ts";
import type { Option } from "./option.ts";
import type { Pair } from "./pair.ts";
import type { Predicate } from "./predicate.ts";
import type { Refinement } from "./refinement.ts";
import { getLeft, getRight } from "./either.ts";
import { flow, pipe } from "./fn.ts";
/**
* A Filterable structure allows one to filter over the values contained in the
* structure. This includes standard filter, filterMap, partition, and
* partitionMap.
*
* @since 2.0.0
*/
export interface Filterable<U extends Kind> extends Hold<U> {
readonly filter: {
<A, I extends A>(
refinement: Refinement<A, I>,
): <B = never, C = never, D = unknown, E = unknown>(
ta: $<U, [A, B, C], [D], [E]>,
) => $<U, [I, B, C], [D], [E]>;
<A>(
predicate: Predicate<A>,
): <B = never, C = never, D = unknown, E = unknown>(
ta: $<U, [A, B, C], [D], [E]>,
) => $<U, [A, B, C], [D], [E]>;
};
readonly filterMap: <A, I>(
fai: (a: A) => Option<I>,
) => <B = never, C = never, D = unknown, E = unknown>(
ua: $<U, [A, B, C], [D], [E]>,
) => $<U, [I, B, C], [D], [E]>;
readonly partition: {
<A, I extends A>(
refinement: Refinement<A, I>,
): <B = never, C = never, D = unknown, E = unknown>(
ta: $<U, [A, B, C], [D], [E]>,
) => Pair<$<U, [I, B, C], [D], [E]>, $<U, [A, B, C], [D], [E]>>;
<A>(
predicate: Predicate<A>,
): <B = never, C = never, D = unknown, E = unknown>(
ta: $<U, [A, B, C], [D], [E]>,
) => Pair<$<U, [A, B, C], [D], [E]>, $<U, [A, B, C], [D], [E]>>;
};
readonly partitionMap: <A, I, J>(
fai: (a: A) => Either<J, I>,
) => <B = never, C = never, D = unknown, E = unknown>(
ua: $<U, [A, B, C], [D], [E]>,
) => Pair<$<U, [I, B, C], [D], [E]>, $<U, [J, B, C], [D], [E]>>;
}
/**
* @since 2.0.0
*/
export function filter<U extends Kind, V extends Kind>(
{ map }: Mappable<U>,
{ filter }: Filterable<V>,
): <A>(
predicate: Predicate<A>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]> {
return (predicate) => (uva) => pipe(uva, map(filter(predicate)));
}
/**
* @since 2.0.0
*/
export function filterMap<U extends Kind, V extends Kind>(
{ map }: Mappable<U>,
{ filterMap }: Filterable<V>,
): <A, I>(
predicate: (a: A) => Option<I>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => $<U, [$<V, [I, B, C], [D], [E]>, J, K], [L], [M]> {
return (predicate) => (uva) => pipe(uva, map(filterMap(predicate)));
}
/**
* @since 2.0.0
*/
export function partition<U extends Kind, V extends Kind>(
M: Mappable<U>,
F: Filterable<V>,
): <A, I extends A>(
predicate: Refinement<A, I>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => Pair<
$<U, [$<V, [I, B, C], [D], [E]>, J, K], [L], [M]>,
$<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>
>;
export function partition<U extends Kind, V extends Kind>(
M: Mappable<U>,
F: Filterable<V>,
): <A>(
predicate: Predicate<A>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => Pair<
$<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
$<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>
>;
export function partition<U extends Kind, V extends Kind>(
M: Mappable<U>,
F: Filterable<V>,
): <A>(
predicate: Predicate<A>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => Pair<
$<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
$<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>
> {
const _filter = filter(M, F);
// TODO: Figure this overload out :/
return (predicate) => {
// deno-lint-ignore no-explicit-any
const first = _filter(predicate as any);
// deno-lint-ignore no-explicit-any
const second = _filter(((a: any) => !predicate(a)) as any);
// deno-lint-ignore no-explicit-any
return (uva) => [first(uva), second(uva)] as any;
};
}
/**
* @since 2.0.0
*/
export function partitionMap<U extends Kind, V extends Kind>(
M: Mappable<U>,
F: Filterable<V>,
): <A, X, Y>(
fai: (a: A) => Either<X, Y>,
) => <
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
L = unknown,
M = unknown,
>(
ua: $<U, [$<V, [A, B, C], [D], [E]>, J, K], [L], [M]>,
) => Pair<
$<U, [$<V, [Y, B, C], [D], [E]>, J, K], [L], [M]>,
$<U, [$<V, [X, B, C], [D], [E]>, J, K], [L], [M]>
> {
const _filterMap = filterMap(M, F);
return (predicate) => {
const first = flow(predicate, getRight);
const second = flow(predicate, getLeft);
return (uva) => [
pipe(uva, _filterMap(first)),
pipe(uva, _filterMap(second)),
];
};
}