I’m learning a bit about function programming, and I’m wondering:
1) If my ForEach extension method is pure? The way I’m calling it seems violate the ‘don’t mess with the object getting passed in’, right?
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach ( var item in source ) action(item); } static void Main(string[] args) { List<Cat> cats = new List<Cat>() { new Cat{ Purring=true,Name='Marcus',Age=10}, new Cat{ Purring=false, Name='Fuzzbucket',Age=25 }, new Cat{ Purring=false, Name='Beanhead',Age=9 }, new Cat{Purring=true,Name='Doofus',Age=3} }; cats.Where(x=>x.Purring==true).ForEach(x => { Console.WriteLine('{0} is a purring cat... purr!', x.Name); }); // ************************************************* // Does this code make the extension method impure? // ************************************************* cats.Where(x => x.Purring == false).ForEach(x => { x.Purring = true; // purr,baby }); // all the cats now purr cats.Where(x=>x.Purring==true).ForEach(x => { Console.WriteLine('{0} is a purring cat... purr!', x.Name); }); } public class Cat { public bool Purring; public string Name; public int Age; }
2) If it is impure, is it bad code? I personally think it makes cleaner looking code than the old foreach ( var item in items) { blah; }, but I worry that since it might be impure, it could make a mess.
3) Would it be bad code if it returned IEnumerable<T> instead of void? I’d say as long as it is impure, yes it would be very bad code as it would encourage chaining something that would modify the chain. For example, is this bad code?
// possibly bad extension public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach ( var item in source ) action(item); return source; }
Impurity doesn’t necesarily mean bad code. Many people find it easy and useful to use side effects to solve a problem. The key is first knowing how to do it in a pure way, so you’ll know when impurity is appropriate :).
.NET doesn’t have the concept of purity in the type system, so a ‘pure’ method that takes in arbitrary delegates can always be impure, depending on how it’s called. For instance, ‘Where’, aka ‘filter’, would usually be considered a pure function, since it doesn’t modify its arguments or modify global state.
But, there’s nothing stopping you from putting such code inside the argument to Where. For example:
So that’s definately an impure usage of Where. Enumerables can do whatever they want as they iterate.
Is your code bad? No. Using a foreach loop is just as ‘impure’ — you’re still modifying the source objects. I write code like that all the time. Chain together some selects, filters, etc., then execute a ForEach on it to invoke some work. You’re right, it’s cleaner and easier.
Example: ObservableCollection. It has no AddRange method for some reason. So, if I want to add a bunch of things to it, what do I do?
or
I prefer the second one. At a minimum, I don’t see how it can be construed as being worse than the first way.
When is it bad code? When it does side effecting code in a place that’s not expected. This is the case for my first example using Where. And even then, there are times when the scope is very limited and the usage is clear.
Chaining ForEach
I’ve written code that does things like that. To avoid confusion, I would give it another name. The main confusion is ‘is this immediately evaluated or lazy?’. ForEach implies that it’ll go execute a loop right away. But something returning an IEnumerable implies that the items will be processed as needed. So I’d suggest giving it another name (‘Process’, ‘ModifySeq’, ‘OnEach’… something like that), and making it lazy: