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 /** | 5 /** |
6 * The common interface of all collections. | 6 * The common interface of all collections. |
7 * | 7 * |
8 * The [Collection] class contains a skeleton implementation of | 8 * The [Collection] class contains a skeleton implementation of |
9 * an iterator based collection. | 9 * an iterator based collection. |
10 */ | 10 */ |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 /** | 56 /** |
57 * Reduce a collection to a single value by iteratively combining each element | 57 * Reduce a collection to a single value by iteratively combining each element |
58 * of the collection with an existing value using the provided function. | 58 * of the collection with an existing value using the provided function. |
59 * Use [initialValue] as the initial value, and the function [combine] to | 59 * Use [initialValue] as the initial value, and the function [combine] to |
60 * create a new value from the previous one and an element. | 60 * create a new value from the previous one and an element. |
61 * | 61 * |
62 * Example of calculating the sum of a collection: | 62 * Example of calculating the sum of a collection: |
63 * | 63 * |
64 * collection.reduce(0, (prev, element) => prev + element); | 64 * collection.reduce(0, (prev, element) => prev + element); |
65 */ | 65 */ |
66 Dynamic reduce(var initialValue, | 66 dynamic reduce(var initialValue, |
67 Dynamic combine(var previousValue, E element)) { | 67 dynamic combine(var previousValue, E element)) { |
68 var value = initialValue; | 68 var value = initialValue; |
69 for (E element in this) value = combine(value, element); | 69 for (E element in this) value = combine(value, element); |
70 return value; | 70 return value; |
71 } | 71 } |
72 | 72 |
73 /** | 73 /** |
74 * Returns true if every elements of this collection satisify the | 74 * Returns true if every elements of this collection satisify the |
75 * predicate [f]. Returns false otherwise. | 75 * predicate [f]. Returns false otherwise. |
76 */ | 76 */ |
77 bool every(bool f(E element)) { | 77 bool every(bool f(E element)) { |
(...skipping 12 matching lines...) Expand all Loading... |
90 if (f(element)) return true; | 90 if (f(element)) return true; |
91 } | 91 } |
92 return false; | 92 return false; |
93 } | 93 } |
94 | 94 |
95 /** | 95 /** |
96 * Returns true if there is no element in this collection. | 96 * Returns true if there is no element in this collection. |
97 */ | 97 */ |
98 bool get isEmpty => !iterator().hasNext; | 98 bool get isEmpty => !iterator().hasNext; |
99 } | 99 } |
OLD | NEW |