New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to Map interface to access entries as an Iterable and optionally to directly filter map entries #16117
Comments
We generally try to avoid creating extra "pair" objects when you iterate a map. If you want to do something for each entry, you can use the forEach method with a binary function argument. That gives you a call per entry. If you want to return something per entry, I'd use the keys iterable, and possibly lookup the value for the key if I need it. For filtering, we could consider adding a Map.where (Map.removeWhere/Map.retainWhere for destructively updating). Removed Type-Defect label. |
This comment was originally written by @seaneagan The "entries as an Iterable" part is a duplicate of issue #7088 This is at least the third bug that's been filed for this. |
This comment was originally written by @mezoni
What means your "when you iterate a map"? ============ |
The "when iterating a map" refers to using Map.forEach. It expects a function with two parameters instead of a function taking a single Pair object. |
This comment was originally written by @mezoni
Thanks. As you know (as a programmer) "iteration" does not means traverse from the begin to the end without the possiblity to terminate this process. The "iteration" only means a possibility to traverse. Also, why in the documentation I do not found that this is an "iteration"? /** |
This issue was originally filed by @Andersmholmgren
i.e. something like
Iterable<KeyValuePair<K, V>> get entries;
It's often useful to be able to iterate of the entry set of a map and process entry by entry. This is common in other languages like Java.
e.g you could do stuff like
final Map<Symbol, MethodMirror> instanceMembers = reflect(o).type.instanceMembers;
final Iterator<KeyValuePair<Symbol, MethodMirror>> getters = instanceMembers.entries.where((kv) =>
kv.value.isGetter);
final Iterable<KeyValuePair<Symbol, Object>> getterValues = getters.map((kv) =>
new KeyValuePair(kv.key, invokeGetter(kv.value)));
Sadly in this example I ended up into https://code.google.com/p/dart/issues/detail?id=10975 so it proved futile but this sort of thing is very useful
Also (but less important) it can be useful to filter a map directly.
i.e.
Map<K,V> where(bool test(K key, V value));
This allows you then to create a subset of the first map and work on that.
The text was updated successfully, but these errors were encountered: