OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Collection<T> supports most of the ES 5 Array methods, but it's missing | 5 // Collection<T> supports most of the ES 5 Array methods, but it's missing |
6 // map and reduce. | 6 // map and reduce. |
7 | 7 |
8 // TODO(jmesserly): we might want a version of this that return an iterable, | 8 // TODO(jmesserly): we might want a version of this that return an iterable, |
9 // however JS, Python and Ruby versions are all eager. | 9 // however JS, Python and Ruby versions are all eager. |
10 List map(Iterable source, mapper(source)) { | 10 List map(Iterable source, Dynamic mapper(source)) { |
11 List result = new List(); | 11 List result = []; |
12 if (source is List) { | 12 if (source is List) { |
13 List list = source; // TODO: shouldn't need this | 13 List list = source; // TODO: shouldn't need this |
14 result.length = list.length; | 14 result.length = list.length; |
15 for (int i = 0; i < list.length; i++) { | 15 for (int i = 0; i < list.length; i++) { |
16 result[i] = mapper(list[i]); | 16 result[i] = mapper(list[i]); |
17 } | 17 } |
18 } else { | 18 } else { |
19 for (final item in source) { | 19 for (final item in source) { |
20 result.add(mapper(item)); | 20 result.add(mapper(item)); |
21 } | 21 } |
22 } | 22 } |
23 return result; | 23 return result; |
24 } | 24 } |
25 | 25 |
26 reduce(Iterable source, callback, [initialValue]) { | 26 reduce(Iterable source, callback, [initialValue]) { |
27 final i = source.iterator(); | 27 final i = source.iterator(); |
28 | 28 |
29 var current = initialValue; | 29 var current = initialValue; |
30 if (current == null && i.hasNext()) { | 30 if (current == null && i.hasNext()) { |
31 current = i.next(); | 31 current = i.next(); |
32 } | 32 } |
33 while (i.hasNext()) { | 33 while (i.hasNext()) { |
34 current = callback(current, i.next()); | 34 current = callback(current, i.next()); |
35 } | 35 } |
36 return current; | 36 return current; |
37 } | 37 } |
38 | 38 |
39 List zip(Iterable left, Iterable right, mapper(left, right)) { | 39 List zip(Iterable left, Iterable right, Dynamic mapper(left, right)) { |
40 List result = new List(); | 40 List result = []; |
41 var x = left.iterator(); | 41 var x = left.iterator(); |
42 var y = right.iterator(); | 42 var y = right.iterator(); |
43 while (x.hasNext() && y.hasNext()) { | 43 while (x.hasNext() && y.hasNext()) { |
44 result.add(mapper(x.next(), y.next())); | 44 result.add(mapper(x.next(), y.next())); |
45 } | 45 } |
46 if (x.hasNext() || y.hasNext()) { | 46 if (x.hasNext() || y.hasNext()) { |
47 throw new IllegalArgumentException(); | 47 throw new IllegalArgumentException(); |
48 } | 48 } |
49 return result; | 49 return result; |
50 } | 50 } |
51 | 51 |
| 52 Map<Dynamic, List> groupBy(Iterable left, Dynamic keyFn(value)) { |
| 53 Map result = {}; |
| 54 var iter = left.iterator(); |
| 55 while (iter.hasNext()) { |
| 56 var i = iter.next(); |
| 57 result.putIfAbsent(keyFn(i), () => []).add(i); |
| 58 } |
| 59 return result; |
| 60 } |
| 61 |
52 /** Sorts the map by the key. */ | 62 /** Sorts the map by the key. */ |
53 List orderValuesByKeys(Map map) { | 63 List orderValuesByKeys(Map map) { |
54 // TODO(jmesserly): it'd be nice to have SortedMap in corelib. | 64 // TODO(jmesserly): it'd be nice to have SortedMap in corelib. |
55 List keys = map.getKeys(); | 65 List keys = map.getKeys(); |
56 keys.sort((x, y) => x.compareTo(y)); | 66 keys.sort((x, y) => x.compareTo(y)); |
57 final values = []; | 67 final values = []; |
58 for (var k in keys) { | 68 for (var k in keys) { |
59 values.add(map[k]); | 69 values.add(map[k]); |
60 } | 70 } |
61 return values; | 71 return values; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 final int length; | 136 final int length; |
127 int _index = 0; | 137 int _index = 0; |
128 FixedIterator(this.value, this.length); | 138 FixedIterator(this.value, this.length); |
129 | 139 |
130 bool hasNext() => _index < length; | 140 bool hasNext() => _index < length; |
131 E next() { | 141 E next() { |
132 _index++; | 142 _index++; |
133 return value; | 143 return value; |
134 } | 144 } |
135 } | 145 } |
OLD | NEW |