Created
September 3, 2018 19:07
-
-
Save d-date/8ea4c14c8970aedec376be66e839d26e to your computer and use it in GitHub Desktop.
Very comfortable to understand function composition! #CodePiece #tryswiftnyc
This file contains hidden or 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
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