I noticed in some code in this sample that contained the >> operator:
let printTree =
tree >> Seq.iter (Seq.fold (+) "" >> printfn "%s")
What does the >> operator mean/do?
Thanks very much, now it is much clearer. Here's my example I generated to get the hang of it:
open System
open System.IO
let read_lines path = File.ReadAllLines(path) |> Array.to_list
let trim line = (string line).Trim()
let to_upper line = (string line).ToUpper()
let new_list = [ for line in read_lines "myText.txt" -> line |> (trim >> to_upper) ]
printf "%A" new_list
It's the function composition operator.
More info on Chris Smith's blogpost.
The composition operators,
<<
and>>
are use to join two functions such that the result of one becomes the input of the other. Since functions are also values, unless otherwise note, they are treated as such so that the following expressions are equivalent:f1 f2 f3 ... fn x = (..((f1 f2) f3) ... fn) x
Specifically,
f2, f3, ...fn
andx
are treated as values and are not evaluated prior to being passed as parameters to their preceding functions. Sometimes that is what you want but other times you want to indicate that theresult
of one function is to be the input of the other. This can be realized using the composition operators<<
and>>
thus:(f1 << f2 << f3 ... << fn) x = f1(f2(f3 ... (fn x )..)
Similarly
(fn >> ... f3 >> f2 >> f1) x = f1(f2(f3 ... (fn x )..)
Since the composition operator returns a function, the explicit parameter,
x
, is not required unlike in the pipe operatorsx |> fn ... |> f1
orf1 <| ... fn <| x
According to F# Symbol and Operator Reference it is Forward Function Composition operator.
That is function composition, used for partial application
The
>>
operator composes two functions, sox |> (g >> f) = x |> g |> f = f (g x)
. There's also another operator<<
which composes in the other direction, so that(f << g) x = f (g x)
, which may be more natural in some cases.