-
-
Notifications
You must be signed in to change notification settings - Fork 503
/
Functor.ts
355 lines (326 loc) · 10.9 KB
/
Functor.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
/**
* A `Functor` is a type constructor which supports a mapping operation `map`.
*
* `map` can be used to turn functions `a -> b` into functions `f a -> f b` whose argument and return types use the type
* constructor `f` to represent some computational context.
*
* Instances must satisfy the following laws:
*
* 1. Identity: `F.map(fa, a => a) <-> fa`
* 2. Composition: `F.map(fa, a => bc(ab(a))) <-> F.map(F.map(fa, ab), bc)`
*
* @since 2.0.0
*/
import { pipe } from './function'
import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor<F> {
readonly URI: F
readonly map: <A, B>(fa: HKT<F, A>, f: (a: A) => B) => HKT<F, B>
}
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor1<F extends URIS> {
readonly URI: F
readonly map: <A, B>(fa: Kind<F, A>, f: (a: A) => B) => Kind<F, B>
}
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor2<F extends URIS2> {
readonly URI: F
readonly map: <E, A, B>(fa: Kind2<F, E, A>, f: (a: A) => B) => Kind2<F, E, B>
}
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor2C<F extends URIS2, E> {
readonly URI: F
readonly _E: E
readonly map: <A, B>(fa: Kind2<F, E, A>, f: (a: A) => B) => Kind2<F, E, B>
}
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor3<F extends URIS3> {
readonly URI: F
readonly map: <R, E, A, B>(fa: Kind3<F, R, E, A>, f: (a: A) => B) => Kind3<F, R, E, B>
}
/**
* @category type classes
* @since 2.2.0
*/
export interface Functor3C<F extends URIS3, E> {
readonly URI: F
readonly _E: E
readonly map: <R, A, B>(fa: Kind3<F, R, E, A>, f: (a: A) => B) => Kind3<F, R, E, B>
}
/**
* @category type classes
* @since 2.0.0
*/
export interface Functor4<F extends URIS4> {
readonly URI: F
readonly map: <S, R, E, A, B>(fa: Kind4<F, S, R, E, A>, f: (a: A) => B) => Kind4<F, S, R, E, B>
}
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* `map` composition.
*
* @category combinators
* @since 2.10.0
*/
export function map<F extends URIS3, G extends URIS>(
F: Functor3<F>,
G: Functor1<G>
): <A, B>(f: (a: A) => B) => <R, E>(fa: Kind3<F, R, E, Kind<G, A>>) => Kind3<F, R, E, Kind<G, B>>
export function map<F extends URIS2, G extends URIS2>(
F: Functor2<F>,
G: Functor2<G>
): <A, B>(f: (a: A) => B) => <EF, EG>(fa: Kind2<F, EF, Kind2<G, EG, A>>) => Kind2<F, EF, Kind2<G, EG, B>>
export function map<F extends URIS2, G extends URIS>(
F: Functor2<F>,
G: Functor1<G>
): <A, B>(f: (a: A) => B) => <E>(fa: Kind2<F, E, Kind<G, A>>) => Kind2<F, E, Kind<G, B>>
export function map<F extends URIS, G extends URIS3>(
F: Functor1<F>,
G: Functor3<G>
): <A, B>(f: (a: A) => B) => <R, E>(fa: Kind<F, Kind3<G, R, E, A>>) => Kind<F, Kind3<G, R, E, B>>
export function map<F extends URIS, G extends URIS2>(
F: Functor1<F>,
G: Functor2<G>
): <A, B>(f: (a: A) => B) => <E>(fa: Kind<F, Kind2<G, E, A>>) => Kind<F, Kind2<G, E, B>>
export function map<F extends URIS, G extends URIS>(
F: Functor1<F>,
G: Functor1<G>
): <A, B>(f: (a: A) => B) => (fa: Kind<F, Kind<G, A>>) => Kind<F, Kind<G, B>>
export function map<F, G extends URIS2>(
F: Functor<F>,
G: Functor2<G>
): <A, B>(f: (a: A) => B) => <E>(fa: HKT<F, Kind2<G, E, A>>) => HKT<F, Kind2<G, E, B>>
export function map<F, G extends URIS>(
F: Functor<F>,
G: Functor1<G>
): <A, B>(f: (a: A) => B) => (fa: HKT<F, Kind<G, A>>) => HKT<F, Kind<G, B>>
export function map<F, G>(
F: Functor<F>,
G: Functor<G>
): <A, B>(f: (a: A) => B) => (fa: HKT<F, HKT<G, A>>) => HKT<F, HKT<G, B>>
export function map<F, G>(
F: Functor<F>,
G: Functor<G>
): <A, B>(f: (a: A) => B) => (fa: HKT<F, HKT<G, A>>) => HKT<F, HKT<G, B>> {
return (f) => (fa) => F.map(fa, (ga) => G.map(ga, f))
}
/**
* @category combinators
* @since 2.10.0
*/
export function flap<F extends URIS4>(
F: Functor4<F>
): <A>(a: A) => <S, R, E, B>(fab: Kind4<F, S, R, E, (a: A) => B>) => Kind4<F, S, R, E, B>
export function flap<F extends URIS3>(
F: Functor3<F>
): <A>(a: A) => <R, E, B>(fab: Kind3<F, R, E, (a: A) => B>) => Kind3<F, R, E, B>
export function flap<F extends URIS2>(
F: Functor2<F>
): <A>(a: A) => <E, B>(fab: Kind2<F, E, (a: A) => B>) => Kind2<F, E, B>
export function flap<F extends URIS>(F: Functor1<F>): <A>(a: A) => <B>(fab: Kind<F, (a: A) => B>) => Kind<F, B>
export function flap<F>(F: Functor<F>): <A>(a: A) => <B>(fab: HKT<F, (a: A) => B>) => HKT<F, B>
export function flap<F>(F: Functor<F>): <A>(a: A) => <B>(fab: HKT<F, (a: A) => B>) => HKT<F, B> {
return (a) => (fab) => F.map(fab, (f) => f(a))
}
// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
/**
* @since 2.10.0
*/
export function bindTo<F extends URIS4>(
F: Functor4<F>
): <N extends string>(name: N) => <S, R, E, A>(fa: Kind4<F, S, R, E, A>) => Kind4<F, S, R, E, { readonly [K in N]: A }>
export function bindTo<F extends URIS3>(
F: Functor3<F>
): <N extends string>(name: N) => <R, E, A>(fa: Kind3<F, R, E, A>) => Kind3<F, R, E, { readonly [K in N]: A }>
export function bindTo<F extends URIS3, E>(
F: Functor3C<F, E>
): <N extends string>(name: N) => <R, A>(fa: Kind3<F, R, E, A>) => Kind3<F, R, E, { readonly [K in N]: A }>
export function bindTo<F extends URIS2>(
F: Functor2<F>
): <N extends string>(name: N) => <E, A>(fa: Kind2<F, E, A>) => Kind2<F, E, { readonly [K in N]: A }>
export function bindTo<F extends URIS2, E>(
F: Functor2C<F, E>
): <N extends string>(name: N) => <A>(fa: Kind2<F, E, A>) => Kind2<F, E, { readonly [K in N]: A }>
export function bindTo<F extends URIS>(
F: Functor1<F>
): <N extends string>(name: N) => <A>(fa: Kind<F, A>) => Kind<F, { readonly [K in N]: A }>
export function bindTo<F>(
F: Functor<F>
): <N extends string>(name: N) => <A>(fa: HKT<F, A>) => HKT<F, { readonly [K in N]: A }>
export function bindTo<F>(
F: Functor<F>
): <N extends string>(name: N) => <A>(fa: HKT<F, A>) => HKT<F, { readonly [K in N]: A }> {
return (name) => (fa) => F.map(fa, (a) => ({ [name]: a } as any))
}
// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
// tslint:disable: deprecation
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition<F, G> {
readonly map: <A, B>(fa: HKT<F, HKT<G, A>>, f: (a: A) => B) => HKT<F, HKT<G, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorCompositionHKT1<F, G extends URIS> {
readonly map: <A, B>(fa: HKT<F, Kind<G, A>>, f: (a: A) => B) => HKT<F, Kind<G, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorCompositionHKT2<F, G extends URIS2> {
readonly map: <E, A, B>(fa: HKT<F, Kind2<G, E, A>>, f: (a: A) => B) => HKT<F, Kind2<G, E, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorCompositionHKT2C<F, G extends URIS2, E> {
readonly map: <A, B>(fa: HKT<F, Kind2<G, E, A>>, f: (a: A) => B) => HKT<F, Kind2<G, E, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition11<F extends URIS, G extends URIS> {
readonly map: <A, B>(fa: Kind<F, Kind<G, A>>, f: (a: A) => B) => Kind<F, Kind<G, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition12<F extends URIS, G extends URIS2> {
readonly map: <E, A, B>(fa: Kind<F, Kind2<G, E, A>>, f: (a: A) => B) => Kind<F, Kind2<G, E, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition12C<F extends URIS, G extends URIS2, E> {
readonly map: <A, B>(fa: Kind<F, Kind2<G, E, A>>, f: (a: A) => B) => Kind<F, Kind2<G, E, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition21<F extends URIS2, G extends URIS> {
readonly map: <E, A, B>(fa: Kind2<F, E, Kind<G, A>>, f: (a: A) => B) => Kind2<F, E, Kind<G, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition2C1<F extends URIS2, G extends URIS, E> {
readonly map: <A, B>(fa: Kind2<F, E, Kind<G, A>>, f: (a: A) => B) => Kind2<F, E, Kind<G, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition22<F extends URIS2, G extends URIS2> {
readonly map: <FE, GE, A, B>(fa: Kind2<F, FE, Kind2<G, GE, A>>, f: (a: A) => B) => Kind2<F, FE, Kind2<G, GE, B>>
}
/**
* @since 2.0.0
* @deprecated
*/
export interface FunctorComposition22C<F extends URIS2, G extends URIS2, E> {
readonly map: <FE, A, B>(fa: Kind2<F, FE, Kind2<G, E, A>>, f: (a: A) => B) => Kind2<F, FE, Kind2<G, E, B>>
}
/**
* @since 2.2.0
* @deprecated
*/
export interface FunctorComposition23<F extends URIS2, G extends URIS3> {
readonly map: <FE, R, E, A, B>(fa: Kind2<F, FE, Kind3<G, R, E, A>>, f: (a: A) => B) => Kind2<F, FE, Kind3<G, R, E, B>>
}
/**
* @since 2.2.0
* @deprecated
*/
export interface FunctorComposition23C<F extends URIS2, G extends URIS3, E> {
readonly map: <FE, R, A, B>(fa: Kind2<F, FE, Kind3<G, R, E, A>>, f: (a: A) => B) => Kind2<F, FE, Kind3<G, R, E, B>>
}
/**
* Use [`map`](#map) instead.
*
* @since 2.0.0
* @deprecated
*/
export function getFunctorComposition<F extends URIS2, G extends URIS3, E>(
F: Functor2<F>,
G: Functor3C<G, E>
): FunctorComposition23C<F, G, E>
/** @deprecated */
export function getFunctorComposition<F extends URIS2, G extends URIS2, E>(
F: Functor2<F>,
G: Functor2C<G, E>
): FunctorComposition22C<F, G, E>
/** @deprecated */
export function getFunctorComposition<F extends URIS2, G extends URIS2>(
F: Functor2<F>,
G: Functor2<G>
): FunctorComposition22<F, G>
/** @deprecated */
export function getFunctorComposition<F extends URIS2, G extends URIS, E>(
F: Functor2C<F, E>,
G: Functor1<G>
): FunctorComposition2C1<F, G, E>
/** @deprecated */
export function getFunctorComposition<F extends URIS2, G extends URIS>(
F: Functor2<F>,
G: Functor1<G>
): FunctorComposition21<F, G>
/** @deprecated */
export function getFunctorComposition<F extends URIS, G extends URIS2, E>(
F: Functor1<F>,
G: Functor2C<G, E>
): FunctorComposition12C<F, G, E>
/** @deprecated */
export function getFunctorComposition<F extends URIS, G extends URIS2>(
F: Functor1<F>,
G: Functor2<G>
): FunctorComposition12<F, G>
/** @deprecated */
export function getFunctorComposition<F extends URIS, G extends URIS>(
F: Functor1<F>,
G: Functor1<G>
): FunctorComposition11<F, G>
/** @deprecated */
export function getFunctorComposition<F, G>(F: Functor<F>, G: Functor<G>): FunctorComposition<F, G>
/** @deprecated */
export function getFunctorComposition<F, G>(F: Functor<F>, G: Functor<G>): FunctorComposition<F, G> {
const _map = map(F, G)
return {
map: (fga, f) => pipe(fga, _map(f))
}
}