Skip to content

Commit 0508b30

Browse files
committed
Merge pull request #305 from CodaFi/cogito-ergo-product
Cogito ergo product
2 parents 95e0745 + daa202b commit 0508b30

File tree

14 files changed

+480
-93
lines changed

14 files changed

+480
-93
lines changed

Swiftz.xcodeproj/project.pbxproj

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
8240CD581C3A397800EF4D29 /* (null) in Sources */ = {isa = PBXBuildFile; };
6060
8240CD591C3A397800EF4D29 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 820248091C1BA8A50020B7EF /* Unit.swift */; };
6161
8240CD5A1C3A397800EF4D29 /* Writer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 828BB55B1B7E71B600D3327A /* Writer.swift */; };
62-
8240CD5B1C3A397800EF4D29 /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D66B1C066E6000D6289B /* Reader.swift */; };
6362
8240CD5C1C3A397800EF4D29 /* ArrayExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A88FDF1A71DFC5003D53CF /* ArrayExt.swift */; };
6463
8240CD5D1C3A397800EF4D29 /* ArrowExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F3175D1B805EA7007B80E2 /* ArrowExt.swift */; };
6564
8240CD5E1C3A397800EF4D29 /* CharacterExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A88FE61A71DFC5003D53CF /* CharacterExt.swift */; };
@@ -130,7 +129,6 @@
130129
8240CE0E1C3A425C00EF4D29 /* Those.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A8900B1A71DFC5003D53CF /* Those.swift */; };
131130
8240CE0F1C3A425C00EF4D29 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 820248091C1BA8A50020B7EF /* Unit.swift */; };
132131
8240CE101C3A425C00EF4D29 /* Writer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 828BB55B1B7E71B600D3327A /* Writer.swift */; };
133-
8240CE111C3A425C00EF4D29 /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D66B1C066E6000D6289B /* Reader.swift */; };
134132
8240CE121C3A425C00EF4D29 /* ArrayExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A88FDF1A71DFC5003D53CF /* ArrayExt.swift */; };
135133
8240CE131C3A425C00EF4D29 /* ArrowExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F3175D1B805EA7007B80E2 /* ArrowExt.swift */; };
136134
8240CE141C3A425C00EF4D29 /* CharacterExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A88FE61A71DFC5003D53CF /* CharacterExt.swift */; };
@@ -165,6 +163,14 @@
165163
82E5A65D1B53666000255137 /* NonEmptyListSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82E5A65B1B53666000255137 /* NonEmptyListSpec.swift */; };
166164
82EBC6AF1B7AA4CC004AF7B6 /* Ratio.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82EBC6AE1B7AA4CC004AF7B6 /* Ratio.swift */; };
167165
82EBC6B01B7AA4CC004AF7B6 /* Ratio.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82EBC6AE1B7AA4CC004AF7B6 /* Ratio.swift */; };
166+
82F251181CD891A500D292AE /* Monoidal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F251171CD891A500D292AE /* Monoidal.swift */; };
167+
82F251191CD891A500D292AE /* Monoidal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F251171CD891A500D292AE /* Monoidal.swift */; };
168+
82F2511A1CD891A500D292AE /* Monoidal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F251171CD891A500D292AE /* Monoidal.swift */; };
169+
82F2511B1CD891A500D292AE /* Monoidal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F251171CD891A500D292AE /* Monoidal.swift */; };
170+
82F2511D1CD89D4E00D292AE /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F2511C1CD89D4E00D292AE /* Reader.swift */; };
171+
82F2511E1CD89D4E00D292AE /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F2511C1CD89D4E00D292AE /* Reader.swift */; };
172+
82F2511F1CD89D4E00D292AE /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F2511C1CD89D4E00D292AE /* Reader.swift */; };
173+
82F251201CD89D4E00D292AE /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F2511C1CD89D4E00D292AE /* Reader.swift */; };
168174
82F3175E1B805EA7007B80E2 /* ArrowExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F3175D1B805EA7007B80E2 /* ArrowExt.swift */; };
169175
82F3175F1B805EA7007B80E2 /* ArrowExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F3175D1B805EA7007B80E2 /* ArrowExt.swift */; };
170176
82F317631B81090B007B80E2 /* ArrowExtSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F317621B81090B007B80E2 /* ArrowExtSpec.swift */; };
@@ -269,8 +275,6 @@
269275
84DF75CF1B0BD1D400C912B0 /* TupleExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A8900D1A71DFC5003D53CF /* TupleExt.swift */; };
270276
84DF76301B0BDCE800C912B0 /* Swiftz.h in Headers */ = {isa = PBXBuildFile; fileRef = 84DF762F1B0BDCE800C912B0 /* Swiftz.h */; settings = {ATTRIBUTES = (Public, ); }; };
271277
84DF76311B0BDCE800C912B0 /* Swiftz.h in Headers */ = {isa = PBXBuildFile; fileRef = 84DF762F1B0BDCE800C912B0 /* Swiftz.h */; settings = {ATTRIBUTES = (Public, ); }; };
272-
DE37D66C1C066E6000D6289B /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D66B1C066E6000D6289B /* Reader.swift */; };
273-
DE37D66D1C066E6000D6289B /* Reader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D66B1C066E6000D6289B /* Reader.swift */; };
274278
DE37D6711C06BE9C00D6289B /* ReaderSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D6701C06BE9C00D6289B /* ReaderSpec.swift */; };
275279
DE37D6721C06BE9C00D6289B /* ReaderSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE37D6701C06BE9C00D6289B /* ReaderSpec.swift */; };
276280
DEC686D71C0260C70057B393 /* DictionaryExtSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC686D61C0260C70057B393 /* DictionaryExtSpec.swift */; };
@@ -409,6 +413,8 @@
409413
82E51BA41B5D96BD003CA361 /* ProxySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProxySpec.swift; sourceTree = "<group>"; usesTabs = 1; };
410414
82E5A65B1B53666000255137 /* NonEmptyListSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NonEmptyListSpec.swift; sourceTree = "<group>"; usesTabs = 1; };
411415
82EBC6AE1B7AA4CC004AF7B6 /* Ratio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Ratio.swift; sourceTree = "<group>"; usesTabs = 1; };
416+
82F251171CD891A500D292AE /* Monoidal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Monoidal.swift; sourceTree = "<group>"; };
417+
82F2511C1CD89D4E00D292AE /* Reader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reader.swift; sourceTree = "<group>"; };
412418
82F3175D1B805EA7007B80E2 /* ArrowExt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrowExt.swift; sourceTree = "<group>"; usesTabs = 1; };
413419
82F317621B81090B007B80E2 /* ArrowExtSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrowExtSpec.swift; sourceTree = "<group>"; usesTabs = 1; };
414420
8480AB4D1A7B448A00C6162D /* Sections.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Sections.swift; path = Carthage/Checkouts/Swiftx/Swiftx/Sections.swift; sourceTree = SOURCE_ROOT; usesTabs = 1; };
@@ -465,7 +471,6 @@
465471
84DF751E1B0BD17700C912B0 /* Swiftz.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swiftz.framework; sourceTree = BUILT_PRODUCTS_DIR; };
466472
84DF75281B0BD17700C912B0 /* Swiftz-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Swiftz-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
467473
84DF762F1B0BDCE800C912B0 /* Swiftz.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Swiftz.h; path = Swiftz/Swiftz.h; sourceTree = "<group>"; usesTabs = 1; };
468-
DE37D66B1C066E6000D6289B /* Reader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reader.swift; sourceTree = "<group>"; };
469474
DE37D6701C06BE9C00D6289B /* ReaderSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReaderSpec.swift; sourceTree = "<group>"; };
470475
DEC686D61C0260C70057B393 /* DictionaryExtSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryExtSpec.swift; sourceTree = "<group>"; };
471476
/* End PBXFileReference section */
@@ -627,9 +632,12 @@
627632
84A88FDE1A71DFC5003D53CF /* Applicative.swift */,
628633
84A88FE11A71DFC5003D53CF /* Arrow.swift */,
629634
84A88FE41A71DFC5003D53CF /* Category.swift */,
635+
84A88FE81A71DFC5003D53CF /* Copointed.swift */,
630636
84A88FE71A71DFC5003D53CF /* Comonad.swift */,
631637
84A88FE91A71DFC5003D53CF /* Curry.swift */,
632638
84A88FFB1A71DFC5003D53CF /* Monad.swift */,
639+
82F251171CD891A500D292AE /* Monoidal.swift */,
640+
84A890041A71DFC5003D53CF /* Pointed.swift */,
633641
);
634642
name = Control;
635643
sourceTree = "<group>";
@@ -655,7 +663,6 @@
655663
84A88FE21A71DFC5003D53CF /* Bifunctor.swift */,
656664
84A88FE31A71DFC5003D53CF /* Bounded.swift */,
657665
8273EFA61B84D7DA008BA037 /* Const.swift */,
658-
84A88FE81A71DFC5003D53CF /* Copointed.swift */,
659666
8496F0E61B37571F00896D26 /* Foldable.swift */,
660667
84A88FEC1A71DFC5003D53CF /* Function.swift */,
661668
84A88FED1A71DFC5003D53CF /* Functor.swift */,
@@ -666,17 +673,16 @@
666673
84A88FFC1A71DFC5003D53CF /* Monoid.swift */,
667674
84A88FFE1A71DFC5003D53CF /* NonEmptyList.swift */,
668675
84A890011A71DFC5003D53CF /* Num.swift */,
669-
84A890041A71DFC5003D53CF /* Pointed.swift */,
670676
82E51B9F1B5D9413003CA361 /* Proxy.swift */,
671677
82EBC6AE1B7AA4CC004AF7B6 /* Ratio.swift */,
678+
82F2511C1CD89D4E00D292AE /* Reader.swift */,
672679
84A890071A71DFC5003D53CF /* Semigroup.swift */,
673680
84961FB41A998976004A186A /* State.swift */,
674681
825C0E421B85681C0026E738 /* Stream.swift */,
675682
84A8900A1A71DFC5003D53CF /* SYB.swift */,
676683
84A8900B1A71DFC5003D53CF /* Those.swift */,
677684
820248091C1BA8A50020B7EF /* Unit.swift */,
678685
828BB55B1B7E71B600D3327A /* Writer.swift */,
679-
DE37D66B1C066E6000D6289B /* Reader.swift */,
680686
);
681687
name = Data;
682688
sourceTree = "<group>";
@@ -1057,6 +1063,7 @@
10571063
8240CD381C3A397800EF4D29 /* Error.swift in Sources */,
10581064
8240CD391C3A397800EF4D29 /* Nothing.swift in Sources */,
10591065
8240CD3A1C3A397800EF4D29 /* Optional.swift in Sources */,
1066+
82F2511A1CD891A500D292AE /* Monoidal.swift in Sources */,
10601067
8240CD3B1C3A397800EF4D29 /* Sections.swift in Sources */,
10611068
8240CD3C1C3A397800EF4D29 /* Applicative.swift in Sources */,
10621069
8240CD3D1C3A397800EF4D29 /* Arrow.swift in Sources */,
@@ -1080,6 +1087,7 @@
10801087
8240CD4F1C3A397800EF4D29 /* Num.swift in Sources */,
10811088
8240CD501C3A397800EF4D29 /* Pointed.swift in Sources */,
10821089
8240CD511C3A397800EF4D29 /* Proxy.swift in Sources */,
1090+
82F2511F1CD89D4E00D292AE /* Reader.swift in Sources */,
10831091
8240CD521C3A397800EF4D29 /* Ratio.swift in Sources */,
10841092
8240CD531C3A397800EF4D29 /* Semigroup.swift in Sources */,
10851093
8240CD541C3A397800EF4D29 /* State.swift in Sources */,
@@ -1089,7 +1097,6 @@
10891097
8240CD581C3A397800EF4D29 /* (null) in Sources */,
10901098
8240CD591C3A397800EF4D29 /* Unit.swift in Sources */,
10911099
8240CD5A1C3A397800EF4D29 /* Writer.swift in Sources */,
1092-
8240CD5B1C3A397800EF4D29 /* Reader.swift in Sources */,
10931100
8240CD5C1C3A397800EF4D29 /* ArrayExt.swift in Sources */,
10941101
8240CD5D1C3A397800EF4D29 /* ArrowExt.swift in Sources */,
10951102
8240CD5E1C3A397800EF4D29 /* CharacterExt.swift in Sources */,
@@ -1145,7 +1152,9 @@
11451152
8240CDF31C3A425C00EF4D29 /* Applicative.swift in Sources */,
11461153
8240CDF41C3A425C00EF4D29 /* Arrow.swift in Sources */,
11471154
8240CDF51C3A425C00EF4D29 /* Category.swift in Sources */,
1155+
82F2511B1CD891A500D292AE /* Monoidal.swift in Sources */,
11481156
8240CDF61C3A425C00EF4D29 /* Comonad.swift in Sources */,
1157+
82F251201CD89D4E00D292AE /* Reader.swift in Sources */,
11491158
8240CDF71C3A425C00EF4D29 /* Curry.swift in Sources */,
11501159
8240CDF81C3A425C00EF4D29 /* Monad.swift in Sources */,
11511160
8240CDF91C3A425C00EF4D29 /* Bifunctor.swift in Sources */,
@@ -1172,7 +1181,6 @@
11721181
8240CE0E1C3A425C00EF4D29 /* Those.swift in Sources */,
11731182
8240CE0F1C3A425C00EF4D29 /* Unit.swift in Sources */,
11741183
8240CE101C3A425C00EF4D29 /* Writer.swift in Sources */,
1175-
8240CE111C3A425C00EF4D29 /* Reader.swift in Sources */,
11761184
8240CE121C3A425C00EF4D29 /* ArrayExt.swift in Sources */,
11771185
8240CE131C3A425C00EF4D29 /* ArrowExt.swift in Sources */,
11781186
8240CE141C3A425C00EF4D29 /* CharacterExt.swift in Sources */,
@@ -1200,7 +1208,9 @@
12001208
82E51BA01B5D9413003CA361 /* Proxy.swift in Sources */,
12011209
84A8904A1A71DFE5003D53CF /* Either.swift in Sources */,
12021210
82EBC6AF1B7AA4CC004AF7B6 /* Ratio.swift in Sources */,
1211+
82F251181CD891A500D292AE /* Monoidal.swift in Sources */,
12031212
84A890201A71DFC5003D53CF /* HList.swift in Sources */,
1213+
82F2511D1CD89D4E00D292AE /* Reader.swift in Sources */,
12041214
84A890181A71DFC5003D53CF /* Copointed.swift in Sources */,
12051215
84A8900E1A71DFC5003D53CF /* Applicative.swift in Sources */,
12061216
84A8903B1A71DFC5003D53CF /* Those.swift in Sources */,
@@ -1230,7 +1240,6 @@
12301240
8496F0E71B37571F00896D26 /* Foldable.swift in Sources */,
12311241
84A890391A71DFC5003D53CF /* StringExt.swift in Sources */,
12321242
84A8904D1A71DFE5003D53CF /* Nothing.swift in Sources */,
1233-
DE37D66C1C066E6000D6289B /* Reader.swift in Sources */,
12341243
84A890131A71DFC5003D53CF /* Bounded.swift in Sources */,
12351244
84A890141A71DFC5003D53CF /* Category.swift in Sources */,
12361245
84A890481A71DFE5003D53CF /* Array.swift in Sources */,
@@ -1283,7 +1292,9 @@
12831292
84DF75A91B0BD1D400C912B0 /* Category.swift in Sources */,
12841293
82E51BA11B5D9413003CA361 /* Proxy.swift in Sources */,
12851294
84DF75AA1B0BD1D400C912B0 /* Comonad.swift in Sources */,
1295+
82F251191CD891A500D292AE /* Monoidal.swift in Sources */,
12861296
82EBC6B01B7AA4CC004AF7B6 /* Ratio.swift in Sources */,
1297+
82F2511E1CD89D4E00D292AE /* Reader.swift in Sources */,
12871298
84DF75AB1B0BD1D400C912B0 /* Curry.swift in Sources */,
12881299
84DF75B21B0BD1D400C912B0 /* Monad.swift in Sources */,
12891300
84DF75B51B0BD1D400C912B0 /* Bifunctor.swift in Sources */,
@@ -1313,7 +1324,6 @@
13131324
84DF75C51B0BD1D400C912B0 /* SYB.swift in Sources */,
13141325
84DF75C61B0BD1D400C912B0 /* Those.swift in Sources */,
13151326
84DF75C81B0BD1D400C912B0 /* ArrayExt.swift in Sources */,
1316-
DE37D66D1C066E6000D6289B /* Reader.swift in Sources */,
13171327
84DF75C91B0BD1D400C912B0 /* CharacterExt.swift in Sources */,
13181328
84DF75CA1B0BD1D400C912B0 /* DictionaryExt.swift in Sources */,
13191329
84DF75CD1B0BD1D400C912B0 /* OptionalExt.swift in Sources */,

