| 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 * This contains extra functions and classes useful for implementing | 6 * This contains extra functions and classes useful for implementing |
| 7 * serialiation. Some or all of these will be removed once the functionality is | 7 * serialiation. Some or all of these will be removed once the functionality is |
| 8 * available in the core library. | 8 * available in the core library. |
| 9 */ | 9 */ |
| 10 library serialization_helpers; | 10 library serialization_helpers; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 /** Iterate over the collection, passing both key and value parameters. */ | 71 /** Iterate over the collection, passing both key and value parameters. */ |
| 72 void forEach(Function f); | 72 void forEach(Function f); |
| 73 | 73 |
| 74 /** | 74 /** |
| 75 * Return a new collection whose keys are the same as [collection], but whose | 75 * Return a new collection whose keys are the same as [collection], but whose |
| 76 * values are the result of applying [f] to the key/value pairs. So, if | 76 * values are the result of applying [f] to the key/value pairs. So, if |
| 77 * [collection] is a List, it will be the same as the map() method if the | 77 * [collection] is a List, it will be the same as the map() method if the |
| 78 * [key] parameter wasn't passed. | 78 * [key] parameter wasn't passed. |
| 79 */ | 79 */ |
| 80 map(Function f) { | 80 mappedBy(Function f) { |
| 81 var result = copyEmpty(); | 81 var result = copyEmpty(); |
| 82 forEach((key, value) { | 82 forEach((key, value) { |
| 83 result[key] = f(key, value); | 83 result[key] = f(key, value); |
| 84 }); | 84 }); |
| 85 return result; | 85 return result; |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Return an empty copy of our collection. Very limited, only knows enough | 89 * Return an empty copy of our collection. Very limited, only knows enough |
| 90 * to return a Map or List as appropriate. | 90 * to return a Map or List as appropriate. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 * Only supports forEach() and map() operations because that was all I needed | 124 * Only supports forEach() and map() operations because that was all I needed |
| 125 * for the moment. | 125 * for the moment. |
| 126 */ | 126 */ |
| 127 values(x) { | 127 values(x) { |
| 128 if (x is Iterable) return x; | 128 if (x is Iterable) return x; |
| 129 if (x is Map) return new ListLikeIterable(x); | 129 if (x is Map) return new ListLikeIterable(x); |
| 130 throw new ArgumentError("Invalid argument"); | 130 throw new ArgumentError("Invalid argument"); |
| 131 } | 131 } |
| 132 | 132 |
| 133 mapValues(x, f) { | 133 mapValues(x, f) { |
| 134 if (x is Set) return x.map(f).toSet(); | 134 if (x is Set) return x.mappedBy(f).toSet(); |
| 135 if (x is Iterable) return x.map(f).toList(); | 135 if (x is Iterable) return x.mappedBy(f).toList(); |
| 136 if (x is Map) return new ListLikeIterable(x).map(f); | 136 if (x is Map) return new ListLikeIterable(x).mappedBy(f); |
| 137 throw new ArgumentError("Invalid argument"); | 137 throw new ArgumentError("Invalid argument"); |
| 138 } | 138 } |
| 139 | 139 |
| 140 /** | 140 /** |
| 141 * A class for iterating over things as if they were Lists, which primarily | 141 * A class for iterating over things as if they were Lists, which primarily |
| 142 * means that forEach passes one argument, and map() returns a new Map | 142 * means that forEach passes one argument, and map() returns a new Map |
| 143 * with the same keys as [collection] and values which haev been transformed | 143 * with the same keys as [collection] and values which haev been transformed |
| 144 * by the argument to map(). | 144 * by the argument to map(). |
| 145 */ | 145 */ |
| 146 class ListLikeIterable { | 146 class ListLikeIterable { |
| 147 ListLikeIterable(this.collection); | 147 ListLikeIterable(this.collection); |
| 148 final Map collection; | 148 final Map collection; |
| 149 | 149 |
| 150 /** Iterate over the collection, passing just the value parameters. */ | 150 /** Iterate over the collection, passing just the value parameters. */ |
| 151 forEach(f) { | 151 forEach(f) { |
| 152 collection.forEach((key, value) => f(value)); | 152 collection.forEach((key, value) => f(value)); |
| 153 } | 153 } |
| 154 | 154 |
| 155 /** | 155 /** |
| 156 * Return a new collection whose keys are the same as [collection], but whose | 156 * Return a new collection whose keys are the same as [collection], but whose |
| 157 * values are the result of applying [f] to the key/value pairs. So, if | 157 * values are the result of applying [f] to the key/value pairs. So, if |
| 158 * [collection] is a List, it will be the same as if map() had been called | 158 * [collection] is a List, it will be the same as if map() had been called |
| 159 * directly on [collection]. | 159 * directly on [collection]. |
| 160 */ | 160 */ |
| 161 map(Function f) { | 161 mappedBy(Function f) { |
| 162 var result = new Map(); | 162 var result = new Map(); |
| 163 collection.forEach((key, value) => result[key] = f(value)); | 163 collection.forEach((key, value) => result[key] = f(value)); |
| 164 return result; | 164 return result; |
| 165 } | 165 } |
| 166 | 166 |
| 167 /** | 167 /** |
| 168 * Return an iterator that behaves like a List iterator, taking one parameter. | 168 * Return an iterator that behaves like a List iterator, taking one parameter. |
| 169 */ | 169 */ |
| 170 Iterator get iterator => collection.values.iterator; | 170 Iterator get iterator => collection.values.iterator; |
| 171 } | 171 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 244 |
| 245 int get length => keys.length; | 245 int get length => keys.length; |
| 246 void clear() { | 246 void clear() { |
| 247 keys.clear(); | 247 keys.clear(); |
| 248 values.clear(); | 248 values.clear(); |
| 249 } | 249 } |
| 250 bool get isEmpty => keys.isEmpty; | 250 bool get isEmpty => keys.isEmpty; |
| 251 | 251 |
| 252 // Note that this is doing an equality comparison. | 252 // Note that this is doing an equality comparison. |
| 253 bool containsValue(x) => values.contains(x); | 253 bool containsValue(x) => values.contains(x); |
| 254 } | 254 } |
| OLD | NEW |