Skip to content

AxelDelsol/FunctionalCsharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

FunctionalCsharp

F# provides two operators to use and compose functions : operators >> and |>.

Operator >>

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

Operator |>

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

Roadmap

  • Compose method
  • Pipe method

C# concepts

  • Generics
  • Extension methods

About

C# implementation of >> and |> operators (from F#)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages