Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active July 20, 2020 03:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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