This is non-language-specific, but I’ll use examples in C#. Often I face the problem in which I need to add a parameter to an object inside any given iteration of at least one of its parameters, and I have always to come up with a lame temporary list or array of some kind concomitant with the problem of keeping it properly correlated.
So, please bear with me on the examples below:
-
Is there an easier and better way to do this in C sharp?
List<String> storeStr; void AssignStringListWithNewUniqueStr (List<String> aList) { foreach (String str in aList) { storeStr.add(str); str = AProcedureToGenerateNewUniqueStr(); } } void PrintStringListWithNewUniqueStr (List<String> aList) { int i = 0; foreach (String str in aList) { print(str + storeStr[i]); i++; } }
Notice the correlation above is guaranteed only because I’m iterating through an unchanged aList. When asking about a “easier and better way” I mean it should also make sure the storeStr would always be correlated with its equivalent on aList while keeping it as short and simple as possible. The List could also have been any kind of array or object.
-
Is there any language in which something like this is possible? It must give same results than above.
IterationBound<String> storeStr; void AssignStringListWithNewUniqueStr (List<String> aList) { foreach (String str in aList) { storeStr = str; str = AProcedureToGenerateNewUniqueStr(); } } void PrintStringListWithNewUniqueStr (List<String> aList) { foreach (String str in aList) { print(str + storeStr); } }
In this case, the fictitious “IterationBound” kind would guarantee the correlation between the list and the new parameter (in a way, just like Garbage Collectors guarantee allocs). It would somehow notice it was created inside an iteration and associate itself with that specific index (no matter if the syntax there would be uglier, of course). Then, when its called back again in another iteration and it was already created or stored in that specific index, it would retrieve this specific value of that iteration.
Why not simply project your enumerable into a new form?
This way you keep each element associated with the new data.