Skip to content
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

Closed
DartBot opened this issue Jan 15, 2014 · 6 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-core type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jan 15, 2014

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.

@lrhn
Copy link
Member

lrhn commented Jan 16, 2014

We generally try to avoid creating extra "pair" objects when you iterate a map.
There is no requirement that the map is implemented using an object per entry internally, so it would be an extra overhead to create an object for each entry during iteration. Also, a Map is not just a "Set of Pairs", and I personally don't want to encourage using it as such.

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.
Added Type-Enhancement, Area-Library, Library-Core, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Mar 17, 2014

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.

@lrhn
Copy link
Member

lrhn commented Nov 6, 2014

Added Duplicate label.
Marked as being merged into #7088.

@DartBot
Copy link
Author

DartBot commented Nov 6, 2014

This comment was originally written by @mezoni


We generally try to avoid creating extra "pair" objects when you iterate a map.

What means your "when you iterate a map"?
How we can iterate a map if the map does not implement interface "Iterable"?

============
for (var keyWithValue in map) {
  someMethod(keyWithValue);
}
============

@lrhn
Copy link
Member

lrhn commented Nov 6, 2014

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.

@DartBot
Copy link
Author

DartBot commented Nov 6, 2014

This comment was originally written by @mezoni


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.

Thanks.
How I can obtain an Iterator from this "iteration"?
How I can terminate this "iteration"?

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"?
Why I found that this is an "action where to each pair of key and value applied specified function"?

/**
 * Applies [f] to each key-value pair of the map.
 *
 * Calling f must not add or remove keys from the map.
 */
void forEach(void f(K key, V value));

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core closed-duplicate Closed in favor of an existing report labels Nov 6, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants