Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 3
    Unless I'm mistaken, because a List is mutable, in the first function you claimed 'pure', another thread could remove all of the elements from the list or add a bunch more while it's in the foreach loop. Not certain how that would play with the IEnumerator being while(iter.MoveNext())ed, but unless the IEnumerator is immutable (doubtful) then that would threaten to thrash the foreach loop. Commented Oct 25, 2012 at 4:45
  • True, you have to assume that the collection is never written to while threads are reading from it. That would be a valid assumption, if each thread calling the method builds its own list. Commented Oct 25, 2012 at 14:43
  • I don't think you can call it 'pure' when it has that mutable object in it that it is using by reference. If it received an IEnumerable you might be able to make that claim because you can't add or remove elements from an IEnumerable, but then it could be an Array or a List handed in as IEnumerable so the IEnumerable contract doesn't guarantee any form of purity. The real technique to make that function pure would be immutability with pass-by-copy, C# doesn't do this so you would have to copy the List right when the function receives it; but the only way to do that is with a foreach on it... Commented Oct 25, 2012 at 14:54
  • 1
    @JimmyHoffa: Dammit, you got me obsessed over this chicken and egg problem! If you see a solution anywhere, please let me know. Commented Oct 25, 2012 at 22:04
  • 1
    Just came across this answer now and it's one of the best explanations on the topic I've come across, the examples are super concise and really make it easy to grok. Thanks! Commented Dec 9, 2015 at 21:04