Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active Jul 20, 2020
Embed
What would you like to do?
Endo Semigroup conformance.
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