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
| XCTAssert( | |
| AssociativeLaw<String>.verify(a: "hello", b: " ", c: "wolrd!", operation: +) | |
| ) |
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
| XCTAssert( | |
| AssociativeLaw<String>.verify(a: "hello", b: " ", c: "wolrd!", operation: <>) | |
| ) |
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
| /// | |
| /// Associative Law | |
| /// | |
| /// **Associativity** is the condition that the 2 ways to use a "binary composition" of morphisms to compose a sequence of 3 morphisms "are equal". | |
| /// | |
| /// - Associative through **Addition**: `a + (b + c) = (a + b) + c` | |
| /// - Associative through **Multiplication**: `a * (b * c) = (a * b) * c` | |
| /// | |
| public enum AssociativeLaw<Element: Equatable> {} |
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
| [1, 2, 3] <> [4, 5, 6] | |
| // prints out: [1, 2, 3, 4, 5, 6] |
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
| extension Array: Semigroup { | |
| public static func <> (left: Array, right: Array) -> Array { | |
| left + right | |
| } | |
| } |
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
| "Pac" <> "Man" | |
| // prints out: "PacMan" |
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
| extension String: Semigroup { | |
| public static func <> (left: String, right: String) -> String { | |
| left + right | |
| } | |
| } |
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
| infix operator <> : AdditionPrecedence | |
| public protocol Semigroup { | |
| static func <> (left: Self, right: Self) -> Self | |
| } |
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
| func compose<A, B, C>( | |
| f: @escaping (A) -> B, // f: A -> B | |
| g: @escaping (B) -> C // g: B -> C | |
| ) -> (A) -> C { // h: A -> C = g • f | |
| return { a in | |
| g(f(a)) // g • f | |
| } | |
| } | |
| // ... then |
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
| func string(from int: Int) -> String { | |
| String(int) | |
| } |