Skip to content

Instantly share code, notes, and snippets.

@d-date
Created September 3, 2018 19:07
Show Gist options
  • Save d-date/8ea4c14c8970aedec376be66e839d26e to your computer and use it in GitHub Desktop.
Save d-date/8ea4c14c8970aedec376be66e839d26e to your computer and use it in GitHub Desktop.
Very comfortable to understand function composition! #CodePiece #tryswiftnyc
precedencegroup FunctionApplication {
associativity: left
higherThan: AssignmentPrecedence
}
infix operator |>: FunctionApplication
func |> <A,B>(a: A, f: (A) -> B) -> B {
return f(a)
}
// 2.incr().square()
let x = 2 |> incr |> square // |> String.init
precedencegroup FunctionComposition {
associativity: left
higherThan: FunctionApplication
}
infix operator >>>: FunctionComposition
// f // (A) -> B
// >>>
// g // (B) -> C
// -> ================
// (A) -> C
func >>> <A, B, C>(
f: @escaping (A) -> B,
g: @escaping (B) -> C)
-> (A) -> C {
return { g(f($0)) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment