1
import Foundation
2

3
/// An Applicative Functor is a `Functor` that also provides functionality to lift pure expressions, and sequence computations and combine their results.
4
///
5
/// Instances of this typeclass must obey the following laws:
6
///
7
/// 1. Identity
8
///
9
///         ap(pure(id), v) == v
10
///
11
/// 2. Composition
12
///
13
///         ap(ap(ap(pure(compose), u), v), w) == compose(u, compose(v, w))
14
///
15
/// 3. Homomorphism
16
///
17
///         ap(pure(f), pure(x)) == pure(f(x))
18
///
19
/// 4. Interchange
20
///
21
///         ap(fa, pure(b)) == ap(pure({ x in x(a) }), fa)
22
public protocol Applicative: Functor {
23
    /// Lifts a value to the context type implementing this instance of `Applicative`.
24
    ///
25
    /// - Parameter a: Value to be lifted.
26
    /// - Returns: Provided value in the context type implementing this instance.
27
    static func pure<A>(_ a: A) -> Kind<Self, A>
28

29
    /// Sequential application.
30
    ///
31
    /// - Parameters:
32
    ///   - ff: A function in the context implementing this instance.
33
    ///   - fa: A value in the context implementing this instance.
34
    /// - Returns: A value in the context implementing this instance, resulting of the transformation of the contained original value with the contained function.
35
    static func ap<A, B>(_ ff: Kind<Self, (A) -> B>, _ fa: Kind<Self, A>) -> Kind<Self, B>
36
}
37

38
// MARK: Related functions
39
public extension Applicative {
40
    /// Sequentially compose two computations, discarding the value produced by the first.
41
    ///
42
    /// - Parameters:
43
    ///   - fa: 1st computation.
44
    ///   - fb: 2nd computation.
45
    /// - Returns: Result of running the second computation after the first one.
46 1
    static func sequenceRight<A, B>(_ fa: Kind<Self, A>, _ fb: Kind<Self, B>) -> Kind<Self, B> {
47 1
        return map(fa, fb) { _, b in b }
48
    }
49

50
    /// Sequentially compose two computations, discarding the value produced by the second.
51
    ///
52
    /// - Parameters:
53
    ///   - fa: 1st computation.
54
    ///   - fb: 2nd computation.
55
    /// - Returns: Result produced from the first computation after both are computed.
56 0
    static func sequenceLeft<A, B>(_ fa: Kind<Self, A>, _ fb: Kind<Self, B>) -> Kind<Self, A> {
57 0
        return map(fa, fb) { a, _ in a }
58
    }
59

60
    /// Creates a tuple in the context implementing this instance from two values in the same context.
61
    ///
62
    /// - Parameters:
63
    ///   - fa: 1st value for the tuple.
64
    ///   - fb: 2nd value for the tuple.
65
    /// - Returns: A tuple of the provided values in the context implementing this instance.
66 1
    static func product<A, B>(_ fa: Kind<Self, A>, _ fb: Kind<Self, B>) -> Kind<Self, (A, B)> {
67 1
        return ap(map(fa, { (a: A) in { (b: B) in (a, b) }}), fb)
68
    }
69

70
    /// Adds an element to the right of a tuple in the context implementing this instance.
71
    ///
72
    /// - Parameters:
73
    ///   - fa: A tuple of two elements in the context implementing this instance.
74
    ///   - fz: A value in the context implementing this instance.
75
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
76 1
    static func product<A, B, Z>(_ fa: Kind<Self, (A, B)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, Z)> {
77 1
        return map(product(fa, fz), { a, b in (a.0, a.1, b) })
78
    }
79

80
    /// Adds an element to the right of a tuple in the context implementing this instance.
81
    ///
82
    /// - Parameters:
83
    ///   - fa: A tuple of three elements in the context implementing this instance.
84
    ///   - fz: A value in the context implementing this instance.
85
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
86 1
    static func product<A, B, C, Z>(_ fa: Kind<Self, (A, B, C)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, Z)> {
87 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, b) })
88
    }
89

90
    /// Adds an element to the right of a tuple in the context implementing this instance.
91
    ///
92
    /// - Parameters:
93
    ///   - fa: A tuple of four elements in the context implementing this instance.
94
    ///   - fz: A value in the context implementing this instance.
95
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
96 1
    static func product<A, B, C, D, Z>(_ fa: Kind<Self, (A, B, C, D)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, D, Z)> {
97 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, a.3, b) })
98
    }
99

100
    /// Adds an element to the right of a tuple in the context implementing this instance.
101
    ///
102
    /// - Parameters:
103
    ///   - fa: A tuple of five elements in the context implementing this instance.
104
    ///   - fz: A value in the context implementing this instance.
105
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
106 1
    static func product<A, B, C, D, E, Z>(_ fa: Kind<Self, (A, B, C, D, E)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, D, E, Z)> {
107 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, a.3, a.4, b) })
108
    }
109

110
    /// Adds an element to the right of a tuple in the context implementing this instance.
111
    ///
112
    /// - Parameters:
113
    ///   - fa: A tuple of six elements in the context implementing this instance.
114
    ///   - fz: A value in the context implementing this instance.
115
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
116 1
    static func product<A, B, C, D, E, G, Z>(_ fa: Kind<Self, (A, B, C, D, E, G)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, D, E, G, Z)> {
117 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, a.3, a.4, a.5, b) })
118
    }
