ChrisInSeattle / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

IEnumerable.Do #68

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I've often found myself missing (and implementing) an IEnumerable<T>.Do method. 
Just like ForEach, only it yields the sequence. Great for when you want to 
insert a side effecting action in the middle of a linq statement. 
Would be nice if this was implemented once in a central location. :) 
Implementation is trivial, just throw a yield into a copy of ForEach:

public static IEnumerable<T> Do<T>(this IEnumerable<T> source, Action<T> action)
{
    source.ThrowIfNull("source");
    action.ThrowIfNull("action");
    foreach (T element in source)
    {
        action(element);
        yield return element;
    }
}

Original issue reported on code.google.com by roj...@gmail.com on 2 Aug 2011 at 7:03

GoogleCodeExporter commented 8 years ago
This is already implemented as Pipe:
https://code.google.com/p/morelinq/source/browse/MoreLinq/Pipe.cs?name=1.0

Original comment by azizatif on 22 Jun 2013 at 11:57