I’m starting a new series, mostly just me taking notes while I go through the PointFree videos.
The pipe operator
The |>
operator is defined as:
precedencegroup ForwardApplication {
associativity: left
}
infix operator |>: ForwardApplication
func |> <A, B>(x: A, f: (A) -> B) -> B {
return f(x)
}
It can be used as::
func incr(_ x: Int) -> Int {
return x + 1
}
func square(_ x: Int) -> Int {
return x * x
}
2 |> incr |> square
Function composition operator
This operator is defined as:
precedencegroup ForwardComposition {
higherThan: ForwardApplication
associativity: left
}
infix operator >>>: ForwardComposition
func >>> <A, B, C>(_ f: @escaping (A) -> B, _ g: @escaping (B) -> C) -> ((A) -> C) {
return { a in g(f(a)) }
}
Now we can compose two functions(given that the types match) into a new one by:
2 |> incr >>> square
[1, 2, 3].map(square >>> incr)
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email