The pipe operator is just syntactic sugar for chain calls. This is very similar to how linq expressions are expressed in C #.
Explanation from here :
Straight pipe operator I love this guy. The straight pipe operator is simply defined as:
let (|>) xf = fx
And it has a signature like:
'a -> ('a -> 'b) -> 'b
Which means: a given generic type 'a and a function that takes' a and returns a' b, then returns the application of the function to the input.
Instead of explaining this, let me give an example of where it can be used:
// Take a number, square it, then convert it to a string, then reverse that string let square x = x * x let toStr (x : int) = x.ToString() let rev (x : string) = new String(Array.rev (x.ToCharArray())) // 512 -> 1024 -> "1024" -> "4201" let result = rev (toStr (square 512))
The code is very simple, but note how disobedient the syntax looks. All we want to do is take the result of one calculation and pass it on to the next calculation. We could rewrite it by introducing a number of new variables:
let step1 = square 512 let step2 = toStr step1 let step3 = rev step2 let result = step3
But now you need to keep all these temporary variables straight. What the operator (|>) does is take a value and "redirect it" to the function, essentially letting you specify a function parameter before calling the function. This greatly simplifies the F # code, allowing you to combine functions together when the result of one is passed to the next. Therefore, to use the same example, the code can be written clearly:
let result = 512 |> square |> toStr |> rev
Edit
In F #, what you really do with the method call takes a function and then applies it to the next parameter, so in your example, this will be List.map (fun x -> x * x * x) applied to [2;4;6] . Everything the pipe operator does takes the parameters in the reverse order, and then the application reverses them.
Function
: List.map (fun x -> x * x * x) parameter: [2;4;6]
Standard F # Call Syntax: fg
Inverse F # Call Syntax: gf
Standard:
let var = List.map (fun x -> x * x * x) [2;4;6]
Inverted:
let var = [2;4;6] |> List.map (fun x -> x * x * x)