When I first learned about the backwards pipe I was excited and immediately thought that it was rather cool. If you don’t know what a pipe is, it simply allows you to chain methods making the return value of one the input parameter of the next one. In F# it’s commonly seen as |> for forward piping, and <| for backwards piping. After getting over the ‘this is so cool’-feeling I realized I had no idea what to use it for. So in the air between Oslo and Stavanger (Norway) I decided to think about it a little bit and share what I know so far. F-sharpies might want to add to this post if you know some more, I’m a beginner in terms of F#.
The backwards pipe operator, <|, applies a function on the left of the operator to the value on the right of the operator.
let sum nrX nrY = nrX + nrY
// No parenthesis…yay! (?)
printfn “If we add we get…. %d” <| 2 + 3
// Oh noes.. parenthesis *sad face*
printfn “If we add we get…. %d” (2 + 3)
// Don’t get me started on double and triple reverse pipe operators
sum <|| (2,3)
In short, the only application I can find for the backwards pipe in F# is avoiding trailing parenthesis (but not always). Can this be true? This is it? I’m sure there must be more. Feel free to share if you know =)