Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In F# what does the >> operator mean?

Tags:

operators

f#

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 
like image 994
Russell Avatar asked Dec 14 '09 22:12

Russell


People also ask

What temp is C in F?

1 Celsius is equal to 33.8 Fahrenheit.

What is 1 C equal to in Fahrenheit?

Answer: 1° Celsius is equivalent to 33.8° Fahrenheit. Let us use the formulae of conversion between Celsius and Fahrenheit scales. Explanation: The formula to convert Celsius to Fahrenheit is given by °F = °C × (9/5) + 32.

How do you convert C to F fast?

If you find yourself needing to quickly convert Celsius to Fahrenheit, here is a simple trick you can use: multiply the temperature in degrees Celsius by 2, and then add 30 to get the (estimated) temperature in degrees Fahrenheit.

How do you calculate F to C?

Fahrenheit to Celsius Exact Formula In other words, if you'd like to convert a temperature reading in Fahrenheit to Celsius: Start with the temperature in Fahrenheit (e.g., 100 degrees). Subtract 32 from this figure (e.g., 100 - 32 = 68). Divide your answer by 1.8 (e.g., 68 / 1.8 = 37.78)


Video Answer


2 Answers

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’.

like image 89
p.campbell Avatar answered Oct 13 '22 02:10

p.campbell


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.

like image 23
kvb Avatar answered Oct 13 '22 00:10

kvb