Swiftz/ArrayExt.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ extension Array : Applicative {
3737
}
3838
}
3939

40+
extension Array : Cartesian {
41+
public typealias FTOP = Array<()>
42+
public typealias FTAB = Array<(A, B)>
43+
public typealias FTABC = Array<(A, B, C)>
44+
public typealias FTABCD = Array<(A, B, C, D)>
45+
46+
public static var unit : Array<()> { return [()] }
47+
public func product<B>(r : Array<B>) -> Array<(A, B)> {
48+
return self.mzip(r)
49+
}
50+
51+
public func product<B, C>(r : Array<B>, _ s : Array<C>) -> Array<(A, B, C)> {
52+
return { x in { y in { z in (x, y, z) } } } <^> self <*> r <*> s
53+
}
54+
55+
public func product<B, C, D>(r : Array<B>, _ s : Array<C>, _ t : Array<D>) -> Array<(A, B, C, D)> {
56+
return { x in { y in { z in { w in (x, y, z, w) } } } } <^> self <*> r <*> s <*> t
57+
}
58+
}
59+
4060
extension Array : ApplicativeOps {
4161
public typealias C = Any
4262
public typealias FC = [C]
@@ -96,7 +116,7 @@ extension Array : MonadPlus {
96116
}
97117

98118
extension Array : MonadZip {
99-
public typealias FTAB = [(A, B)]
119+
public typealias FTABL = [(A, B)]
100120

101121
public func mzip<B>(ma : [B]) -> [(A, B)] {
102122
return [(A, B)](zip(self, ma))

Swiftz/EitherExt.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,71 @@ extension Either : Applicative {
5656
}
5757
}
5858

59+
extension Either : Cartesian {
60+
public typealias FTOP = Either<L, ()>
61+
public typealias FTAB = Either<L, (A, B)>
62+
public typealias FTABC = Either<L, (A, B, C)>
63+
public typealias FTABCD = Either<L, (A, B, C, D)>
64+
65+
public static var unit : Either<L, ()> { return .Right(()) }
66+
public func product<B>(r : Either<L, B>) -> Either<L, (A, B)> {
67+
switch self {
68+
case let .Left(c):
69+
return .Left(c)
70+
case let .Right(d):
71+
switch r {
72+
case let .Left(e):
73+
return .Left(e)
74+
case let .Right(f):
75+
return .Right((d, f))
76+
}
77+
}
78+
}
79+
80+
public func product<B, C>(r : Either<L, B>, _ s : Either<L, C>) -> Either<L, (A, B, C)> {
81+
switch self {
82+
case let .Left(c):
83+
return .Left(c)
84+
case let .Right(d):
85+
switch r {
86+
case let .Left(e):
87+
return .Left(e)
88+
case let .Right(f):
89+
switch s {
90+
case let .Left(g):
91+
return .Left(g)
92+
case let .Right(h):
93+
return .Right((d, f, h))
94+
}
95+
}
96+
}
97+
}
98+
99+
public func product<B, C, D>(r : Either<L, B>, _ s : Either<L, C>, _ t : Either<L, D>) -> Either<L, (A, B, C, D)> {
100+
switch self {
101+
case let .Left(c):
102+
return .Left(c)
103+
case let .Right(d):
104+
switch r {
105+
case let .Left(e):
106+
return .Left(e)
107+
case let .Right(f):
108+
switch s {
109+
case let .Left(g):
110+
return .Left(g)
111+
case let .Right(h):
112+
switch t {
113+
case let .Left(i):
114+
return .Left(i)
115+
case let .Right(j):
116+
return .Right((d, f, h, j))
117+
}
118+
}
119+
}
120+
}
121+
}
122+
}
123+
59124
extension Either : ApplicativeOps {
60125
public typealias C = Any
61126
public typealias FC = Either<L, C>

Swiftz/Identity.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ extension Identity : Applicative {
5555
}
5656
}
5757

58+
extension Identity : Cartesian {
59+
public typealias FTOP = Identity<()>
60+
public typealias FTAB = Identity<(A, B)>
61+
public typealias FTABC = Identity<(A, B, C)>
62+
public typealias FTABCD = Identity<(A, B, C, D)>
63+
64+
public static var unit : Identity<()> { return Identity<()>(()) }
65+
public func product<B>(r : Identity<B>) -> Identity<(A, B)> {
66+
return self.mzip(r)
67+
}
68+
69+
public func product<B, C>(r : Identity<B>, _ s : Identity<C>) -> Identity<(A, B, C)> {
70+
return { x in { y in { z in (x, y, z) } } } <^> self <*> r <*> s
71+
}
72+
73+
public func product<B, C, D>(r : Identity<B>, _ s : Identity<C>, _ t : Identity<D>) -> Identity<(A, B, C, D)> {
74+
return { x in { y in { z in { w in (x, y, z, w) } } } } <^> self <*> r <*> s <*> t
75+
}
76+
}
77+
5878
extension Identity : ApplicativeOps {
5979
public typealias C = Any
6080
public typealias FC = Identity<C>
@@ -111,7 +131,7 @@ public func <<-<< <A, B, C>(g : B -> Identity<C>, f : A -> Identity<B>) -> (A ->
111131
}
112132

113133
extension Identity : MonadZip {
114-
public typealias FTAB = Identity<(A, B)>
134+
public typealias FTABL = Identity<(A, B)>
115135

116136
public func mzip<B>(other : Identity<B>) -> Identity<(A, B)> {
117137
return Identity<(A, B)>((self.runIdentity, other.runIdentity))

Swiftz/List.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,26 @@ public func <*> <A, B>(f : List<(A -> B)>, l : List<A>) -> List<B> {
659659
return l.ap(f)
660660
}
661661

662+
extension List : Cartesian {
663+
public typealias FTOP = List<()>
664+
public typealias FTAB = List<(A, B)>
665+
public typealias FTABC = List<(A, B, C)>
666+
public typealias FTABCD = List<(A, B, C, D)>
667+
668+
public static var unit : List<()> { return [()] }
669+
public func product<B>(r : List<B>) -> List<(A, B)> {
670+
return self.mzip(r)
671+
}
672+
673+
public func product<B, C>(r : List<B>, _ s : List<C>) -> List<(A, B, C)> {
674+
return List.liftA3({ x in { y in { z in (x, y, z) } } })(self)(r)(s)
675+
}
676+
677+
public func product<B, C, D>(r : List<B>, _ s : List<C>, _ t : List<D>) -> List<(A, B, C, D)> {
678+
return { x in { y in { z in { w in (x, y, z, w) } } } } <^> self <*> r <*> s <*> t
679+
}
680+
}
681+
662682
extension List : Monad {
663683
public func bind<B>(f : A -> List<B>) -> List<B> {
664684
return self.concatMap(f)
@@ -710,3 +730,19 @@ public func sequence<A>(ms: [List<A>]) -> List<[A]> {
710730
}
711731
})
712732
}
733+
734+
extension List : MonadZip {
735+
public typealias FTABL = List<(A, B)>
736+
737+
public func mzip<B>(ma : List<B>) -> List<(A, B)> {
738+
return List<(A, B)>(fromArray: zip(self, ma).map(identity))
739+
}
740+
741+
public func mzipWith<B, C>(other : List<B>, _ f : A -> B -> C) -> List<C> {
742+
return self.mzip(other).map(uncurry(f))
743+
}
744+
745+
public static func munzip<B>(ftab : List<(A, B)>) -> (List<A>, List<B>) {
746+
return (ftab.map(fst), ftab.map(snd))
747+
}
748+
}

Swiftz/Monad.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ public protocol MonadZip : Monad {
4040
associatedtype FC = K1<C>
4141

4242
/// A Monad containing a zipped tuple.
43-
associatedtype FTAB = K1<(A, B)>
43+
associatedtype FTABL = K1<(A, B)>
4444

4545
/// Zip for monads.
46-
func mzip(_ : FB) -> FTAB
46+
func mzip(_ : FB) -> FTABL
4747

4848
/// ZipWith for monads.
4949
func mzipWith(_ : FB, _ : A -> B -> C) -> FC
5050

5151
/// Unzip for monads.
52-
static func munzip(_ : FTAB) -> (Self, FB)
52+
static func munzip(_ : FTABL) -> (Self, FB)
5353
}
5454

5555
/// A monoid for monads.

0 commit comments

Comments
 (0)