/endo-semigroup.swift Secret
Last active
July 20, 2020 03:01
Star
You must be signed in to star a gist
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