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) -> Kind ``` 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(_ ff: Kind B>, _ fa: Kind) -> Kind ``` 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(_ fa: Kind, _ fb: Kind) -> Kind { ``` 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(_ fa: Kind, _ fb: Kind) -> Kind { ``` 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(_ fa: Kind, _ fb: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fz: Kind) -> Kind { ``` 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(_ fa: Kind, _ fb: Eval>, _ f: @escaping (A, B) -> Z) -> Eval> { ``` 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: Kind, ``` 158 1 ``` _ b : Kind) -> Kind { ``` 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: Kind, ``` 170 ``` _ b: Kind, ``` 171 1 ``` _ c: Kind) -> Kind { ``` 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: Kind, ``` 184 ``` _ b: Kind, ``` 185 ``` _ c: Kind, ``` 186 1 ``` _ d: Kind) -> Kind { ``` 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: Kind, ``` 200 ``` _ b: Kind, ``` 201 ``` _ c: Kind, ``` 202 ``` _ d: Kind, ``` 203 1 ``` _ e: Kind) -> Kind { ``` 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: Kind, ``` 218 ``` _ b: Kind, ``` 219 ``` _ c: Kind, ``` 220 ``` _ d: Kind, ``` 221 ``` _ e: Kind, ``` 222 1 ``` _ g: Kind) -> Kind { ``` 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: Kind, ``` 238 ``` _ b: Kind, ``` 239 ``` _ c: Kind, ``` 240 ``` _ d: Kind, ``` 241 ``` _ e: Kind, ``` 242 ``` _ g: Kind, ``` 243 1 ``` _ h: Kind) -> Kind { ``` 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: Kind, ``` 260 ``` _ b: Kind, ``` 261 ``` _ c: Kind, ``` 262 ``` _ d: Kind, ``` 263 ``` _ e: Kind, ``` 264 ``` _ g: Kind, ``` 265 ``` _ h: Kind, ``` 266 1 ``` _ i: Kind) -> Kind { ``` 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: Kind, ``` 284 ``` _ b: Kind, ``` 285 ``` _ c: Kind, ``` 286 ``` _ d: Kind, ``` 287 ``` _ e: Kind, ``` 288 ``` _ g: Kind, ``` 289 ``` _ h: Kind, ``` 290 ``` _ i: Kind, ``` 291 1 ``` _ j: Kind) -> Kind { ``` 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: Kind, ``` 303 ``` _ b: Kind, ``` 304 1 ``` _ f: @escaping (A, B) -> Z) -> Kind { ``` 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: Kind, ``` 317 ``` _ b: Kind, ``` 318 ``` _ c: Kind, ``` 319 1 ``` _ f: @escaping (A, B, C) -> Z) -> Kind { ``` 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: Kind, ``` 333 ``` _ b: Kind, ``` 334 ``` _ c: Kind, ``` 335 ``` _ d: Kind, ``` 336 1 ``` _ f: @escaping (A, B, C, D) -> Z) -> Kind { ``` 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: Kind, ``` 351 ``` _ b: Kind, ``` 352 ``` _ c: Kind, ``` 353 ``` _ d: Kind, ``` 354 ``` _ e: Kind, ``` 355 1 ``` _ f: @escaping (A, B, C, D, E) -> Z) -> Kind { ``` 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: Kind, ``` 371 ``` _ b: Kind, ``` 372 ``` _ c: Kind, ``` 373 ``` _ d: Kind, ``` 374 ``` _ e: Kind, ``` 375 ``` _ g: Kind, ``` 376 1 ``` _ f: @escaping (A, B, C, D, E, G) -> Z) -> Kind { ``` 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: Kind, ``` 393 ``` _ b: Kind, ``` 394 ``` _ c: Kind, ``` 395 ``` _ d: Kind, ``` 396 ``` _ e: Kind, ``` 397 ``` _ g: Kind, ``` 398 ``` _ h: Kind, ``` 399 1 ``` _ f: @escaping (A, B, C, D, E, G, H) -> Z) -> Kind { ``` 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: Kind, ``` 417 ``` _ b: Kind, ``` 418 ``` _ c: Kind, ``` 419 ``` _ d: Kind, ``` 420 ``` _ e: Kind, ``` 421 ``` _ g: Kind, ``` 422 ``` _ h: Kind, ``` 423 ``` _ i: Kind, ``` 424 1 ``` _ f: @escaping (A, B, C, D, E, G, H, I) -> Z) -> Kind { ``` 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: Kind, ``` 443 ``` _ b: Kind, ``` 444 ``` _ c: Kind, ``` 445 ``` _ d: Kind, ``` 446 ``` _ e: Kind, ``` 447 ``` _ g: Kind, ``` 448 ``` _ h: Kind, ``` 449 ``` _ i: Kind, ``` 450 ``` _ j: Kind, ``` 451 1 ``` _ f: @escaping (A, B, C, D, E, G, H, I, J) -> Z) -> Kind { ``` 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 { ``` 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(_ fa: Kind) -> Kind 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(_ fa: Kind, _ fb: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz: Kind) -> Kind 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(_ fa: Kind, _ fz : Kind) -> Kind 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(_ a: Kind, ``` 584 0 ``` _ b : Kind) -> Kind 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(_ a: Kind, ``` 598 ``` _ b: Kind, ``` 599 0 ``` _ c: Kind) -> Kind 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(_ a: Kind, ``` 614 ``` _ b: Kind, ``` 615 ``` _ c: Kind, ``` 616 0 ``` _ d: Kind) -> Kind 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(_ a: Kind, ``` 632 ``` _ b: Kind, ``` 633 ``` _ c: Kind, ``` 634 ``` _ d: Kind, ``` 635 0 ``` _ e: Kind) -> Kind 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(_ a: Kind, ``` 652 ``` _ b: Kind, ``` 653 ``` _ c: Kind, ``` 654 ``` _ d: Kind, ``` 655 ``` _ e: Kind, ``` 656 0 ``` _ g: Kind) -> Kind 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(_ a: Kind, ``` 674 ``` _ b: Kind, ``` 675 ``` _ c: Kind, ``` 676 ``` _ d: Kind, ``` 677 ``` _ e: Kind, ``` 678 ``` _ g: Kind, ``` 679 0 ``` _ h: Kind) -> Kind 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(_ a: Kind, ``` 698 ``` _ b: Kind, ``` 699 ``` _ c: Kind, ``` 700 ``` _ d: Kind, ``` 701 ``` _ e: Kind, ``` 702 ``` _ g: Kind, ``` 703 ``` _ h: Kind, ``` 704 0 ``` _ i: Kind) -> Kind 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(_ a: Kind, ``` 724 ``` _ b: Kind, ``` 725 ``` _ c: Kind, ``` 726 ``` _ d: Kind, ``` 727 ``` _ e: Kind, ``` 728 ``` _ g: Kind, ``` 729 ``` _ h: Kind, ``` 730 ``` _ i: Kind, ``` 731 0 ``` _ j: Kind) -> Kind 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: Kind, ``` 745 ``` _ b: Kind, ``` 746 0 ``` _ f: @escaping (Z, B) -> A) -> Kind { ``` 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: Kind, ``` 761 ``` _ b: Kind, ``` 762 ``` _ c: Kind, ``` 763 0 ``` _ f: @escaping (Z, B, C) -> A) -> Kind { ``` 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: Kind, ``` 779 ``` _ b: Kind, ``` 780 ``` _ c: Kind, ``` 781 ``` _ d: Kind, ``` 782 0 ``` _ f: @escaping (Z, B, C, D) -> A) -> Kind { ``` 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: Kind, ``` 799 ``` _ b: Kind, ``` 800 ``` _ c: Kind, ``` 801 ``` _ d: Kind, ``` 802 ``` _ e: Kind, ``` 803 0 ``` _ f: @escaping (Z, B, C, D, E) -> A) -> Kind { ``` 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: Kind, ``` 821 ``` _ b: Kind, ``` 822 ``` _ c: Kind, ``` 823 ``` _ d: Kind, ``` 824 ``` _ e: Kind, ``` 825 ``` _ g: Kind, ``` 826 0 ``` _ f: @escaping (Z, B, C, D, E, G) -> A) -> Kind { ``` 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: Kind, ``` 845 ``` _ b: Kind, ``` 846 ``` _ c: Kind, ``` 847 ``` _ d: Kind, ``` 848 ``` _ e: Kind, ``` 849 ``` _ g: Kind, ``` 850 ``` _ h: Kind, ``` 851 0 ``` _ f: @escaping (Z, B, C, D, E, G, H) -> A) -> Kind { ``` 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: Kind, ``` 871 ``` _ b: Kind, ``` 872 ``` _ c: Kind, ``` 873 ``` _ d: Kind, ``` 874 ``` _ e: Kind, ``` 875 ``` _ g: Kind, ``` 876 ``` _ h: Kind, ``` 877 ``` _ i: Kind, ``` 878 0 ``` _ f: @escaping (Z, B, C, D, E, G, H, I) -> A) -> Kind { ``` 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: Kind, ``` 899 ``` _ b: Kind, ``` 900 ``` _ c: Kind, ``` 901 ``` _ d: Kind, ``` 902 ``` _ e: Kind, ``` 903 ``` _ g: Kind, ``` 904 ``` _ h: Kind, ``` 905 ``` _ i: Kind, ``` 906 ``` _ j: Kind, ``` 907 0 ``` _ f: @escaping (Z, B, C, D, E, G, H, I, J) -> A) -> Kind { ``` 908 0 ``` return F.map(a, b, c, d, e, g, h, i, j, f) ``` 909 ``` } ``` 910 ```} ```

Read our documentation on viewing source code .