In F# what does the >> operator mean?

15,036

Solution 1

It's the function composition operator.

More info on Chris Smith's blogpost.

Introducing the Function Composition operator (>>):

let inline (>>) f g x = g(f x)

Which reads as: given two functions, f and g, and a value, x, compute the result of f of x and pass that result to g. The interesting thing here is that you can curry the (>>) function and only pass in parameters f and g, the result is a function which takes a single parameter and produces the result g ( f ( x ) ).

Here's a quick example of composing a function out of smaller ones:

let negate x = x * -1 
let square x = x * x 
let print  x = printfn "The number is: %d" x
let square_negate_then_print = square >> negate >> print 
asserdo square_negate_then_print 2

When executed prints ‘-4’.

Solution 2

The >> operator composes two functions, so x |> (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.

Solution 3

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 and x 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 the result 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 operators x |> fn ... |> f1 or f1 <| ... fn <| x

Solution 4

According to F# Symbol and Operator Reference it is Forward Function Composition operator.

Solution 5

That is function composition, used for partial application

Share:
15,036

Related videos on Youtube

Russell
Author by

Russell

I am an Architect and Senior developer based in Adelaide. I love learning, mentoring and contributing to the development community. You can find me on Twitter @RussLescai.

Updated on August 07, 2020

Comments

  • Russell
    Russell over 3 years

    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?

    EDIT:

    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
    
    • cfern
      cfern over 14 years
      You can even be more concise now: ... let new_list2 = read_lines "myText.txt" |> List.map (trim >> to_upper) ... This is the form in which I write data processing myself.
    • Russell
      Russell over 14 years
      Awesome, so I could turn it into : let new_list2 = File.ReadAllLines(path) |> Array.map (trim >> to_upper) ... very neat :) thanks
    • cfern
      cfern over 14 years
      See, you're learning already :)
    • Russell
      Russell over 14 years
      hehe yep thanks :) I am finding it a very interesting experience learning the functional paradigm coming from a OO paradigm background.
    • codebliss
      codebliss over 14 years
      map f >> map g = map (f >> g). And the latter is faster.
    • Prakash
      Prakash about 14 years
  • JonathanPeel
    JonathanPeel about 3 years
    For us coming from OOP, looking at this makes heads hurt, but then I realse I can replace (fun x -> x |> Entry.nv |> SetFirstName |> dispatch) with (Entry.nv >> SetFirstName >> dispatch) and F# just continues to impress.