Revamping associative arrays

bearophile bearophileHUGS at lycos.com
Sun Oct 18 01:19:57 PDT 2009


Walter Bright:

> You can iterate over both keys and values with:
>      foreach (key, value; aa)

I know this, of course, but you are missing the point by a mile. An explicit foreach is not the only way you may want to iterate an AA on.

If I have a higher order function like map, I must be able to use it on any kind of iterable. Like an array, associative array, a set, a list, and so on. I must be able to change the data structure I give to the map, if the need arise while I change the code, and it has to keep working. If the mapping function takes a single argument the map has to choose to what iterate, among keys or values (or even both, as pairs). In such case iterating on keys is more useful. That's how all my dlibs higher order functions work when you give them an AA, they iterate on AA keys, array items, set items, list items, etc.

If you think still I am not right, you may ask for a poll here, to see how many like the default AA iteration to be on keys or values.

Bye,
bearophile



More information about the Digitalmars-d mailing list