• Coding
  • ForEach Extension Method on Generic Types

arithma wrote@xterm: Jon Skeet to the rescue. In your face :P
Jon Skeet, C# in Depth 2nd Edition (Page 257, Line 7) wroteIn this chapter we’ll first look at how to use extension methods and how to write them. We’ll then examine a few of the extension methods provided by .NET 3.5, and see how they can be chained together easily. This chaining ability is an important part of the reason for introducing extension methods to the language in the first place, and is an important part of LINQ.
Yep, you win. I retract this from my statement:
What arithma said is incorrect concerning extension methods being primarily for LINQ
arithma wroteIEnumerable can signify an infinite sequence.
Doesn't that emphasize my wondering what the point is of having a ForEach() extension method on an IEnumerable<T>? Would the termination condition be implicit (hard coded)?
@MSD: InfiniteSequence.ForEach(Action).Take(10).ToList()
Of course this is not in the spirit of IEnumerable, which would rather use immutable thinking rather than depending on side effects.
arithma wrote@MSD: InfiniteSequence.ForEach(Action).Take(10).ToList()
Of course this is not in the spirit of IEnumerable, which would rather use immutable thinking rather than depending on side effects.
Well, that is better written:
AnIEnumerable.Take(10).ToList().Foreach(Action);
isn't it?
Service Library:
IEResult => return InfiniteSequence.ForEach(Action);

Client Side:
IEResult.Take(10);
ForEach is not meant to project and return.
I would do this:
return anIEnumerable.Select(x => Modify(x));
on the service side
and then .Take() on the client side.
Using ForEach on the service side is changing an object before fetching it, which combines 2 different functionalities in one method.
That didn't make sense since anyway:
ForEach(Modify) === Select(x => Modify(x))

ForEach's purpose of being is to change the object.
@xterm: yes, you're right. But we're more focused on the fact the IE can be an infinite sequence, ForEach, Select, anything could replace it. ForEach has the dirtiest functional feel, ever.
Semantically, I don't think that Select and ForEach have the same meaning, ForEach is supposed to apply a certain modification while Select is used for projection without modification of the original source even if in this case the source would not be actually modified because of lazy evaluation. I do agree that they are both functionally equivalent in this case.
Ok obviously I need to research some more about the difference between IEnumerable and IList because honestly I do not know what difference it makes to use either one of those. I decided to use IE because that's what was being returned from my repository (which basically uses EF to query the DB).

You guys confused the hell out of me now lol. Would the following statement cause any issues? (because IEnumerable holds infinite number of items)
IEnumerable<Category> result = _repository.GetCategories();
results.ForEach(a=> a.Left += 2, a=> a.Right +=2);
Again, all what I'm trying to do is replace the following with the above code:
IEnumerable<Category> result = _repository.GetCategories();
foreach(Category c in result) {
   c.Left += 2;
   c.Right += 2;
}
Why? Simply because I like to chain my methods. Period. :)
xterm wroteToo much heat!
public static void ForEach<T>(this IEnumerable<T> lst, Action<T> action ){
      foreach (var item in lst)
              action(item);
}
The way to use such an extension can be as simple as:
var list = Enumerable.Range(1, 100).Select(num => new Node { Left = num, Right = num });
list.ForEach(a => { a.Left += 2; a.Right += 2; });
At a first glance, this is totally correct, it compiles properly. But you will not get the desired effect if you're acting on an IEnumerable, which is the result of the first statement. The extension method on the IEnumerable will not persist the changes inside the list. you could easily test this by debugging the content of list.
explanation for aforementioned magic:

this happens because the creation/instantiation of the items(Node) of the list are delayed(lazily evaluated) until eager evaluation is needed, so when you loop over this IE in ForEach method you get a list of objects and when you call ToList over this same IE you'll get a new list with different objects.

Anyway that's great example to show the utility and the intricacy of IE(not internet explorer, IEnumerable :P)

thanks man for this great example(this tops my other examples to explain IE :D)