F# provides two operators to use and compose functions : operators >> and |>.
This operator takes two functions and creates a new one.
Example:
let addOne x = x + 1
let multiplyByTwo x = x * 2
let combin = addOne >> multiplyByTwo
let result = combin 3 // result == 8
Here combin
is a function that takes one integer x and outputs 2*(x+1). That is, it first apply the function addOne and then call multiplyByTwo on the output of addOne.
Goal : implement the equivalent in C#
Func<int,int> addOne = (int x) => x + 1;
Func<int,int> multiplyByTwo = (int x) => 2*x;
Func<int,int> combin = addOne.Compose(multiplyByTwo);
var result = combin(3); // result == 8
This operator allows you to pass the output of one function as the input of another one.
Example:
let addOne x = x + 1
let multiplyByTwo x = x * 2
let result = 3
|> addOne
|> multiplyByTwo // result == 8
Func<int,int> addOne = (int x) => x + 1;
Func<int,int> multiplyByTwo = (int x) => 2*x;
int result = 3
.Pipe(addOne)
.Pipe(multiplyByTwo) // result == 8
- Compose method
- Pipe method
- Generics
- Extension methods