Skip to content

Instantly share code, notes, and snippets.

@tkersey
Created March 2, 2023 01:09
  • Star 1 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?
// 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