OK, so it has botherd me for a long time, that MS decided to call the Map, Filter, Fold and Bind something completely nonsense, like Select, Where etc.
It seems I’m not the only one: http://stackoverflow.com/questions/1230729/where-is-the-fold-linq-extension-method
So I decided to make extension methods, so you can now use the real functional names, here goes:
public static class LinqExtensions
{
public static IEnumerable<TResult> Map<TSource, TResult>
(this IEnumerable<TSource> source,
Func<TSource, int, TResult> selector)
{
return source.Select(selector);
}
public static IEnumerable<TResult> Map<TSource, TResult>
(this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
return source.Select(selector);
}
public static IEnumerable<TSource> Filter<TSource>
(this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
{
return source.Where(predicate);
}
public static IEnumerable<TSource> Filter<TSource>
(this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
return source.Where(predicate);
}
public static TAccumulate FoldLeft<TSource, TAccumulate>
(this IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
{
return source.Aggregate(seed, func);
}
public static TSource FoldLeft<TSource>
(this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func)
{
return source.Aggregate(func);
}
public static TAccumulate FoldRight<TSource, TAccumulate>
(this IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
{
return source.Reverse().Aggregate(seed, func);
}
public static TSource FoldRight<TSource>
(this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> func)
{
return source.Reverse().Aggregate(func);
}
public static IEnumerable<TResult> Bind<TSource, TCollection, TResult>
(this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>>
collectionSelector,
Func<TSource, TCollection, TResult>
resultSelector)
{
return source.SelectMany(collectionSelector, resultSelector);
}
public static IEnumerable<TResult> Bind<TSource, TCollection, TResult>
(this IEnumerable<TSource> source,
Func<TSource, int,
IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector)
{
return source.SelectMany(collectionSelector, resultSelector);
}
public static IEnumerable<TResult> Bind<TSource, TResult>
(this IEnumerable<TSource> source,
Func<TSource, int, IEnumerable<TResult>> selector)
{
return source.SelectMany(selector);
}
public static IEnumerable<TResult> Bind<TSource, TResult>
(this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TResult>> selector)
{
return source.SelectMany(selector);
}
}
What I did was simply taking the method signatures from the object browser and renaming the methods. So some of the arguments might not be named “the functional way”.
In order for the code to fit on the page, I had to reformat it a bit.
This would allow us to do the following:
var enumerable = strs.
Filter(x => x.StartsWith("Ni")).
Map(x => x);
Wow I feel so much better now :-)
Under the hood these use the linq methods…
Probably somebody has done this before and better, but it was just an experiment…