119

120
    /// Adds an element to the right of a tuple in the context implementing this instance.
121
    ///
122
    /// - Parameters:
123
    ///   - fa: A tuple of seven elements in the context implementing this instance.
124
    ///   - fz: A value in the context implementing this instance.
125
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
126 1
    static func product<A, B, C, D, E, G, H, Z>(_ fa: Kind<Self, (A, B, C, D, E, G, H)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, D, E, G, H, Z)> {
127 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, a.3, a.4, a.5, a.6, b) })
128
    }
129

130
    /// Adds an element to the right of a tuple in the context implementing this instance.
131
    ///
132
    /// - Parameters:
133
    ///   - fa: A tuple of eight elements in the context implementing this instance.
134
    ///   - fz: A value in the context implementing this instance.
135
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
136 1
    static func product<A, B, C, D, E, G, H, I, Z>(_ fa: Kind<Self, (A, B, C, D, E, G, H, I)>, _ fz: Kind<Self, Z>) -> Kind<Self, (A, B, C, D, E, G, H, I, Z)> {
137 1
        return map(product(fa, fz), { a, b in (a.0, a.1, a.2, a.3, a.4, a.5, a.6, a.7, b) })
138
    }
139

140
    /// Performs two computations in the context implementing this instance and combines their result using the provided function.
141
    ///
142
    /// - Parameters:
143
    ///   - fa: A value in the context implementing this instance.
144
    ///   - fb: A lazy value in the context implementing this instance.
145
    ///   - f: A function to combine the result of the computations.
146
    /// - Returns: A lazy value with the result of combining the results of each computation.
147 1
    static func map2Eval<A, B, Z>(_ fa: Kind<Self, A>, _ fb: Eval<Kind<Self, B>>, _ f: @escaping (A, B) -> Z) -> Eval<Kind<Self, Z>> {
148 1
        return Eval.fix(fb.map{ fc in map(fa, fc, f) })
149
    }
150

151
    /// Creates a tuple out of two values in the context implementing this instance.
152
    ///
153
    /// - Parameters:
154
    ///   - a: 1st value of the tuple.
155
    ///   - b: 2nd value of the tuple.
156
    /// - Returns: A tuple in the context implementing this instance.
157
    static func zip<A, B>(_ a: Kind<Self, A>,
158 1
                             _ b : Kind<Self, B>) -> Kind<Self, (A, B)> {
159 1
        return product(a, b)
160
    }
161

162
    /// Creates a tuple out of three values in the context implementing this instance.
163
    ///
164
    /// - Parameters:
165
    ///   - a: 1st value of the tuple.
166
    ///   - b: 2nd value of the tuple.
167
    ///   - c: 3rd value of the tuple.
168
    /// - Returns: A tuple in the context implementing this instance.
169
    static func zip<A, B, C>(_ a: Kind<Self, A>,
170
                                _ b: Kind<Self, B>,
171 1
                                _ c: Kind<Self, C>) -> Kind<Self, (A, B, C)> {
172 1
        return product(product(a, b), c)
173
    }
174

175
    /// Creates a tuple out of four values in the context implementing this instance.
176
    ///
177
    /// - Parameters:
178
    ///   - a: 1st value of the tuple.
179
    ///   - b: 2nd value of the tuple.
180
    ///   - c: 3rd value of the tuple.
181
    ///   - d: 4th value of the tuple.
182
    /// - Returns: A tuple in the context implementing this instance.
183
    static func zip<A, B, C, D>(_ a: Kind<Self, A>,
184
                                   _ b: Kind<Self, B>,
185
                                   _ c: Kind<Self, C>,
186 1
                                   _ d: Kind<Self, D>) -> Kind<Self, (A, B, C, D)> {
187 1
        return product(product(product(a, b), c), d)
188
    }
189

190
    /// Creates a tuple out of five values in the context implementing this instance.
191
    ///
192
    /// - Parameters:
193
    ///   - a: 1st value of the tuple.
194
    ///   - b: 2nd value of the tuple.
195
    ///   - c: 3rd value of the tuple.
196
    ///   - d: 4th value of the tuple.
197
    ///   - e: 5th value of the tuple.
198
    /// - Returns: A tuple in the context implementing this instance.
199
    static func zip<A, B, C, D, E>(_ a: Kind<Self, A>,
200
                                      _ b: Kind<Self, B>,
201
                                      _ c: Kind<Self, C>,
202
                                      _ d: Kind<Self, D>,
203 1
                                      _ e: Kind<Self, E>) -> Kind<Self, (A, B, C, D, E)> {
204 1
        return product(product(product(product(a, b), c), d), e)
205
    }
206

207
    /// Creates a tuple out of six values in the context implementing this instance.
208
    ///
209
    /// - Parameters:
210
    ///   - a: 1st value of the tuple.
211
    ///   - b: 2nd value of the tuple.
212
    ///   - c: 3rd value of the tuple.
213
    ///   - d: 4th value of the tuple.
214
    ///   - e: 5th value of the tuple.
215
    ///   - g: 6th value of the tuple.
216
    /// - Returns: A tuple in the context implementing this instance.
217
    static func zip<A, B, C, D, E, G>(_ a: Kind<Self, A>,
218
                                         _ b: Kind<Self, B>,
219
                                         _ c: Kind<Self, C>,
220
                                         _ d: Kind<Self, D>,
221
                                         _ e: Kind<Self, E>,
222 1
                                         _ g: Kind<Self, G>) -> Kind<Self, (A, B, C, D, E, G)> {
223 1
        return product(product(product(product(product(a, b), c), d), e), g)
224
    }
