| 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 | 8 |
| 9 /// A pair of values. |
| 10 class Pair<E, F> { |
| 11 E first; |
| 12 F last; |
| 13 |
| 14 Pair(this.first, this.last); |
| 15 |
| 16 String toString() => '($first, $last)'; |
| 17 |
| 18 bool operator==(other) { |
| 19 if (other is! Pair) return false; |
| 20 return other.first == first && other.last == last; |
| 21 } |
| 22 |
| 23 int get hashCode => first.hashCode ^ last.hashCode; |
| 24 } |
| 25 |
| 9 /// Converts a number in the range [0-255] to a two digit hex string. | 26 /// Converts a number in the range [0-255] to a two digit hex string. |
| 10 /// | 27 /// |
| 11 /// For example, given `255`, returns `ff`. | 28 /// For example, given `255`, returns `ff`. |
| 12 String byteToHex(int byte) { | 29 String byteToHex(int byte) { |
| 13 assert(byte >= 0 && byte <= 255); | 30 assert(byte >= 0 && byte <= 255); |
| 14 | 31 |
| 15 const DIGITS = "0123456789abcdef"; | 32 const DIGITS = "0123456789abcdef"; |
| 16 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16]; | 33 return DIGITS[(byte ~/ 16) % 16] + DIGITS[byte % 16]; |
| 17 } | 34 } |
| 18 | 35 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 39 helper(element); | 56 helper(element); |
| 40 } else { | 57 } else { |
| 41 result.add(element); | 58 result.add(element); |
| 42 } | 59 } |
| 43 } | 60 } |
| 44 } | 61 } |
| 45 helper(nested); | 62 helper(nested); |
| 46 return result; | 63 return result; |
| 47 } | 64 } |
| 48 | 65 |
| 66 /// Returns the union of all elements in each set in [sets]. |
| 67 Set unionAll(Iterable<Set> sets) => |
| 68 sets.fold(new Set(), (union, set) => union.union(set)); |
| 69 |
| 49 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose | 70 /// Passes each key/value pair in [map] to [fn] and returns a new [Map] whose |
| 50 /// values are the return values of [fn]. | 71 /// values are the return values of [fn]. |
| 51 Map mapMapValues(Map map, fn(key, value)) => | 72 Map mapMapValues(Map map, fn(key, value)) => |
| 52 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); | 73 new Map.fromIterable(map.keys, value: (key) => fn(key, map[key])); |
| 53 | 74 |
| 54 /// Merges [streams] into a single stream that emits events from all sources. | 75 /// Merges [streams] into a single stream that emits events from all sources. |
| 55 Stream mergeStreams(Iterable<Stream> streams) { | 76 Stream mergeStreams(Iterable<Stream> streams) { |
| 56 streams = streams.toList(); | 77 streams = streams.toList(); |
| 57 var doneCount = 0; | 78 var doneCount = 0; |
| 58 // Use a sync stream to preserve the synchrony behavior of the input streams. | 79 // Use a sync stream to preserve the synchrony behavior of the input streams. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // We use a delayed future to allow runAsync events to finish. The | 118 // We use a delayed future to allow runAsync events to finish. The |
| 98 // Future.value or Future() constructors use runAsync themselves and would | 119 // Future.value or Future() constructors use runAsync themselves and would |
| 99 // therefore not wait for runAsync callbacks that are scheduled after invoking | 120 // therefore not wait for runAsync callbacks that are scheduled after invoking |
| 100 // this method. | 121 // this method. |
| 101 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 122 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 102 } | 123 } |
| 103 | 124 |
| 104 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under | 125 /// Like [new Future], but avoids issue 11911 by using [new Future.value] under |
| 105 /// the covers. | 126 /// the covers. |
| 106 Future newFuture(callback()) => new Future.value().then((_) => callback()); | 127 Future newFuture(callback()) => new Future.value().then((_) => callback()); |
| OLD | NEW |