Revamping associative arrays

bearophile bearophileHUGS at lycos.com
Sun Oct 18 03:18:34 PDT 2009


Piotrek:

>No! No! No! Maybe you are wrong.<

Life is complex so I am usually wrong, because it's hard to consider all sides of a thing, but sometimes other people are even more wrong :-)


>I remember that I spent many hours on finding bug in python's script written by me in my job. The reason was the python's behaviour decribed by you.<

I have have followed many Python programmers for a lot of time and I think such problem of yours is not common. But I'll keep an eye open for possible other people with your problem.


>Then I couldn't understand why the hell iterating on collection returns a key in the first place. It's so not intuitive.<

What's intuitive on iterating on values? Well, I think Walter agrees with you, I remember his explanation (iterating on a normal array doesn't yield its indexes), but beside what's intuitive you have also to keep in mind what's handy, and iterating on keys is more useful.

Beside the things I have said, if you think of associative arrays as sets where there is a value associated to each set item (and this is how Python dicts are implemented and how the light iterable objects they give you when you ask for keys in Python3), when you iterate on the set you get the set items, so if you extend the set you keep iterating on the set items...


> Your explanation is not even close in convincing me. If I wanted keys I would write:
>  foreach (key, value; set) or (key; set.keys)

In D set.keys needs a good amount of memory and time, it's useful only in special situations, for example when you are sure your AA is small. Andrei will probably push to add/replace something to find keys and a lazy way.


>Now I know why I don't like Python and I hope I will never have to need it again. For scriptng (but not at work since I don't do scripting enymore) I prefer D (rdmd).<

Refusing forever to use a popular (and usually quite appreciated) language just because you don't like a single small feature is stupid. I don't want to force you to like Python, but the choice of a language must be based on a bit more global evaluation of it. Every language under the sun has plenty warts. C++ has enough warts (far larger than the one you have listed) that you can write a big book on them, but people keep using it still.

"Not doing scripting any more" too is probably a not smart thing to say, because in most programming jobs I've seen, there are small files to munge, commands to automate, things to show or plot, and so on, etc. A scripting language (even just shell scripting) is designed for such things.


>bearophile, I like your great commitment in D development but I don't like pushing D toward pythonish world. (Of course some ideas from Python project could be succesfully used in D)<

Thank you :-) I've known several languages (I think this is normal in this newsgroup), and I try to suggest what my experience shows me :-) I'm often wrong anyway.

Bye,
bearophile



More information about the Digitalmars-d mailing list