Revamping associative arrays

Bill Baxter wbaxter at gmail.com
Sun Oct 18 11:50:14 PDT 2009


On Sun, Oct 18, 2009 at 10:56 AM, bearophile <bearophileHUGS at lycos.com> wrote:

> The opEquals among AAs requires probably less than 20 lines of code.
> Two persons (plus Walter, of course) have said they don't like to iterate on the keys first. The other people have kept muzzle shut so I can't tell yet.

I typed a long post that weighed lots of pros and cons of different
options, but then I hit upon a simple rule that I think makes a lot of
sense:

I think the default should be to iterate over whatever 'in' looks at.

And conversely, I think 'in' should compare against whatever default
iteration iterates over.

Proposed:
arrays -- default iteration over values,   "x in A" answers if x is
one of the values.
assoc arrays -- default iteration over keys,  "x in AA" answers if x
is one of the keys
sets -- iteration over keys (or call 'em values, could be either), "x
in S" answers if x is one of them

I think this is what Python uses, actually.  But I just think it makes
a lot of sense to say that 'x in Y' should be some kind of shorthand
for
foreach(thing; Y) {
     if (x == thing) {
           return something useful
     }
}

I guess that's even clearer in Python where you iterate by writing
"for thing in Y:"

This looks to me like a general rule that trumps a rule which merely
stems from the happenstantial syntactic similarity between arrays and
associative arrays.

--bb



More information about the Digitalmars-d mailing list