Chromium Code Reviews| Index: pkg/barback/lib/src/utils.dart |
| diff --git a/pkg/barback/lib/src/utils.dart b/pkg/barback/lib/src/utils.dart |
| index f1bcddde7f45085a1e977c5c1d5b03ea1653f07f..e4fe08269f8354c08ad07b5d101a9cb4bc5a526a 100644 |
| --- a/pkg/barback/lib/src/utils.dart |
| +++ b/pkg/barback/lib/src/utils.dart |
| @@ -140,10 +140,34 @@ List flatten(Iterable nested) { |
| Set unionAll(Iterable<Set> sets) => |
| sets.fold(new Set(), (union, set) => union.union(set)); |
| -/// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose |
| -/// values are the return values of [fn]. |
| -Map mapMapValues(Map map, fn(key, value)) => |
| - new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); |
| +/// Creates a map from [iter], using the return values of [keyFn] as the keys |
| +/// and the return values of [valueFn] as the values. |
| +Map listToMap(Iterable iter, keyFn(element), valueFn(element)) { |
|
Bob Nystrom
2014/01/30 19:33:44
Is this any different from new Map.fromIterable()?
nweiz
2014/01/31 03:43:27
It's not. Removed.
|
| + var map = new Map(); |
| + for (var element in iter) { |
| + map[keyFn(element)] = valueFn(element); |
| + } |
| + return map; |
| +} |
| + |
| +/// Creates a new map from [map] with new keys and values. |
| +/// |
| +/// The return values of [keyFn] are used as the keys and the return values of |
| +/// [valueFn] are used as the values for the new map. |
| +Map mapMap(Map map, keyFn(key, value), valueFn(key, value)) => |
| + listToMap(map.keys, |
| + (key) => keyFn(key, map[key]), |
| + (key) => valueFn(key, map[key])); |
| + |
| +/// Creates a new map from [map] with the same keys. |
| +/// |
| +/// The return values of [fn] are used as the values for the new map. |
| +Map mapMapValues(Map map, fn(key, value)) => mapMap(map, (key, _) => key, fn); |
| + |
| +/// Creates a new map from [map] with the same keys. |
| +/// |
| +/// The return values of [fn] are used as the keys for the new map. |
| +Map mapMapKeys(Map map, fn(key, value)) => mapMap(map, fn, (_, value) => value); |
|
Bob Nystrom
2014/01/30 19:33:44
Not in this patch, but we should look at moving th
nweiz
2014/01/31 03:43:27
Absolutely.
|
| /// Returns whether [set1] has exactly the same elements as [set2]. |
| bool setEquals(Set set1, Set set2) => |