www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - AA iteration of keys and values

I suggest to take a look at a D.learn thread:
http://forum.dlang.org/thread/morxorwjkjejowynjqkn forum.dlang.org

There is a common need to have a (lazy) range of keys and their 
corresponding values of an associative array. Sometimes iterating 
them in parallel with a foreach is not enough, and performing a 
lookup for each key generated by byKeys is not efficient.

In Python2 the two lazy iterables of an associartive array given 
by iterkeys() and itervalues() are guaranteed to have the same 
order (Python doesn't allow you to modify the associative array 
while iterating on it).

In D the current implementation gives them in the same order 
(again if you don't modify the associative array in the 
meantime), because this is the most natural way to implement the 
algorithm. But as far as I know there is no guarantee this is 
always true.

To solve this problem I have suggested to introduce another AA 
pseudomethod, named byPair, that returns a lazy range of 
key-value tuples, similar to the dict.iteritems() method of 
Python2:

http://d.puremagic.com/issues/show_bug.cgi?id=5466

But Andrei has not introduced this enhancement probably because 
it requires to import the std.typecons module from the object.d, 
tying it even more to Phobos.

I think the D docs should be updated, to state that the byKey and 
byValue yield corresponding keys-values:
http://d.puremagic.com/issues/show_bug.cgi?id=8473

Bye,
bearophile
Jul 30 2012