Jun 29, 2011

f# essential - part 2 - pipleline operator

Before I discuss pipeline operator, let's talk about operator. F# allow us to define operator as function. The following are infix operators

$ % & * + - . / < = > @ ^ | 

The following are prefix operators.

! ? ~

You can define a infix operator like below

let (operator) ops1 ops2 = ...
//for example 
let (+) a b = a - b
2+3

You can define a prefix operator like below

let (!) a = 1
!8

So you can change your expression from "add 2 4" to "2 + 3" or "m1 a" to "!a", let's defined our pipleine operator as below.

let (-->) x f = f x
let rb = 0.5 --> System.Math.Cos

However, F# already define the "|>" for us, so we should use "|>". Operator is not a function, definition, rather you should think of it as macro. The compiler will change the syntax before it compile.