Created
July 30, 2015 01:49
-
-
Save jbrownson/248b99919f447596bb75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // algebra - the study of mathematical symbols and the rules for manipulating these symbols | |
| // [https://en.wikipedia.org/wiki/Algebra] | |
| // composition - combining parts or elements to form a whole. | |
| // [http://dictionary.reference.com/browse/composition] | |
| // algebraic data type - a kind of composite type, i.e. a type formed by combining other types | |
| // [https://en.wikipedia.org/wiki/Algebraic_data_type] | |
| enum One { | |
| case One | |
| } | |
| ([ .One | |
| ] as [One]).count | |
| func uselessArgument(one: One) {} | |
| func uselessReturn() -> One { return .One } | |
| // like Swift's () | |
| enum Two { | |
| case One | |
| case Two | |
| } | |
| ([ .One, | |
| .Two | |
| ] as [Two]).count | |
| // like Bool | |
| enum Zero {} | |
| ([ /* No values */ | |
| ] as [Zero]).count | |
| func uncallable(zero: Zero) {} | |
| func canNeverReturn() -> Zero { return canNeverReturn() } | |
| do { | |
| enum Sum { | |
| case A(One) | |
| case B(Two) | |
| } | |
| ([ .A(.One), | |
| .B(.One), | |
| .B(.Two) | |
| ] as [Sum]).count | |
| } | |
| do { | |
| enum Sum { | |
| case A(Two) | |
| case B(Two) | |
| } | |
| ([ .A(.One), | |
| .A(.Two), | |
| .B(.One), | |
| .B(.Two) | |
| ] as [Sum]).count | |
| } | |
| do { | |
| enum Sum { | |
| case A(Two) | |
| case B(Zero) | |
| } | |
| ([ .A(.One), | |
| .A(.Two) | |
| ] as [Sum]).count | |
| } | |
| // cases add the total values of the composed types like + | |
| do { | |
| enum Mul { | |
| case A(One, Two) | |
| } | |
| ([ .A(.One, .One), | |
| .A(.One, .Two) | |
| ] as [Mul]).count | |
| } | |
| do { | |
| enum Mul { | |
| case A(Two, Two) | |
| } | |
| ([ .A(.One, .One), | |
| .A(.One, .Two), | |
| .A(.Two, .One), | |
| .A(.Two, .Two) | |
| ] as [Mul]).count | |
| } | |
| do { | |
| enum Mul { | |
| case A(One, Two, Zero) | |
| } | |
| ([ /* No values */ | |
| ] as [Mul]).count | |
| } | |
| // associated parameters multiply the total values of the composed types, like * | |
| do { | |
| enum Foo { | |
| case A(One, Two) | |
| case B(Two, Two) | |
| } | |
| ([ .A(.One, .One), | |
| .A(.One, .Two), | |
| .B(.One, .One), | |
| .B(.One, .Two), | |
| .B(.Two, .One), | |
| .B(.Two, .Two) | |
| ] as [Foo]).count | |
| } | |
| UInt8.max // UInt8 is a lot like an enum w/ 256 entries (don't forget 0) | |
| let allUInt8s = Array(0..<UInt8.max) + [UInt8.max] | |
| do { | |
| enum Foo { | |
| case A(Two, One) | |
| case B(UInt8) | |
| } | |
| (([ .A(.One, .One), | |
| .A(.Two, .One) | |
| ] as [Foo]) + allUInt8s.map {.B($0)}).count | |
| } | |
| do { | |
| enum Foo { | |
| case A(Two, UInt8, One) | |
| case B(UInt8, Zero) | |
| case C(UInt8, UInt8) | |
| } | |
| (allUInt8s.flatMap { | |
| [Foo.A(.One, $0, .One), Foo.A(.Two, $0, .One)]} + | |
| allUInt8s.flatMap { | |
| a in allUInt8s.map { | |
| b in Foo.C(a, b)}}).count | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment