We can create our custom operators and combine them. It is not a common practice but can be useful in some cases.
Let’s start by creating an operator similar to the UNIX pipeline |:
echo “Hello” | wc -l
This operator will redirect the output from the left, as an input for the right closure:
precedencegroup ForwardApplication {
associativity: left
}
infix operator |: ForwardApplication
func |<A, B>(_ x: A, f: (A) -> B) -> B {
f(x)
}
3 | String.init //"3"
String(3) //"3"
Usually we will use String(3), but we can use | to forward the left value as an input for the right function.
We can indicate with precedence group how to evaluate in cases where we have multiple operators.
3 | String.init | someOtherFunctionWithSameInputOutput