Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.utils; | 5 library barback.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
| 9 | 9 |
| 10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 helper(nested); | 135 helper(nested); |
| 136 return result; | 136 return result; |
| 137 } | 137 } |
| 138 | 138 |
| 139 /// Returns the union of all elements in each set in [sets]. | 139 /// Returns the union of all elements in each set in [sets]. |
| 140 Set unionAll(Iterable<Set> sets) => | 140 Set unionAll(Iterable<Set> sets) => |
| 141 sets.fold(new Set(), (union, set) => union.union(set)); | 141 sets.fold(new Set(), (union, set) => union.union(set)); |
| 142 | 142 |
| 143 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose | 143 /// Creates a map from [iter], using the return values of [keyFn] as the keys |
| 144 /// values are the return values of [fn]. | 144 /// and the return values of [valueFn] as the values. |
| 145 Map mapMapValues(Map map, fn(key, value)) => | 145 Map listToMap(Iterable iter, keyFn(element), valueFn(element)) { |
|
Bob Nystrom
2014/01/30 19:33:44
Is this any different from new Map.fromIterable()?
nweiz
2014/01/31 03:43:27
It's not. Removed.
| |
| 146 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); | 146 var map = new Map(); |
| 147 for (var element in iter) { | |
| 148 map[keyFn(element)] = valueFn(element); | |
| 149 } | |
| 150 return map; | |
| 151 } | |
| 152 | |
| 153 /// Creates a new map from [map] with new keys and values. | |
| 154 /// | |
| 155 /// The return values of [keyFn] are used as the keys and the return values of | |
| 156 /// [valueFn] are used as the values for the new map. | |
| 157 Map mapMap(Map map, keyFn(key, value), valueFn(key, value)) => | |
| 158 listToMap(map.keys, | |
| 159 (key) => keyFn(key, map[key]), | |
| 160 (key) => valueFn(key, map[key])); | |
| 161 | |
| 162 /// Creates a new map from [map] with the same keys. | |
| 163 /// | |
| 164 /// The return values of [fn] are used as the values for the new map. | |
| 165 Map mapMapValues(Map map, fn(key, value)) => mapMap(map, (key, _) => key, fn); | |
| 166 | |
| 167 /// Creates a new map from [map] with the same keys. | |
| 168 /// | |
| 169 /// The return values of [fn] are used as the keys for the new map. | |
| 170 Map mapMapKeys(Map map, fn(key, value)) => mapMap(map, fn, (_, value) => value); | |
|
Bob Nystrom
2014/01/30 19:33:44
Not in this patch, but we should look at moving th
nweiz
2014/01/31 03:43:27
Absolutely.
| |
| 147 | 171 |
| 148 /// Returns whether [set1] has exactly the same elements as [set2]. | 172 /// Returns whether [set1] has exactly the same elements as [set2]. |
| 149 bool setEquals(Set set1, Set set2) => | 173 bool setEquals(Set set1, Set set2) => |
| 150 set1.length == set2.length && set1.containsAll(set2); | 174 set1.length == set2.length && set1.containsAll(set2); |
| 151 | 175 |
| 152 /// Merges [streams] into a single stream that emits events from all sources. | 176 /// Merges [streams] into a single stream that emits events from all sources. |
| 153 /// | 177 /// |
| 154 /// If [broadcast] is true, this will return a broadcast stream; otherwise, it | 178 /// If [broadcast] is true, this will return a broadcast stream; otherwise, it |
| 155 /// will return a buffered stream. | 179 /// will return a buffered stream. |
| 156 Stream mergeStreams(Iterable<Stream> streams, {bool broadcast: false}) { | 180 Stream mergeStreams(Iterable<Stream> streams, {bool broadcast: false}) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 subscription = callback().listen(controller.add, | 292 subscription = callback().listen(controller.add, |
| 269 onError: controller.addError, | 293 onError: controller.addError, |
| 270 onDone: controller.close); | 294 onDone: controller.close); |
| 271 }, | 295 }, |
| 272 onCancel: () => subscription.cancel(), | 296 onCancel: () => subscription.cancel(), |
| 273 onPause: () => subscription.pause(), | 297 onPause: () => subscription.pause(), |
| 274 onResume: () => subscription.resume(), | 298 onResume: () => subscription.resume(), |
| 275 sync: true); | 299 sync: true); |
| 276 return controller.stream; | 300 return controller.stream; |
| 277 } | 301 } |
| OLD | NEW |