225

226
    /// Creates a tuple out of seven values in the context implementing this instance.
227
    ///
228
    /// - Parameters:
229
    ///   - a: 1st value of the tuple.
230
    ///   - b: 2nd value of the tuple.
231
    ///   - c: 3rd value of the tuple.
232
    ///   - d: 4th value of the tuple.
233
    ///   - e: 5th value of the tuple.
234
    ///   - g: 6th value of the tuple.
235
    ///   - h: 7th value of the tuple.
236
    /// - Returns: A tuple in the context implementing this instance.
237
    static func zip<A, B, C, D, E, G, H>(_ a: Kind<Self, A>,
238
                                            _ b: Kind<Self, B>,
239
                                            _ c: Kind<Self, C>,
240
                                            _ d: Kind<Self, D>,
241
                                            _ e: Kind<Self, E>,
242
                                            _ g: Kind<Self, G>,
243 1
                                            _ h: Kind<Self, H>) -> Kind<Self, (A, B, C, D, E, G, H)> {
244 1
        return product(product(product(product(product(product(a, b), c), d), e), g), h)
245
    }
246

247
    /// Creates a tuple out of eight values in the context implementing this instance.
248
    ///
249
    /// - Parameters:
250
    ///   - a: 1st value of the tuple.
251
    ///   - b: 2nd value of the tuple.
252
    ///   - c: 3rd value of the tuple.
253
    ///   - d: 4th value of the tuple.
254
    ///   - e: 5th value of the tuple.
255
    ///   - g: 6th value of the tuple.
256
    ///   - h: 7th value of the tuple.
257
    ///   - i: 8th value of the tuple.
258
    /// - Returns: A tuple in the context implementing this instance.
259
    static func zip<A, B, C, D, E, G, H, I>(_ a: Kind<Self, A>,
260
                                               _ b: Kind<Self, B>,
261
                                               _ c: Kind<Self, C>,
262
                                               _ d: Kind<Self, D>,
263
                                               _ e: Kind<Self, E>,
264
                                               _ g: Kind<Self, G>,
265
                                               _ h: Kind<Self, H>,
266 1
                                               _ i: Kind<Self, I>) -> Kind<Self, (A, B, C, D, E, G, H, I)> {
267 1
        return product(product(product(product(product(product(product(a, b), c), d), e), g), h), i)
268
    }
269

270
    /// Creates a tuple out of nine values in the context implementing this instance.
271
    ///
272
    /// - Parameters:
273
    ///   - a: 1st value of the tuple.
274
    ///   - b: 2nd value of the tuple.
275
    ///   - c: 3rd value of the tuple.
276
    ///   - d: 4th value of the tuple.
277
    ///   - e: 5th value of the tuple.
278
    ///   - g: 6th value of the tuple.
279
    ///   - h: 7th value of the tuple.
280
    ///   - i: 8th value of the tuple.
281
    ///   - j: 9th value of the tuple.
282
    /// - Returns: A tuple in the context implementing this instance.
283
    static func zip<A, B, C, D, E, G, H, I, J>(_ a: Kind<Self, A>,
284
                                                  _ b: Kind<Self, B>,
285
                                                  _ c: Kind<Self, C>,
286
                                                  _ d: Kind<Self, D>,
287
                                                  _ e: Kind<Self, E>,
288
                                                  _ g: Kind<Self, G>,
289
                                                  _ h: Kind<Self, H>,
290
                                                  _ i: Kind<Self, I>,
291 1
                                                  _ j: Kind<Self, J>) -> Kind<Self, (A, B, C, D, E, G, H, I, J)> {
292 1
        return product(product(product(product(product(product(product(product(a, b), c), d), e), g), h), i), j)
293
    }
294

295
    /// Combines the result of two computations in the context implementing this instance, using the provided function.
296
    ///
297
    /// - Parameters:
298
    ///   - a: 1st computation.
299
    ///   - b: 2nd computation.
300
    ///   - f: Combination function.
301
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
302
    static func map<A, B, Z>(_ a: Kind<Self, A>,
303
                             _ b: Kind<Self, B>,
304 1
                             _ f: @escaping (A, B) -> Z) -> Kind<Self, Z> {
305 1
        return map(zip(a, b), f)
306
    }
307

308
    /// Combines the result of three computations in the context implementing this instance, using the provided function.
309
    ///
310
    /// - Parameters:
311
    ///   - a: 1st computation.
312
    ///   - b: 2nd computation.
313
    ///   - c: 3rd computation.
314
    ///   - f: Combination function.
315
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
316
    static func map<A, B, C, Z>(_ a: Kind<Self, A>,
317
                                _ b: Kind<Self, B>,
318
                                _ c: Kind<Self, C>,
319 1
                                _ f: @escaping (A, B, C) -> Z) -> Kind<Self, Z> {
320 1
        return map(zip(a, b, c), f)
321
    }
322

323
    /// Combines the result of four computations in the context implementing this instance, using the provided function.
324
    ///
325
    /// - Parameters:
326
    ///   - a: 1st computation.
327
    ///   - b: 2nd computation.
328
    ///   - c: 3rd computation.
329
    ///   - d: 4th computation.
