I 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;
public IEnumerator<T> GetEnumerator()
So, you can see that we are just wrapping an IEnumerable and returning the items we need to support the IEnumerable interface. Then we just define a “Map” method that takes an IParallelEnumerable instead of an IEnumerable and voila! Instant parallelism without the need for different method names. The map method is just the same as it was in this post, only the method signature is now change to look like this:
public static IEnumerable<TResult> Map<TArg, TResult>(
this IParallelEnumerable<TArg> list,
Func<TArg, TResult> func)
So, whenever we have an IParallelEnumerable we will call this method instead of our other Map method. We still return an IEnumerable because we don’t want additional methods chained to this call to automatically try to use parallel versions.
So, I hope you found this useful, and I hope you also check out Dizzy. I’ve added a few other methods, and plan to keep adding methods regularly. If you have any ideas for new methods, please let me know.
Loved the article? Hated it? Didn’t even read it?
We’d love to hear from you.