.NET Parallel Extension Like Functionality Added To The Dizzy - Functional List Library
Join the DZone community and get the full member experience.
Join For FreeI have added an "AsParallel()" method to Dizzy which mimics the way that the Parallel Extensions to the .net framework works. So, if you want to call a parallel map function you simply need to do this:
list.AsParallel().Map(n => n.ToUpper());
The AsParallel() method just looks like this:
public static IParallelEnumerable<T> AsParallel<T>(this IEnumerable<T> list)
{
return new ParallelEnumerable<T>(list);
}
As you can see, it just takes an IEnumerable<T> and replaces it with a IParallelEnumerable<T>. This Interface/Class combo doesn't provide any sort of work, it is just a flag to use the Parallel version of methods. Currently "Map" is the only method with a parallel version. The ParallelEnumerable class just looks like this:
public class ParallelEnumerable<T> : IParallelEnumerable<T>
{
private IEnumerable<T> enumerable;
public ParallelEnumerable(IEnumerable<T> list)
{
if (list == null) throw new ArgumentNullException("list");
this.enumerable = list;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.enumerable.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return this.enumerable.GetEnumerator();
}
}
Published at DZone with permission of Justin Etheredge. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments