Talk:Associative array/Iteration

From Rosetta Code
Revision as of 10:34, 3 August 2009 by rosettacode>JimD

Hmm, associative array is not defined ordered. It is an unordered map. For example, it can have keys of complex numbers or images etc, which are unordered. Whether and if an associative array has some internal indices, different from the unordered keys, of full or partial order making indexation better than O(n) is an implementation detail. I tempted to suggest renaming to iteration over an ordered map, but looking at the implementations, I realized that it is rather an ordered view of some unordered map using an alternative ordered key. Any container can have such alternative views. It is not special to associative array, or better to say has nothing to do with them. --Dmitry-kazakov 08:23, 3 August 2009 (UTC)

Hi Dmitry, iterating over all the members of an associative array is quite a common practice in Python. The keys/values may not appear in any particular order, but you must be guaranteed to go over each and every key/value only once. Python has the added assurance that if the associative array is unchanged, iterating over all keys , then iterating over all values will give the values in corresponding order to their keys; but that is not called for in this task. --Paddy3118 09:32, 3 August 2009 (UTC)
Hi! Yes this is IMO the problem with this task. An associative array is not required to provide a conversion to an ordered set of pairs (key, value), which technically is what you described. Yes, it can, but so can any container on any finite machine. You can always order any finite set. The question whether any unordered map shall provide such conversion (in place (a view) or physical (a copy)) is another issue. There can be arguments for and against it. But it is not a programming task, it is a question of a container library design. Phyton's library took one choice, other libraries could do another. --Dmitry-kazakov 10:05, 3 August 2009 (UTC)
Dmitry, it's sufficient that many programmers using many different mappings will want to iterate over all items or keys in the the mapping (with that guarantees that every item will be visited once and only once). Regardless of whether that iteration conforms to any particular ordering it's clearly a suitable basis for comparison among different languages. If my work requires me to deal with some Java code using their HashMap collections then I'm very likely to need info on the closest equivalent to Python .keys() or .items() dictionary methods. Some mapping libraries, classes or types in some languages don't support this then it should absolutely be documented here (so that the programmer adopting such a language and learning about it here will KNOW that he or she must maintain a separate list of the valid keys).