330
    ///   - f: Combination function.
331
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
332
    static func map<A, B, C, D, Z>(_ a: Kind<Self, A>,
333
                                   _ b: Kind<Self, B>,
334
                                   _ c: Kind<Self, C>,
335
                                   _ d: Kind<Self, D>,
336 1
                                   _ f: @escaping (A, B, C, D) -> Z) -> Kind<Self, Z> {
337 1
        return map(zip(a, b, c, d), f)
338
    }
339

340
    /// Combines the result of five computations in the context implementing this instance, using the provided function.
341
    ///
342
    /// - Parameters:
343
    ///   - a: 1st computation.
344
    ///   - b: 2nd computation.
345
    ///   - c: 3rd computation.
346
    ///   - d: 4th computation.
347
    ///   - e: 5th computation.
348
    ///   - f: Combination function.
349
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
350
    static func map<A, B, C, D, E, Z>(_ a: Kind<Self, A>,
351
                                      _ b: Kind<Self, B>,
352
                                      _ c: Kind<Self, C>,
353
                                      _ d: Kind<Self, D>,
354
                                      _ e: Kind<Self, E>,
355 1
                                      _ f: @escaping (A, B, C, D, E) -> Z) -> Kind<Self, Z> {
356 1
        return map(zip(a, b, c, d, e), f)
357
    }
358

359
    /// Combines the result of six computations in the context implementing this instance, using the provided function.
360
    ///
361
    /// - Parameters:
362
    ///   - a: 1st computation.
363
    ///   - b: 2nd computation.
364
    ///   - c: 3rd computation.
365
    ///   - d: 4th computation.
366
    ///   - e: 5th computation.
367
    ///   - g: 6th computation.
368
    ///   - f: Combination function.
369
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
370
    static func map<A, B, C, D, E, G, Z>(_ a: Kind<Self, A>,
371
                                         _ b: Kind<Self, B>,
372
                                         _ c: Kind<Self, C>,
373
                                         _ d: Kind<Self, D>,
374
                                         _ e: Kind<Self, E>,
375
                                         _ g: Kind<Self, G>,
376 1
                                         _ f: @escaping (A, B, C, D, E, G) -> Z) -> Kind<Self, Z> {
377 1
        return map(zip(a, b, c, d, e, g), f)
378
    }
379

380
    /// Combines the result of seven computations in the context implementing this instance, using the provided function.
381
    ///
382
    /// - Parameters:
383
    ///   - a: 1st computation.
384
    ///   - b: 2nd computation.
385
    ///   - c: 3rd computation.
386
    ///   - d: 4th computation.
387
    ///   - e: 5th computation.
388
    ///   - g: 6th computation.
389
    ///   - h: 7th computation.
390
    ///   - f: Combination function.
391
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
392
    static func map<A, B, C, D, E, G, H, Z>(_ a: Kind<Self, A>,
393
                                            _ b: Kind<Self, B>,
394
                                            _ c: Kind<Self, C>,
395
                                            _ d: Kind<Self, D>,
396
                                            _ e: Kind<Self, E>,
397
                                            _ g: Kind<Self, G>,
398
                                            _ h: Kind<Self, H>,
399 1
                                            _ f: @escaping (A, B, C, D, E, G, H) -> Z) -> Kind<Self, Z> {
400 1
        return map(zip(a, b, c, d, e, g, h), f)
401
    }
402

403
    /// Combines the result of eight computations in the context implementing this instance, using the provided function.
404
    ///
405
    /// - Parameters:
406
    ///   - a: 1st computation.
407
    ///   - b: 2nd computation.
408
    ///   - c: 3rd computation.
409
    ///   - d: 4th computation.
410
    ///   - e: 5th computation.
411
    ///   - g: 6th computation.
412
    ///   - h: 7th computation.
413
    ///   - i: 8th computation.
414
    ///   - f: Combination function.
415
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
416
    static func map<A, B, C, D, E, G, H, I, Z>(_ a: Kind<Self, A>,
417
                                               _ b: Kind<Self, B>,
418
                                               _ c: Kind<Self, C>,
419
                                               _ d: Kind<Self, D>,
420
                                               _ e: Kind<Self, E>,
421
                                               _ g: Kind<Self, G>,
422
                                               _ h: Kind<Self, H>,
423
                                               _ i: Kind<Self, I>,
424 1
                                               _ f: @escaping (A, B, C, D, E, G, H, I) -> Z) -> Kind<Self, Z> {
425 1
        return map(zip(a, b, c, d, e, g, h, i), f)
426
    }
427

428
    /// Combines the result of nine computations in the context implementing this instance, using the provided function.
429
    ///
430
    /// - Parameters:
431
    ///   - a: 1st computation.
432
    ///   - b: 2nd computation.
433
    ///   - c: 3rd computation.
434
    ///   - d: 4th computation.
435
    ///   - e: 5th computation.
436
    ///   - g: 6th computation.
437
    ///   - h: 7th computation.
438
    ///   - i: 8th computation.
439
    ///   - j: 9th computation.
440
    ///   - f: Combination function.
