Skip to content

Instantly share code, notes, and snippets.

View eMdOS's full-sized avatar

Emilio Ojeda eMdOS

  • Zapopan, Jalisco
View GitHub Profile
XCTAssert(
AssociativeLaw<String>.verify(a: "hello", b: " ", c: "wolrd!", operation: +)
)
XCTAssert(
AssociativeLaw<String>.verify(a: "hello", b: " ", c: "wolrd!", operation: <>)
)
///
/// 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> {}
[1, 2, 3] <> [4, 5, 6]
// prints out: [1, 2, 3, 4, 5, 6]
extension Array: Semigroup {
public static func <> (left: Array, right: Array) -> Array {
left + right
}
}
"Pac" <> "Man"
// prints out: "PacMan"
extension String: Semigroup {
public static func <> (left: String, right: String) -> String {
left + right
}
}
infix operator <> : AdditionPrecedence
public protocol Semigroup {
static func <> (left: Self, right: Self) -> Self
}
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
func string(from int: Int) -> String {
String(int)
}