Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active July 20, 2020 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/6514c9e87e8554154cbc2cd888153d26 to your computer and use it in GitHub Desktop.
Save jasdev/6514c9e87e8554154cbc2cd888153d26 to your computer and use it in GitHub Desktop.
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