441
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
442
    static func map<A, B, C, D, E, G, H, I, J, Z>(_ a: Kind<Self, A>,
443
                                                  _ b: Kind<Self, B>,
444
                                                  _ c: Kind<Self, C>,
445
                                                  _ d: Kind<Self, D>,
446
                                                  _ e: Kind<Self, E>,
447
                                                  _ g: Kind<Self, G>,
448
                                                  _ h: Kind<Self, H>,
449
                                                  _ i: Kind<Self, I>,
450
                                                  _ j: Kind<Self, J>,
451 1
                                                  _ f: @escaping (A, B, C, D, E, G, H, I, J) -> Z) -> Kind<Self, Z> {
452 1
        return map(zip(a, b, c, d, e, g, h, i, j), f)
453
    }
454
}
455

456
// MARK: Syntax for Applicative
457
public extension Kind where F: Applicative {
458
    /// Lifts a value to the context type implementing this instance of `Applicative`.
459
    ///
460
    /// This is a convenience method to call `Applicative.pure` as a static method of this type.
461
    ///
462
    /// - Parameter a: Value to be lifted.
463
    /// - Returns: Provided value in the context type implementing this instance.
464 1
    static func pure(_ a: A) -> Kind<F, A> {
465 1
        return F.pure(a)
466
    }
467

468
    /// Sequential application.
469
    ///
470
    /// This is a convenience method to call `Applicative.ap` as an instance method of this type.
471
    ///
472
    /// - Parameters:
473
    ///   - fa: A value in the context implementing this instance.
474
    /// - Returns: A value in the context implementing this instance, resulting of the transformation of the contained original value with the contained function.
475 1
    func ap<AA, B>(_ fa: Kind<F, AA>) -> Kind<F, B> where A == (AA) -> B {
476 1
        return F.ap(self, fa)
477
    }
478

479
    /// Creates a tuple in the context implementing this instance from two values in the same context.
480
    ///
481
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
482
    ///
483
    /// - Parameters:
484
    ///   - fa: 1st value for the tuple.
485
    ///   - fb: 2nd value for the tuple.
486
    /// - Returns: A tuple of the provided values in the context implementing this instance.
487 0
    static func product<AA, B>(_ fa: Kind<F, AA>, _ fb: Kind<F, B>) -> Kind<F, (AA, B)> where A == (AA, B) {
488 0
        return F.product(fa, fb)
489
    }
490

491
    /// Adds an element to the right of a tuple in the context implementing this instance.
492
    ///
493
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
494
    ///
495
    /// - Parameters:
496
    ///   - fa: A tuple of two elements in the context implementing this instance.
497
    ///   - fz: A value in the context implementing this instance.
498
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
499 0
    static func product<AA, B, Z>(_ fa: Kind<F, (AA, B)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, Z)> where A == (AA, B, Z) {
500 0
        return F.product(fa, fz)
501
    }
502

503
    /// Adds an element to the right of a tuple in the context implementing this instance.
504
    ///
505
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
506
    ///
507
    /// - Parameters:
508
    ///   - fa: A tuple of three elements in the context implementing this instance.
509
    ///   - fz: A value in the context implementing this instance.
510
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
511 0
    static func product<AA, B, C, Z>(_ fa: Kind<F, (AA, B, C)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, C, Z)> where A == (AA, B, C, Z) {
512 0
        return F.product(fa, fz)
513
    }
514

515
    /// Adds an element to the right of a tuple in the context implementing this instance.
516
    ///
517
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
518
    ///
519
    /// - Parameters:
520
    ///   - fa: A tuple of four elements in the context implementing this instance.
521
    ///   - fz: A value in the context implementing this instance.
522
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
523 0
    static func product<AA, B, C, D, Z>(_ fa: Kind<F, (AA, B, C, D)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, C, D, Z)> where A == (AA, B, C, D, Z) {
524 0
        return F.product(fa, fz)
525
    }
526

527
    /// Adds an element to the right of a tuple in the context implementing this instance.
528
    ///
529
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
530
    ///
531
    /// - Parameters:
532
    ///   - fa: A tuple of five elements in the context implementing this instance.
533
    ///   - fz: A value in the context implementing this instance.
534
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
535 0
    static func product<AA, B, C, D, E, Z>(_ fa: Kind<F, (AA, B, C, D, E)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, C, D, E, Z)> where A == (AA, B, C, D, E, Z) {
536 0
        return F.product(fa, fz)
537
    }
538

539
    /// Adds an element to the right of a tuple in the context implementing this instance.
540
    ///
541
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
542
    ///
543
    /// - Parameters:
544
    ///   - fa: A tuple of six elements in the context implementing this instance.
545
    ///   - fz: A value in the context implementing this instance.
546
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
547 0
    static func product<AA, B, C, D, E, G, Z>(_ fa: Kind<F, (AA, B, C, D, E, G)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, C, D, E, G, Z)> where A == (AA, B, C, D, E, G, Z) {
548 0
        return F.product(fa, fz)
549
    }
550

551
    /// Adds an element to the right of a tuple in the context implementing this instance.
552
    ///
553
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
554
    ///
555
    /// - Parameters:
556
    ///   - fa: A tuple of seven elements in the context implementing this instance.
557
    ///   - fz: A value in the context implementing this instance.
558
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
559 0
    static func product<AA, B, C, D, E, G, H, Z>(_ fa: Kind<F, (AA, B, C, D, E, G, H)>, _ fz: Kind<F, Z>) -> Kind<F, (AA, B, C, D, E, G, H, Z)> where A == (AA, B, C, D, E, G, H, Z) {
560 0
        return F.product(fa, fz)
561
    }
562

563
    /// Adds an element to the right of a tuple in the context implementing this instance.
564
    ///
565
    /// This is a convenience method to call `Applicative.product` as a static method of this type.
566
    ///
567
    /// - Parameters:
568
    ///   - fa: A tuple of eight elements in the context implementing this instance.
569
    ///   - fz: A value in the context implementing this instance.
570
    /// - Returns: A tuple with the value of the second argument added to the right of the tuple, in the context implementing this instance.
571 0
    static func product<AA, B, C, D, E, G, H, I, Z>(_ fa: Kind<F, (AA, B, C, D, E, G, H, I)>, _ fz : Kind<F, Z>) -> Kind<F, (AA, B, C, D, E, G, H, I, Z)> where A == (AA, B, C, D, E, G, H, I, Z) {
572 0
        return F.product(fa, fz)
573
    }
574

575
    /// Creates a tuple out of two values in the context implementing this instance.
576
    ///
577
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
578
    ///
579
    /// - Parameters:
580
    ///   - a: 1st value of the tuple.
581
    ///   - b: 2nd value of the tuple.
582
    /// - Returns: A tuple in the context implementing this instance.
583
    static func zip<AA, B>(_ a: Kind<F, AA>,
584 0
                           _ b : Kind<F, B>) -> Kind<F, (AA, B)> where A == (AA, B){
585 0
        return F.zip(a, b)
586
    }
587

588
    /// Creates a tuple out of three values in the context implementing this instance.
589
    ///
590
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
591
    ///
592
    /// - Parameters:
593
    ///   - a: 1st value of the tuple.
594
    ///   - b: 2nd value of the tuple.
595
    ///   - c: 3rd value of the tuple.
596
    /// - Returns: A tuple in the context implementing this instance.
597
    static func zip<AA, B, C>(_ a: Kind<F, AA>,
598
                              _ b: Kind<F, B>,
599 0
                              _ c: Kind<F, C>) -> Kind<F, (AA, B, C)> where A == (AA, B, C) {
600 0
        return F.zip(a, b, c)
601
    }
602

603
    /// Creates a tuple out of four values in the context implementing this instance.
604
    ///
605
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
606
    ///
607
    /// - Parameters:
608
    ///   - a: 1st value of the tuple.
609
    ///   - b: 2nd value of the tuple.
610
    ///   - c: 3rd value of the tuple.
611
    ///   - d: 4th value of the tuple.
612
    /// - Returns: A tuple in the context implementing this instance.
613
    static func zip<AA, B, C, D>(_ a: Kind<F, AA>,
614
                                 _ b: Kind<F, B>,
615
                                 _ c: Kind<F, C>,
616 0
                                 _ d: Kind<F, D>) -> Kind<F, (AA, B, C, D)> where A == (AA, B, C, D) {
617 0
        return F.zip(a, b, c, d)
618
    }
619

620
    /// Creates a tuple out of five values in the context implementing this instance.
621
    ///
622
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
623
    ///
624
    /// - Parameters:
625
    ///   - a: 1st value of the tuple.
626
    ///   - b: 2nd value of the tuple.
627
    ///   - c: 3rd value of the tuple.
628
    ///   - d: 4th value of the tuple.
629
    ///   - e: 5th value of the tuple.
630
    /// - Returns: A tuple in the context implementing this instance.
631
    static func zip<AA, B, C, D, E>(_ a: Kind<F, AA>,
632
                                    _ b: Kind<F, B>,
633
                                    _ c: Kind<F, C>,
634
                                    _ d: Kind<F, D>,
635 0
                                    _ e: Kind<F, E>) -> Kind<F, (AA, B, C, D, E)> where A == (AA, B, C, D, E) {
636 0
        return F.zip(a, b, c, d, e)
637
    }
638

639
    /// Creates a tuple out of six values in the context implementing this instance.
640
    ///
641
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
642
    ///
643
    /// - Parameters:
644
    ///   - a: 1st value of the tuple.
645
    ///   - b: 2nd value of the tuple.
646
    ///   - c: 3rd value of the tuple.
647
    ///   - d: 4th value of the tuple.
648
    ///   - e: 5th value of the tuple.
649
    ///   - g: 6th value of the tuple.
650
    /// - Returns: A tuple in the context implementing this instance.
651
    static func zip<AA, B, C, D, E, G>(_ a: Kind<F, AA>,
652
                                       _ b: Kind<F, B>,
653
                                       _ c: Kind<F, C>,
654
                                       _ d: Kind<F, D>,
655
                                       _ e: Kind<F, E>,
656 0
                                       _ g: Kind<F, G>) -> Kind<F, (AA, B, C, D, E, G)> where A == (AA, B, C, D, E, G) {
657 0
        return F.zip(a, b, c, d, e, g)
658
    }
659

660
    /// Creates a tuple out of seven values in the context implementing this instance.
661
    ///
662
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
663
    ///
664
    /// - Parameters:
665
    ///   - a: 1st value of the tuple.
666
    ///   - b: 2nd value of the tuple.
667
    ///   - c: 3rd value of the tuple.
668
    ///   - d: 4th value of the tuple.
669
    ///   - e: 5th value of the tuple.
670
    ///   - g: 6th value of the tuple.
671
    ///   - h: 7th value of the tuple.
672
    /// - Returns: A tuple in the context implementing this instance.
673
    static func zip<AA, B, C, D, E, G, H>(_ a: Kind<F, AA>,
674
                                          _ b: Kind<F, B>,
675
                                          _ c: Kind<F, C>,
676
                                          _ d: Kind<F, D>,
677
                                          _ e: Kind<F, E>,
678
                                          _ g: Kind<F, G>,
679 0
                                          _ h: Kind<F, H>) -> Kind<F, (AA, B, C, D, E, G, H)> where A == (AA, B, C, D, E, G, H) {
680 0
        return F.zip(a, b, c, d, e, g, h)
681
    }
682

683
    /// Creates a tuple out of eight values in the context implementing this instance.
684
    ///
685
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
686
    ///
687
    /// - Parameters:
688
    ///   - a: 1st value of the tuple.
689
    ///   - b: 2nd value of the tuple.
690
    ///   - c: 3rd value of the tuple.
691
    ///   - d: 4th value of the tuple.
692
    ///   - e: 5th value of the tuple.
693
    ///   - g: 6th value of the tuple.
694
    ///   - h: 7th value of the tuple.
695
    ///   - i: 8th value of the tuple.
696
    /// - Returns: A tuple in the context implementing this instance.
697
    static func zip<AA, B, C, D, E, G, H, I>(_ a: Kind<F, AA>,
698
                                             _ b: Kind<F, B>,
699
                                             _ c: Kind<F, C>,
700
                                             _ d: Kind<F, D>,
701
                                             _ e: Kind<F, E>,
702
                                             _ g: Kind<F, G>,
703
                                             _ h: Kind<F, H>,
704 0
                                             _ i: Kind<F, I>) -> Kind<F, (AA, B, C, D, E, G, H, I)> where A == (AA, B, C, D, E, G, H, I) {
705 0
        return F.zip(a, b, c, d, e, g, h, i)
706
    }
707

708
    /// Creates a tuple out of nine values in the context implementing this instance.
709
    ///
710
    /// This is a convenience method to call `Applicative.zip` as a static method of this type.
711
    ///
712
    /// - Parameters:
713
    ///   - a: 1st value of the tuple.
714
    ///   - b: 2nd value of the tuple.
715
    ///   - c: 3rd value of the tuple.
716
    ///   - d: 4th value of the tuple.
717
    ///   - e: 5th value of the tuple.
718
    ///   - g: 6th value of the tuple.
719
    ///   - h: 7th value of the tuple.
720
    ///   - i: 8th value of the tuple.
721
    ///   - j: 9th value of the tuple.
722
    /// - Returns: A tuple in the context implementing this instance.
723
    static func zip<AA, B, C, D, E, G, H, I, J>(_ a: Kind<F, AA>,
724
                                                _ b: Kind<F, B>,
725
                                                _ c: Kind<F, C>,
726
                                                _ d: Kind<F, D>,
727
                                                _ e: Kind<F, E>,
728
                                                _ g: Kind<F, G>,
729
                                                _ h: Kind<F, H>,
730
                                                _ i: Kind<F, I>,
731 0
                                                _ j: Kind<F, J>) -> Kind<F, (AA, B, C, D, E, G, H, I, J)> where A == (AA, B, C, D, E, G, H, I, J) {
732 0
        return F.zip(a, b, c, d, e, g, h, i, j)
733
    }
734

735
    /// Combines the result of two computations in the context implementing this instance, using the provided function.
736
    ///
737
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
738
    ///
739
    /// - Parameters:
740
    ///   - a: 1st computation.
741
    ///   - b: 2nd computation.
742
    ///   - f: Combination function.
743
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
744
    static func map<A, B, Z>(_ a: Kind<F, Z>,
745
                             _ b: Kind<F, B>,
746 0
                             _ f: @escaping (Z, B) -> A) -> Kind<F, A> {
747 0
        return F.map(a, b, f)
748
    }
749

750
    /// Combines the result of three computations in the context implementing this instance, using the provided function.
751
    ///
752
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
753
    ///
754
    /// - Parameters:
755
    ///   - a: 1st computation.
756
    ///   - b: 2nd computation.
757
    ///   - c: 3rd computation.
758
    ///   - f: Combination function.
759
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
760
    static func map<A, B, C, Z>(_ a: Kind<F, Z>,
761
                                _ b: Kind<F, B>,
762
                                _ c: Kind<F, C>,
763 0
                                _ f: @escaping (Z, B, C) -> A) -> Kind<F, A> {
764 0
        return F.map(a, b, c, f)
765
    }
766

767
    /// Combines the result of four computations in the context implementing this instance, using the provided function.
768
    ///
769
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
770
    ///
771
    /// - Parameters:
772
    ///   - a: 1st computation.
773
    ///   - b: 2nd computation.
774
    ///   - c: 3rd computation.
775
    ///   - d: 4th computation.
776
    ///   - f: Combination function.
777
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
778
    static func map<A, B, C, D, Z>(_ a: Kind<F, Z>,
779
                                   _ b: Kind<F, B>,
780
                                   _ c: Kind<F, C>,
781
                                   _ d: Kind<F, D>,
782 0
                                   _ f: @escaping (Z, B, C, D) -> A) -> Kind<F, A> {
783 0
        return F.map(a, b, c, d, f)
784
    }
785

786
    /// Combines the result of five computations in the context implementing this instance, using the provided function.
787
    ///
788
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
789
    ///
790
    /// - Parameters:
791
    ///   - a: 1st computation.
792
    ///   - b: 2nd computation.
793
    ///   - c: 3rd computation.
794
    ///   - d: 4th computation.
795
    ///   - e: 5th computation.
796
    ///   - f: Combination function.
797
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
798
    static func map<A, B, C, D, E, Z>(_ a: Kind<F, Z>,
799
                                      _ b: Kind<F, B>,
800
                                      _ c: Kind<F, C>,
801
                                      _ d: Kind<F, D>,
802
                                      _ e: Kind<F, E>,
803 0
                                      _ f: @escaping (Z, B, C, D, E) -> A) -> Kind<F, A> {
804 0
        return F.map(a, b, c, d, e, f)
805
    }
806

807
    /// Combines the result of six computations in the context implementing this instance, using the provided function.
808
    ///
809
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
810
    ///
811
    /// - Parameters:
812
    ///   - a: 1st computation.
813
    ///   - b: 2nd computation.
814
    ///   - c: 3rd computation.
815
    ///   - d: 4th computation.
816
    ///   - e: 5th computation.
817
    ///   - g: 6th computation.
818
    ///   - f: Combination function.
819
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
820
    static func map<A, B, C, D, E, G, Z>(_ a: Kind<F, Z>,
821
                                         _ b: Kind<F, B>,
822
                                         _ c: Kind<F, C>,
823
                                         _ d: Kind<F, D>,
824
                                         _ e: Kind<F, E>,
825
                                         _ g: Kind<F, G>,
826 0
                                         _ f: @escaping (Z, B, C, D, E, G) -> A) -> Kind<F, A> {
827 0
        return F.map(a, b, c, d, e, g, f)
828
    }
829

830
    /// Combines the result of seven computations in the context implementing this instance, using the provided function.
831
    ///
832
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
833
    ///
834
    /// - Parameters:
835
    ///   - a: 1st computation.
836
    ///   - b: 2nd computation.
837
    ///   - c: 3rd computation.
838
    ///   - d: 4th computation.
839
    ///   - e: 5th computation.
840
    ///   - g: 6th computation.
841
    ///   - h: 7th computation.
842
    ///   - f: Combination function.
843
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
844
    static func map<A, B, C, D, E, G, H, Z>(_ a: Kind<F, Z>,
845
                                            _ b: Kind<F, B>,
846
                                            _ c: Kind<F, C>,
847
                                            _ d: Kind<F, D>,
848
                                            _ e: Kind<F, E>,
849
                                            _ g: Kind<F, G>,
850
                                            _ h: Kind<F, H>,
851 0
                                            _ f: @escaping (Z, B, C, D, E, G, H) -> A) -> Kind<F, A> {
852 0
        return F.map(a, b, c, d, e, g, h, f)
853
    }
854

855
    /// Combines the result of eight computations in the context implementing this instance, using the provided function.
856
    ///
857
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
858
    ///
859
    /// - Parameters:
860
    ///   - a: 1st computation.
861
    ///   - b: 2nd computation.
862
    ///   - c: 3rd computation.
863
    ///   - d: 4th computation.
864
    ///   - e: 5th computation.
865
    ///   - g: 6th computation.
866
    ///   - h: 7th computation.
867
    ///   - i: 8th computation.
868
    ///   - f: Combination function.
869
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
870
    static func map<A, B, C, D, E, G, H, I, Z>(_ a: Kind<F, Z>,
871
                                               _ b: Kind<F, B>,
872
                                               _ c: Kind<F, C>,
873
                                               _ d: Kind<F, D>,
874
                                               _ e: Kind<F, E>,
875
                                               _ g: Kind<F, G>,
876
                                               _ h: Kind<F, H>,
877
                                               _ i: Kind<F, I>,
878 0
                                               _ f: @escaping (Z, B, C, D, E, G, H, I) -> A) -> Kind<F, A> {
879 0
        return F.map(a, b, c, d, e, g, h, i, f)
880
    }
881

882
    /// Combines the result of nine computations in the context implementing this instance, using the provided function.
883
    ///
884
    /// This is a convenience method to call `Applicative.map` as a static method of this type.
885
    ///
886
    /// - Parameters:
887
    ///   - a: 1st computation.
888
    ///   - b: 2nd computation.
889
    ///   - c: 3rd computation.
890
    ///   - d: 4th computation.
891
    ///   - e: 5th computation.
892
    ///   - g: 6th computation.
893
    ///   - h: 7th computation.
894
    ///   - i: 8th computation.
895
    ///   - j: 9th computation.
896
    ///   - f: Combination function.
897
    /// - Returns: Result of combining the provided computations, in the context implementing this instance.
898
    static func map<A, B, C, D, E, G, H, I, J, Z>(_ a: Kind<F, Z>,
899
                                                  _ b: Kind<F, B>,
900
                                                  _ c: Kind<F, C>,
901
                                                  _ d: Kind<F, D>,
902
                                                  _ e: Kind<F, E>,
903
                                                  _ g: Kind<F, G>,
904
                                                  _ h: Kind<F, H>,
905
                                                  _ i: Kind<F, I>,
906
                                                  _ j: Kind<F, J>,
907 0
                                                  _ f: @escaping (Z, B, C, D, E, G, H, I, J) -> A) -> Kind<F, A> {
908 0
        return F.map(a, b, c, d, e, g, h, i, j, f)
909
    }
910
}

Read our documentation on viewing source code .

Loading