-
-
Save jasdev/6514c9e87e8554154cbc2cd888153d26 to your computer and use it in GitHub Desktop.
Endo Semigroup conformance.
This file contains 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 <>: SemigroupPrecedence | |
precedencegroup SemigroupPrecedence { | |
higherThan: MultiplicationPrecedence | |
associativity: left | |
} | |
protocol Semigroup { | |
static func <> (lhs: Self, rhs: Self) -> Self | |
} | |
struct Endo<A> { | |
let call: (A) -> A | |
func callAsFunction(_ a: A) -> A { call(a) } | |
} | |
extension Endo: Semigroup { | |
static func <> (lhs: Endo<A>, rhs: Endo<A>) -> Endo<A> { | |
Endo { rhs(lhs($0)) } // You might see folks use | |
// `lhs.call >>> rhs.call` here à la Point-Free’s Prelude: | |
// https://github.com/pointfreeco/swift-prelude/blob/0ec0a56bb99911647d2081f99a6f6c9699a6a646/Sources/Prelude/Endo.swift#L9-L13 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment