title: Is this C# extension method impure and if so, bad code?
body: <p>I'm learning a bit about function programming, and I'm wondering:</p>
  
  <p>1) If my <code>ForEach</code> extension method is pure?  The way I'm calling it seems violate the "don't mess with the object getting passed in", right?</p>
  
  <pre><code>public static void ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; source, Action&lt;T&gt; action)
  {
    foreach ( var item in source )
       action(item);
  }
  
  
  static void Main(string[] args)
  {
      List&lt;Cat&gt; cats = new List&lt;Cat&gt;()
      {
          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=&gt;x.Purring==true).ForEach(x =&gt;
      {
          Console.WriteLine("{0} is a purring cat... purr!", x.Name);
      });
  
      // *************************************************
      //  Does this code make the extension method impure?
      // *************************************************
      cats.Where(x =&gt; x.Purring == false).ForEach(x =&gt;
      {
          x.Purring = true; // purr,baby
      });
  
      // all the cats now purr
      cats.Where(x=&gt;x.Purring==true).ForEach(x =&gt;
      {
          Console.WriteLine("{0} is a purring cat... purr!", x.Name);
      });
  }
  
  public class Cat {
          public bool Purring;
          public string Name;
          public int Age;
  }
  </code></pre>
  
  <p>2) If it is impure, is it bad code?  I personally think it makes cleaner looking code than the old <code>foreach ( var item in items) { blah; }</code>, but I worry that since it might be impure, it could make a mess.</p>
  
  <p>3) Would it be bad code if it returned <code>IEnumerable&lt;T&gt;</code> instead of <code>void</code>?  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?</p>
  
  <pre><code>// possibly bad extension
  public static IEnumerable&lt;T&gt; ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; source, Action&lt;T&gt; action)
  {
      foreach ( var item in source )
          action(item);
  
      return source;
  
  }
  </code></pre>