Index: frog/utils.dart |
diff --git a/frog/utils.dart b/frog/utils.dart |
index 657349a6cf0c7d20ca3ed3c89286791ad5b32128..cb3ca2627c0a682b78a71af7f8d99abf6f1270f7 100644 |
--- a/frog/utils.dart |
+++ b/frog/utils.dart |
@@ -7,8 +7,8 @@ |
// TODO(jmesserly): we might want a version of this that return an iterable, |
// however JS, Python and Ruby versions are all eager. |
-List map(Iterable source, mapper(source)) { |
- List result = new List(); |
+List map(Iterable source, Dynamic mapper(source)) { |
+ List result = []; |
if (source is List) { |
List list = source; // TODO: shouldn't need this |
result.length = list.length; |
@@ -36,8 +36,8 @@ reduce(Iterable source, callback, [initialValue]) { |
return current; |
} |
-List zip(Iterable left, Iterable right, mapper(left, right)) { |
- List result = new List(); |
+List zip(Iterable left, Iterable right, Dynamic mapper(left, right)) { |
+ List result = []; |
var x = left.iterator(); |
var y = right.iterator(); |
while (x.hasNext() && y.hasNext()) { |
@@ -49,6 +49,16 @@ List zip(Iterable left, Iterable right, mapper(left, right)) { |
return result; |
} |
+Map<Dynamic, List> groupBy(Iterable left, Dynamic keyFn(value)) { |
+ Map result = {}; |
+ var iter = left.iterator(); |
+ while (iter.hasNext()) { |
+ var i = iter.next(); |
+ result.putIfAbsent(keyFn(i), () => []).add(i); |
+ } |
+ return result; |
+} |
+ |
/** Sorts the map by the key. */ |
List orderValuesByKeys(Map map) { |
// TODO(jmesserly): it'd be nice to have SortedMap in corelib. |