Combinators.cs
public static class Combinators {
public static TOut Map<TIn, TOut>(this TIn @this, Func<TIn, TOut> f) => f(@this);
public static TOut Fork<TIn, TMiddle, TOut>(this TIn @this, Func<IEnumerable<TMiddle>, TOut> joinFunc,
params Func<TIn, TMiddle>[] partFuncs) => partFuncs.Select(pf => pf(@this)).Map(joinFunc);
}
Let's look at a simple demo how to use this
Program.cs
public static class Program {
string hello = "hhhhhheeeeeeeelllllllllllooooo";
int sumOfLettersToLookFor = hello.Fork(results => (int)results.Sum(),
x => (double)x.Count(l => l == 'h'),
x => (double) x.Count(l => l == 'e'),
x => (double) x.Count(l => l == 'l'),
x => (double) x.Count(l => l == 'o'));
sumOfLettersToLookFor.Dump();
}
Functional programming has many of these monads that are very short and allows you to do combinations that would be lengthy and stateful in the procedural / object oriented way but elegant and short in the functional world.
Finally a screenshot from Linqpad 7 showing the code above works :
(A reference to Jerry Seinfeld to the right for those who know Seinfeld episodes)
No comments:
Post a Comment