Skip to content

A simple library with extension methods to allow you to do pattern matching with fluent syntax instead of doing a switch or a dictionary.

Notifications You must be signed in to change notification settings

tashrobinson/PatternMatching

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

PatternMatching

A simple .NET library with extension methods to allow you to do pattern matching with fluent syntax instead of doing a switch or a dictionary.

Usage

Simply make a reference to the assembly in your project, then add the line

using PatternMatching;

A few examples are:

int a = 5;
a.With(x => x == 5).Do(_ => Console.WriteLine("I am five!"));

That example should print "I am five!" to the standard output.

You can even chain With/Do operations together!

int a = 5;
a.With(x => x == 5).Do(_ => Console.WriteLine("I am five!"))
 .With(x => x != 5).Do(_ => Console.WriteLine("I am not five!"))
 .With(x => x >= 5).Do(x => Console.WriteLine("{0} is greater than or equal to five!", x))
 .With(x => x <  5).Do(x => Console.WriteLine("{0} is less than five!", x));

That example should print only the lines "I am five!" and "5 is greater than or equal to five!" to the standard output.

Finally, there's the Otherwise operation, which is the default fallthrough in case nothing else has been satisfied.

We'll demonstrate this with the classical FizzBuzz problem.

foreach (var i in Enumerable.Range(1, 100))
    i.With(x => x % 3 == 0).Do(_ => Console.WriteLine("Fizz"))
     .With(x => x % 5 == 0).Do(_ => Console.WriteLine("Buzz"))
     .Otherwise(x => Console.WriteLine));

License

PatternMatching is licensed under the BSD License.

About

A simple library with extension methods to allow you to do pattern matching with fluent syntax instead of doing a switch or a dictionary.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published