4 ms·
The point is to not modify the object that you are looping over from within the loop. It is very easy to create bugs this way.
by lowdest 6y ago
The point is to not modify the object that you are looping over from within the loop. It is very easy to create bugs this way.
- Supermancho 6y ago> not modify the object that you are looping over from within the loop That isn't happening. The loop is over an integral range (1..5 in both examples). That integer is being hashed in the example and the hash is being used for a SIDE EFFECT. Internally, the select() is doing the exact same thing, but there is no label on the value and it's being returned out of the ostensible container function. This is not a good example of why this pattern is bad nor of why side effects are bad.
- backslash_16 6y agoIf I have read his example correctly - he's not modifying the object he is looping over. car_ids is externally defined and he is using a loop to exhaustively test other objects and take an action on them, which is adding them to the externally defined array. I do agree with what you are saying, don't modify the enumerable you loop over. IIRC C# is pretty safe in that it doesn't allow modifications to the enumerable inside of a for loop or foreach loop. I think it's interesting people say functional constructs like map don't involve looping. They do, it's just hidden from the caller of those functions.