OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * The [Collections] class implements static methods useful when | 6 * The [Collections] class implements static methods useful when |
7 * writing a class that implements [Collection] and the [iterator] | 7 * writing a class that implements [Collection] and the [iterator] |
8 * method. | 8 * method. |
9 */ | 9 */ |
10 class Collections { | 10 class Collections { |
11 static void forEach(Iterable<Object> iterable, void f(Object o)) { | 11 static void forEach(Iterable iterable, void f(o)) { |
12 for (final e in iterable) { | 12 for (final e in iterable) { |
13 f(e); | 13 f(e); |
14 } | 14 } |
15 } | 15 } |
16 | 16 |
17 static bool some(Iterable<Object> iterable, bool f(Object o)) { | 17 static bool some(Iterable iterable, bool f(o)) { |
18 for (final e in iterable) { | 18 for (final e in iterable) { |
19 if (f(e)) return true; | 19 if (f(e)) return true; |
20 } | 20 } |
21 return false; | 21 return false; |
22 } | 22 } |
23 | 23 |
24 static bool every(Iterable<Object> iterable, bool f(Object o)) { | 24 static bool every(Iterable iterable, bool f(o)) { |
25 for (final e in iterable) { | 25 for (final e in iterable) { |
26 if (!f(e)) return false; | 26 if (!f(e)) return false; |
27 } | 27 } |
28 return true; | 28 return true; |
29 } | 29 } |
30 | 30 |
31 static List<Object> map(Iterable<Object> source, | 31 static List map(Iterable source, List destination, f(o)) { |
32 List<Object> destination, | |
33 f(Object o)) { | |
34 for (final e in source) { | 32 for (final e in source) { |
35 destination.add(f(e)); | 33 destination.add(f(e)); |
36 } | 34 } |
37 return destination; | 35 return destination; |
38 } | 36 } |
39 | 37 |
40 static List<Object> filter(Iterable<Object> source, | 38 static List filter(Iterable source, List destination, bool f(o)) { |
41 List<Object> destination, | |
42 bool f(Object o)) { | |
43 for (final e in source) { | 39 for (final e in source) { |
44 if (f(e)) destination.add(e); | 40 if (f(e)) destination.add(e); |
45 } | 41 } |
46 return destination; | 42 return destination; |
47 } | 43 } |
48 | 44 |
49 static bool isEmpty(Iterable<Object> iterable) { | 45 static bool isEmpty(Iterable iterable) { |
50 return !iterable.iterator().hasNext(); | 46 return !iterable.iterator().hasNext(); |
51 } | 47 } |
52 | 48 |
53 // TODO(jjb): visiting list should be an identityHashSet when it exists | 49 // TODO(jjb): visiting list should be an identityHashSet when it exists |
54 | 50 |
55 /** | 51 /** |
56 * Returns a string representing the specified collection. If the | 52 * Returns a string representing the specified collection. If the |
57 * collection is a [List], the returned string looks like this: | 53 * collection is a [List], the returned string looks like this: |
58 * [:'[element0, element1, ... elementN]':]. The value returned by its | 54 * [:'[element0, element1, ... elementN]':]. The value returned by its |
59 * [toString] method is used to represent each element. If the specified | 55 * [toString] method is used to represent each element. If the specified |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 * Returns true if the specified collection contains the specified object | 137 * Returns true if the specified collection contains the specified object |
142 * reference. | 138 * reference. |
143 */ | 139 */ |
144 static _containsRef(Collection c, Object ref) { | 140 static _containsRef(Collection c, Object ref) { |
145 for (var e in c) { | 141 for (var e in c) { |
146 if (e === ref) return true; | 142 if (e === ref) return true; |
147 } | 143 } |
148 return false; | 144 return false; |
149 } | 145 } |
150 } | 146 } |
OLD | NEW |