| 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 typedef num NumericValueSelector<T>(T value); | 5 typedef num NumericValueSelector<T>(T value); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * General purpose collection utilities. | 8 * General purpose collection utilities. |
| 9 * TODO(jmesserly): make these top level functions? | 9 * TODO(jmesserly): make these top level functions? |
| 10 */ | 10 */ |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 for (final item in source) { | 64 for (final item in source) { |
| 65 if (test(item)) return item; | 65 if (test(item)) return item; |
| 66 } | 66 } |
| 67 | 67 |
| 68 return null; | 68 return null; |
| 69 } | 69 } |
| 70 | 70 |
| 71 /** Compute the minimum of an iterable. Returns null if empty. */ | 71 /** Compute the minimum of an iterable. Returns null if empty. */ |
| 72 static num min(Iterable source) { | 72 static num min(Iterable source) { |
| 73 final iter = source.iterator(); | 73 final iter = source.iterator(); |
| 74 if (!iter.hasNext()) { | 74 if (!iter.hasNext) { |
| 75 return null; | 75 return null; |
| 76 } | 76 } |
| 77 num best = iter.next(); | 77 num best = iter.next(); |
| 78 while (iter.hasNext()) { | 78 while (iter.hasNext) { |
| 79 best = Math.min(best, iter.next()); | 79 best = Math.min(best, iter.next()); |
| 80 } | 80 } |
| 81 return best; | 81 return best; |
| 82 } | 82 } |
| 83 | 83 |
| 84 /** Compute the maximum of an iterable. Returns null if empty. */ | 84 /** Compute the maximum of an iterable. Returns null if empty. */ |
| 85 static num max(Iterable source) { | 85 static num max(Iterable source) { |
| 86 final iter = source.iterator(); | 86 final iter = source.iterator(); |
| 87 if (!iter.hasNext()) { | 87 if (!iter.hasNext) { |
| 88 return null; | 88 return null; |
| 89 } | 89 } |
| 90 num best = iter.next(); | 90 num best = iter.next(); |
| 91 while (iter.hasNext()) { | 91 while (iter.hasNext) { |
| 92 best = Math.max(best, iter.next()); | 92 best = Math.max(best, iter.next()); |
| 93 } | 93 } |
| 94 return best; | 94 return best; |
| 95 } | 95 } |
| 96 | 96 |
| 97 /** Orders an iterable by its values, or by a key selector. */ | 97 /** Orders an iterable by its values, or by a key selector. */ |
| 98 static List orderBy(Iterable source, [NumericValueSelector selector = null]) { | 98 static List orderBy(Iterable source, [NumericValueSelector selector = null]) { |
| 99 final result = new List.from(source); | 99 final result = new List.from(source); |
| 100 sortBy(result, selector); | 100 sortBy(result, selector); |
| 101 return result; | 101 return result; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 /** Compute the sum of an iterable. An empty iterable is an error. */ | 116 /** Compute the sum of an iterable. An empty iterable is an error. */ |
| 117 static num sum(Iterable source, [NumericValueSelector selector = null]) { | 117 static num sum(Iterable source, [NumericValueSelector selector = null]) { |
| 118 final iter = source.iterator(); | 118 final iter = source.iterator(); |
| 119 num total = 0; | 119 num total = 0; |
| 120 if (selector != null) { | 120 if (selector != null) { |
| 121 do { | 121 do { |
| 122 total += selector(iter.next()); | 122 total += selector(iter.next()); |
| 123 } while (iter.hasNext()); | 123 } while (iter.hasNext); |
| 124 } else { | 124 } else { |
| 125 do { | 125 do { |
| 126 total += iter.next(); | 126 total += iter.next(); |
| 127 } while (iter.hasNext()); | 127 } while (iter.hasNext); |
| 128 } | 128 } |
| 129 return total; | 129 return total; |
| 130 } | 130 } |
| 131 | 131 |
| 132 // TODO(jmesserly): something like should exist on Map, either a method or a | 132 // TODO(jmesserly): something like should exist on Map, either a method or a |
| 133 // constructor, see bug #5340679 | 133 // constructor, see bug #5340679 |
| 134 static void copyMap(Map dest, Map source) { | 134 static void copyMap(Map dest, Map source) { |
| 135 for (final k in source.getKeys()) { | 135 for (final k in source.getKeys()) { |
| 136 dest[k] = source[k]; | 136 dest[k] = source[k]; |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 } | 139 } |
| OLD | NEW |