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
// MARK: - Precedence | |
precedencegroup ForwardApplication { | |
associativity: left | |
higherThan: AssignmentPrecedence | |
} | |
precedencegroup ForwardComposition { | |
associativity: left | |
higherThan: ForwardApplication | |
} | |
// MARK: - Infix operators | |
infix operator |> : ForwardApplication | |
infix operator >>>: ForwardComposition | |
// MARK: - Apply | |
public func |> <A, B>(x: A, f: (A) -> B) -> B { | |
f(x) | |
} | |
// MARK: - Compose | |
public func >>> <A, B, C>(f: @escaping (A) -> B, g: @escaping (B) -> C) -> (A) -> C { | |
{ g(f($0)) } | |
} | |
// MARK: - Identity | |
@inline(__always) public func id<A>(_ a: A) -> A { a } | |
// MARK: - Initial object | |
// `Never` is the Swift standard library | |
// MARK: - Terminal object | |
public let unit: Void = () | |
// MARK: - Absurd | |
public func absurd<A>(_ never: Never) -> A {} | |
// MARK: - Const | |
@inline(__always) public func const<A, B>(_ a: A) -> (B) -> A { | |
{ _ in a } | |
} | |
// MARK: - Flip | |
public func flip<A, B, C>(_ f: @escaping (A) -> (B) -> C) -> (B) -> (A) -> C { | |
{ a in { b in f(b)(a) } } | |
} | |
// MARK: - Curry | |
public func curry<A, B, C>(_ f: @escaping (A, B) -> C) -> (A) -> (B) -> C { | |
{ (a: A) -> (B) -> C in { (b: B) -> C in f(a, b) } } | |
} | |
// MARK: - Uncurry | |
public func uncurry<A, B, C>(_ f: @escaping (A) -> (B) -> C) -> (A, B) -> C { | |
{ a, b in f(a)(b) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment