( >> )

val it : (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) = <fun:it@67-6>

This is a very generic function as you can see by the 'a parameters. A type name which starts with ' is a generic parameter, meaning it can take any type as an argument. These types can be more than just a single type. For instance, an 'a can be (x -> y) -> Option<z> or something even more complex. Try the id function with different kinds of arguments to see how it works.

('a -> 'b) can be read as "A function taking anything as an argument, returning anything else". What's important is that it might have a different type as input and output. 'b could also be the same as 'a if you really like. When mapping values in a list, you might return a value of the same type, and thus have 'b = 'a :

List.map ( ( + ) 1 ) [ 1 ; 2 ]

val it : int list = [2; 3]

In order to compose functions, the output of the first has to match the input of the second. Remember that -> is right associative, so the function signature can be read as (a -> b) -> (b -> c) -> (a -> c) . This can be read as "Given a function from a to b and a function from b to c, I'll construct a function from a to c". >> is the flipped version of \(\circ\). So f >> g is the same as \(g \circ f\). If you like the more mathy way of writing your composition, you can rather write g << f , but personally, I like reading code left to right.

let readUser () = System.Console.ReadLine () let createGreeting user = sprintf "Hello %s" user let user = readUser () let greeting = createGreeting user let greetUser = readUser >> createGreeting

The first is the imperative way of first calling one function, storing the result, then using the result in a second call and so on. The last line will create a function that does both. In our number guessing game, we use this composition several places.

Let's define >> ourselves.

let ( >> ) f g a = g ( f a )

val ( >> ) : f:('a -> 'b) -> g:('b -> 'c) -> a:'a -> 'c

Another useful function is |> :

( | > )

val it : ('a -> ('a -> 'b) -> 'b) = <fun:it@78-7>

This makes it simple to chain a result through a set of functions where each function has as input the previous result

let isEven x = ( x % 2 ) = 0 let a = 1 |> isEven |> not let b = 2 |> isEven |> not

val isEven : x:int -> bool val a : bool = true val b : bool = false

Defining it ourselves is easy

let ( | > ) x f = f x

val ( |> ) : x:'a -> f:('a -> 'b) -> 'b

Given the simplicity of the implementation, it's amazing how useful it is in practice. The above two constructs lets you easily replace your Linq needs in F# either by chaining data directly through your process using |> , or creating a more generic function using >> :

[ 10 .. 200 ] |> Seq.map ( ( + ) 1 ) |> Seq.filter ( fun x -> x % 2 = 1 ) |> Seq.skipWhile ( fun x -> x < 100 ) |> Seq.take 3

val it : seq<int> = seq [101; 103; 105]

Yet another is one that deconstructs a tuple while it goes

( "Hello" , "World!" ) ||> printfn "First: %s, Second: %s"

First: Hello, Second: World! val it : unit = ()