-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Apply.ts
235 lines (220 loc) · 7.83 KB
/
Apply.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
/**
* The `Apply` class provides the `ap` which is used to apply a function to an argument under a type constructor.
*
* `Apply` can be used to lift functions of two or more arguments to work on values wrapped with the type constructor
* `f`.
*
* Instances must satisfy the following law in addition to the `Functor` laws:
*
* 1. Associative composition: `F.ap(F.ap(F.map(fbc, bc => ab => a => bc(ab(a))), fab), fa) = F.ap(fbc, F.ap(fab, fa))`
*
* Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
*
* @since 2.0.0
*/
import { Functor, Functor1, Functor2, Functor2C, Functor3, Functor4, Functor3C } from './Functor'
import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT'
import { tuple } from './function'
/**
* @since 2.0.0
*/
export interface Apply<F> extends Functor<F> {
readonly ap: <A, B>(fab: HKT<F, (a: A) => B>, fa: HKT<F, A>) => HKT<F, B>
}
/**
* @since 2.0.0
*/
export interface Apply1<F extends URIS> extends Functor1<F> {
readonly ap: <A, B>(fab: Kind<F, (a: A) => B>, fa: Kind<F, A>) => Kind<F, B>
}
/**
* @since 2.0.0
*/
export interface Apply2<F extends URIS2> extends Functor2<F> {
readonly ap: <E, A, B>(fab: Kind2<F, E, (a: A) => B>, fa: Kind2<F, E, A>) => Kind2<F, E, B>
}
/**
* @since 2.0.0
*/
export interface Apply2C<F extends URIS2, E> extends Functor2C<F, E> {
readonly ap: <A, B>(fab: Kind2<F, E, (a: A) => B>, fa: Kind2<F, E, A>) => Kind2<F, E, B>
}
/**
* @since 2.0.0
*/
export interface Apply3<F extends URIS3> extends Functor3<F> {
readonly ap: <R, E, A, B>(fab: Kind3<F, R, E, (a: A) => B>, fa: Kind3<F, R, E, A>) => Kind3<F, R, E, B>
}
/**
* @since 2.2.0
*/
export interface Apply3C<F extends URIS3, E> extends Functor3C<F, E> {
readonly ap: <R, A, B>(fab: Kind3<F, R, E, (a: A) => B>, fa: Kind3<F, R, E, A>) => Kind3<F, R, E, B>
}
/**
* @since 2.0.0
*/
export interface Apply4<F extends URIS4> extends Functor4<F> {
readonly ap: <S, R, E, A, B>(fab: Kind4<F, S, R, E, (a: A) => B>, fa: Kind4<F, S, R, E, A>) => Kind4<F, S, R, E, B>
}
function curried(f: Function, n: number, acc: Array<unknown>) {
return function(x: unknown) {
const combined = acc.concat([x])
return n === 0 ? f.apply(null, combined) : curried(f, n - 1, combined)
}
}
const tupleConstructors: Record<number, (a: unknown) => unknown> = {}
function getTupleConstructor(len: number): (a: unknown) => any {
if (!tupleConstructors.hasOwnProperty(len)) {
tupleConstructors[len] = curried(tuple, len - 1, [])
}
return tupleConstructors[len]
}
/**
* Tuple sequencing, i.e., take a tuple of monadic actions and does them from left-to-right, returning the resulting tuple.
*
* @example
* import { sequenceT } from 'fp-ts/lib/Apply'
* import { option, some, none } from 'fp-ts/lib/Option'
*
* const sequenceTOption = sequenceT(option)
* assert.deepStrictEqual(sequenceTOption(some(1)), some([1]))
* assert.deepStrictEqual(sequenceTOption(some(1), some('2')), some([1, '2']))
* assert.deepStrictEqual(sequenceTOption(some(1), some('2'), none), none)
*
* @since 2.0.0
*/
export function sequenceT<F extends URIS4>(
F: Apply4<F>
): <S, R, E, T extends Array<Kind4<F, S, R, E, any>>>(
...t: T & { 0: Kind4<F, S, R, E, any> }
) => Kind4<F, S, R, E, { [K in keyof T]: [T[K]] extends [Kind4<F, S, R, E, infer A>] ? A : never }>
export function sequenceT<F extends URIS3>(
F: Apply3<F>
): <R, E, T extends Array<Kind3<F, R, E, any>>>(
...t: T & { 0: Kind3<F, R, E, any> }
) => Kind3<F, R, E, { [K in keyof T]: [T[K]] extends [Kind3<F, R, E, infer A>] ? A : never }>
export function sequenceT<F extends URIS3, E>(
F: Apply3C<F, E>
): <R, T extends Array<Kind3<F, R, E, any>>>(
...t: T & { 0: Kind3<F, R, E, any> }
) => Kind3<F, R, E, { [K in keyof T]: [T[K]] extends [Kind3<F, R, E, infer A>] ? A : never }>
export function sequenceT<F extends URIS2>(
F: Apply2<F>
): <E, T extends Array<Kind2<F, E, any>>>(
...t: T & { 0: Kind2<F, E, any> }
) => Kind2<F, E, { [K in keyof T]: [T[K]] extends [Kind2<F, E, infer A>] ? A : never }>
export function sequenceT<F extends URIS2, E>(
F: Apply2C<F, E>
): <T extends Array<Kind2<F, E, any>>>(
...t: T & { 0: Kind2<F, E, any> }
) => Kind2<F, E, { [K in keyof T]: [T[K]] extends [Kind2<F, E, infer A>] ? A : never }>
export function sequenceT<F extends URIS>(
F: Apply1<F>
): <T extends Array<Kind<F, any>>>(
...t: T & { 0: Kind<F, any> }
) => Kind<F, { [K in keyof T]: [T[K]] extends [Kind<F, infer A>] ? A : never }>
export function sequenceT<F>(
F: Apply<F>
): <T extends Array<HKT<F, any>>>(
...t: T & { 0: HKT<F, any> }
) => HKT<F, { [K in keyof T]: [T[K]] extends [HKT<F, infer A>] ? A : never }>
export function sequenceT<F>(F: Apply<F>): any {
return <A>(...args: Array<HKT<F, A>>) => {
const len = args.length
const f = getTupleConstructor(len)
let fas = F.map(args[0], f)
for (let i = 1; i < len; i++) {
fas = F.ap(fas, args[i])
}
return fas
}
}
type EnforceNonEmptyRecord<R> = keyof R extends never ? never : R
function getRecordConstructor(keys: Array<string>) {
const len = keys.length
return curried(
(...args: Array<unknown>) => {
const r: Record<string, unknown> = {}
for (let i = 0; i < len; i++) {
r[keys[i]] = args[i]
}
return r
},
len - 1,
[]
)
}
/**
* Like `Apply.sequenceT` but works with structs instead of tuples.
*
* @example
* import { either, right, left } from 'fp-ts/lib/Either'
* import { sequenceS } from 'fp-ts/lib/Apply'
*
* const ado = sequenceS(either)
*
* assert.deepStrictEqual(
* ado({
* a: right(1),
* b: right(true)
* }),
* right({ a: 1, b: true })
* )
* assert.deepStrictEqual(
* ado({
* a: right(1),
* b: left('error')
* }),
* left('error')
* )
*
* @since 2.0.0
*/
export function sequenceS<F extends URIS4>(
F: Apply4<F>
): <S, R, E, NER extends Record<string, Kind4<F, S, R, E, any>>>(
r: EnforceNonEmptyRecord<NER> & Record<string, Kind4<F, S, R, E, any>>
) => Kind4<F, S, R, E, { [K in keyof NER]: [NER[K]] extends [Kind4<F, any, any, any, infer A>] ? A : never }>
export function sequenceS<F extends URIS3>(
F: Apply3<F>
): <R, E, NER extends Record<string, Kind3<F, R, E, any>>>(
r: EnforceNonEmptyRecord<NER> & Record<string, Kind3<F, R, E, any>>
) => Kind3<F, R, E, { [K in keyof NER]: [NER[K]] extends [Kind3<F, any, any, infer A>] ? A : never }>
export function sequenceS<F extends URIS3, E>(
F: Apply3C<F, E>
): <R, NER extends Record<string, Kind3<F, R, E, any>>>(
r: EnforceNonEmptyRecord<NER> & Record<string, Kind3<F, R, E, any>>
) => Kind3<F, R, E, { [K in keyof NER]: [NER[K]] extends [Kind3<F, any, any, infer A>] ? A : never }>
export function sequenceS<F extends URIS2>(
F: Apply2<F>
): <E, NER extends Record<string, Kind2<F, E, any>>>(
r: EnforceNonEmptyRecord<NER> & Record<string, Kind2<F, E, any>>
) => Kind2<F, E, { [K in keyof NER]: [NER[K]] extends [Kind2<F, any, infer A>] ? A : never }>
export function sequenceS<F extends URIS2, E>(
F: Apply2C<F, E>
): <NER extends Record<string, Kind2<F, E, any>>>(
r: EnforceNonEmptyRecord<NER>
) => Kind2<F, E, { [K in keyof NER]: [NER[K]] extends [Kind2<F, any, infer A>] ? A : never }>
export function sequenceS<F extends URIS>(
F: Apply1<F>
): <NER extends Record<string, Kind<F, any>>>(
r: EnforceNonEmptyRecord<NER>
) => Kind<F, { [K in keyof NER]: [NER[K]] extends [Kind<F, infer A>] ? A : never }>
export function sequenceS<F>(
F: Apply<F>
): <NER extends Record<string, HKT<F, any>>>(
r: EnforceNonEmptyRecord<NER>
) => HKT<F, { [K in keyof NER]: [NER[K]] extends [HKT<F, infer A>] ? A : never }>
export function sequenceS<F>(F: Apply<F>): (r: Record<string, HKT<F, any>>) => HKT<F, Record<string, any>> {
return r => {
const keys = Object.keys(r)
const len = keys.length
const f = getRecordConstructor(keys)
let fr = F.map(r[keys[0]], f)
for (let i = 1; i < len; i++) {
fr = F.ap(fr, r[keys[i]])
}
return fr
}
}