Skip to content

Map constructors taking Iterables #11330

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

Closed
floitschG opened this issue Jun 18, 2013 · 6 comments
Closed

Map constructors taking Iterables #11330

floitschG opened this issue Jun 18, 2013 · 6 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug

Comments

@floitschG
Copy link
Contributor

Add:

Map<K, V>.fromIterable(Iterable iterable, { K key(element), V value(element) });
key and value default to the identity function.  (Still a small discussion going for the name of the optional arguments).

and
Map<K, V>.fromIterables(Iterable<K> keys, Iterable<V> values);

Examples:
new Map<int, String>.fromIterable([1, 2], value: (x) => "$x");
new Map<int, String>.fromIterables([1, 2], ["eins", "zwei"]);

@DartBot
Copy link

DartBot commented Jun 18, 2013

This comment was originally written by @seaneagan


So it looks like there will be no Pair/tuple concept in dart (see issue #7088)?

Corresponding examples for that approach would be:

new Map<int, String>.fromPairs([1, 2].map((i) => new Pair(x, "$x")));
new Map<int, String>.fromPairs([1, 2].zip(["eins", "zwei"])));

where Iterable has:

Iterable<Pair<K, dynamic>> zip(Iterable other);

If so, do you plan on instead adding Iterable-like functional methods directly to Map:

Iterable map(f(K k, V v));
Map<K, V> where(test(K k, V v));
fold(initialValue, transform(K k, V v));
// etc.

@lrhn
Copy link
Member

lrhn commented Jun 19, 2013

The operations that work on key and value can usually be written in terms of just the key (and access to the map). For example
   Iterable map(f(K key))
already exists on Map.keys.

For ease of use, you can write a function from map and binary function to unary function:
  Function withMapValue(Map map, func(key, value)) => (key) => func(key, map[key])

Then you can do
  map.keys.map(withMapValue(map, (k, v) => ...))
instead of
  map.map((k, v) => ...)
Not as short, but not bad either.

@DartBot
Copy link

DartBot commented Jun 20, 2013

This comment was originally written by odw...@google.com


Having Map<K, V>.fromIterable(Iterable iterable, { K key(element), V value(element) });
is great, and I really like the idea of passing two functions on elements for the keys and the values.
What about having, in addition, a method on Iterable to build a map out of it (in the spirit of toList and toSet)?

Iterable<K>.toMap({ K key(element), V value(element) }

That way we keep the fluentness of Iterable:

Map<K,V> map = Iterable.generate(....)
    .map(....)
    .where(....)
    .toMap(key: toKey)

instead of:
Iterable<K> keys = Iterable.generate(....)
    .map(....)
    .where(....)
Map<K,V> map = new Map.fromIterable(keys, key: toKey)

@floitschG
Copy link
Contributor Author

We thought of adding a toMap on Iterables, but don't think it is needed that often. Now, with the Iterable constructors it becomes even less useful.

@szakarias
Copy link
Contributor

Set owner to @szakarias.
Added Started label.

@szakarias
Copy link
Contributor

Fixed in r=24531.


Added Fixed label.

@floitschG floitschG added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Jun 28, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement 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. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants