What should Map iteration do?
Related:
I cannot find the reason why sets and maps were harmoni'ed with
classes[1]. They do support mySet["foo"]
for mySet.has("foo")
and
for (something of mySet)
for iteration, do they not? Classes don't
have operator overloading, so using classes makes it harder to see the
usual syntax form.
Shouldn't we use proxies instead?
Thaddee Tyl wrote:
Related:
I cannot find the reason why sets and maps were harmoni'ed with classes[1].
If you mean specified with classes, I flinched too -- especially at private(this), syntax that is dead on arrival for classes (which are still not likely to make it into ES6 IMHO).
IIRC Mark used classes to avoid hand-expanding to more verbose functions, closures, freeze and seal calls, etc.
They do support
mySet["foo"]
formySet.has("foo")
andfor (something of mySet)
for iteration, do they not?
No to mySet["foo"], yes to iteration (the topic at hand).
Classes don't have operator overloading, so using classes makes it harder to see the usual syntax form.
Not sure about this. You see lack of mySet["foo"] clearly, you just don't expect that kind of support to be missing ;-).
Shouldn't we use proxies instead?
No, because we do not want the overhead of proxies for maps and sets, in implementations, or anything observable that might leak to indicate proxy-ness.
On Thu, Feb 2, 2012 at 1:35 PM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
Trying to revive this thread...
In C#, a Dictionary<K, V> is an ICollection<KeyValuePair<K, V>>. So,
items by default.
Note also that in Ruby it's not as simple as "defaulting to" key/value pairs. Actually if you write code like obj.each { |v| puts v } which is what you'd write for walking an Array, that doesn't work with a Hash. Array#each expects a block with one argument, but Hash#each expects a block with two arguments. (If you call Hash#each without any block argument, you do get an enumerator that yields pairs, but that is not the usual thing to do in Ruby. That is, typical generic collection algorithms in Ruby do not reach Hashes.)
Until Python 2.2, iterating over a dictionary was an error. Python 2.1.3 was the last version where you had to specify keys/values/items, and in that version of the Python standard library, the numbers are like this:
This fits with my experience. When each value is a serious object from which you can easily get or compute the key, values() is exactly what you want. Otherwise values() is unhelpful, usually.
I want to do the same experiment on a large body of Java code. Since Java does not have a default, the results should show what's most useful (in Java) in practice. I can use Apache commons code, for example. I'll try to find time this week.
I think the "generic algorithms" perspective should argue for items() iteration being the default. At issue is what a function like map or filter should do with a Map; it seems most general to expose all the information in the Map by default. However: passing a Map to a generic algorithm is only one use case for Map iteration, and not the most common in my experience.