Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * This contains extra functions and classes useful for implementing | |
| 7 * serialiation. Some or all of them might be removable with changes to the | |
|
Jennifer Messerly
2012/11/20 01:20:47
perhaps reword second sentence?
Some or all of th
Alan Knight
2012/11/20 12:17:54
Done.
| |
| 8 * core library. | |
| 9 */ | |
| 10 library serialization_helpers; | |
| 11 | |
| 12 /** | |
| 13 * A named function of one argument that just returns it. Useful for using | |
| 14 * as a default value for a function parameter or other places where you want | |
| 15 * to concisely provide a function that just returns its argument. | |
| 16 */ | |
| 17 doNothing(x) => x; | |
| 18 | |
| 19 /** Concatenate two lists. Handle the case where one or both might be null. */ | |
|
Jennifer Messerly
2012/11/20 01:20:47
this seems to me like it should exist in core libr
Alan Knight
2012/11/20 12:17:54
Done. Bug existed already, but added the TODO refe
| |
| 20 append(List a, List b) { | |
|
Jennifer Messerly
2012/11/20 01:20:47
return type List
| |
| 21 if (a == null) { | |
| 22 if (b == null) return []; else return new List.from(b); | |
|
Jennifer Messerly
2012/11/20 01:20:47
should use curlies here:
http://www.dartlang.org/a
Alan Knight
2012/11/20 12:17:54
Rewrote it using ? :
| |
| 23 } | |
| 24 if (b == null) return new List.from(a); | |
| 25 var result = new List.from(a); | |
| 26 result.addAll(b); | |
| 27 return result; | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * Return a sorted version of [aCollection], using the default sort criterion. | |
| 32 * Always returns a List, regardless of the type of [aCollection]. | |
| 33 */ | |
| 34 List sorted(aCollection) { | |
| 35 var result = new List.from(aCollection); | |
| 36 result.sort(); | |
| 37 return result; | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Be able to iterate polymorphically between List-like and Map-like things. | |
| 42 * For example, keysAndValues(["a", "b", "c"]).forEach((key, value) => ...); | |
| 43 * will loop over the key/value pairs 1/"a", 2/"b", 3/"c", as if the argument | |
| 44 * was a Map from integer keys to string values. | |
| 45 * Only supports forEach() and map() operations because that was all the code | |
| 46 * needed for the moment. | |
| 47 */ | |
| 48 MapLikeIterable keysAndValues(x) => new MapLikeIterable(x); | |
| 49 | |
| 50 /** | |
| 51 * A class for iterating over things as if they were Maps, which primarily | |
| 52 * means that forEach() and map() pass two arguments, and map() returns a new | |
| 53 * Map with the same keys as [collection] and values which have been transformed | |
| 54 * by the argument to map(). | |
| 55 */ | |
| 56 class MapLikeIterable { | |
|
Jennifer Messerly
2012/11/20 01:20:47
should this iterate a key-value pairs? then it cou
Alan Knight
2012/11/20 12:17:54
It's possible, but then the iteration would be dif
| |
| 57 MapLikeIterable(this.collection); | |
| 58 final collection; | |
| 59 | |
| 60 /** Iterate over the collection, passing both key and value parameters. */ | |
| 61 void forEach(Function f) { | |
| 62 if (collection is Map) { | |
|
Jennifer Messerly
2012/11/20 01:20:47
this seems to me like there should be two subtypes
Alan Knight
2012/11/20 12:17:54
Yes, these are definitely a short-term hack to do
| |
| 63 collection.forEach(f); | |
|
Jennifer Messerly
2012/11/20 01:20:47
not sure how I feel about forEach and iterator beh
Alan Knight
2012/11/20 12:17:54
Yes, that's unpleasant. The iterator method wasn't
| |
| 64 } else { | |
| 65 Iterator iterator = collection.iterator(); | |
| 66 for (var i = 0; i < collection.length; i++) { | |
| 67 f(i, iterator.next()); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Return a new collection whose keys are the same as [collection], but whose | |
| 74 * values are the result of applying [f] to the key/value pairs. So, if | |
| 75 * [collection] is a List, it will be the same as the map() method if the | |
| 76 * [key] parameter wasn't passed. | |
| 77 */ | |
| 78 map(Function f) { | |
| 79 var result = copyEmpty(); | |
| 80 forEach((key, value) { | |
| 81 result[key] = f(key, value); | |
| 82 }); | |
| 83 return result; | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * Return an empty copy of our collection. Very limited, only knows enough | |
| 88 * to return a Map or List as appropriate. | |
| 89 */ | |
| 90 copyEmpty() { | |
| 91 if (collection is Map) return new Map(); | |
| 92 return new List(collection.length); | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * Return an iterator for [collection]. If it's a Map, then return an iterator | |
| 97 * on its values. Otherwise just return the collection's normal iterator. | |
| 98 */ | |
| 99 Iterator iterator() { | |
| 100 if (collection is Map) { | |
| 101 return collection.values.iterator(); | |
| 102 } else { | |
| 103 return collection.iterator(); | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * An inverse of MapLikeIterable. Lets you iterate polymorphically between | |
| 110 * List-like and Map-like things, but making them behave like Lists, instead | |
| 111 * of behaving like Maps. | |
| 112 * So values(["a", "b", "c"]).forEach((value) => ...); | |
| 113 * will loop over the values "a", "b", "c", as if it were a List of values. | |
| 114 * Only supports forEach() and map() operations because that was all I needed | |
| 115 * for the moment. | |
| 116 */ | |
| 117 ListLikeIterable values(x) => new ListLikeIterable(x); | |
| 118 | |
| 119 /** | |
| 120 * A class for iterating over things as if they were Lists, which primarily | |
| 121 * means that forEach passes one argument, and map() returns a new Map | |
| 122 * with the same keys as [collection] and values which haev been transformed | |
| 123 * by the argument to map(). | |
| 124 */ | |
| 125 class ListLikeIterable { | |
|
Jennifer Messerly
2012/11/20 01:20:47
I'm not seeing the point of this class. If collect
Alan Knight
2012/11/20 12:17:54
Split this out so that if you call values() on an
| |
| 126 ListLikeIterable(this.collection); | |
| 127 final collection; | |
| 128 | |
| 129 /** Iterate over the collection, passing just the value parameters. */ | |
| 130 forEach(f) { | |
| 131 if (collection is Map) { | |
| 132 collection.forEach((key, value) => f(value)); | |
|
Jennifer Messerly
2012/11/20 01:20:47
is this just collection.values.forEach(f)?
Alan Knight
2012/11/20 12:17:54
Yes, but avoids asking for values, which can be an
| |
| 133 } else { | |
| 134 collection.forEach(f); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * Return a new collection whose keys are the same as [collection], but whose | |
| 140 * values are the result of applying [f] to the key/value pairs. So, if | |
| 141 * [collection] is a List, it will be the same as if map() had been called | |
| 142 * directly on [collection]. | |
| 143 */ | |
| 144 map(Function f) { | |
| 145 if (collection is Map) { | |
| 146 var result = copyEmpty(); | |
| 147 collection.forEach((key, value) { | |
| 148 result[key] = f(value); | |
| 149 }); | |
| 150 return result; | |
| 151 } else { | |
| 152 return collection.map(f); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Return an empty copy of our collection. Very limited, only knows enough | |
| 158 * to return a Map or List as appropriate. | |
| 159 */ | |
| 160 copyEmpty() { | |
| 161 if (collection is Map) return new Map(); | |
| 162 return new List(collection.length); | |
| 163 } | |
| 164 | |
| 165 /** | |
| 166 * Return an iterator that behaves like a List iterator, taking one parameter. | |
| 167 */ | |
| 168 Iterator iterator() { | |
| 169 if (collection is Map) { | |
| 170 return collection.values.iterator(); | |
| 171 } else { | |
| 172 return collection.iterator(); | |
| 173 } | |
| 174 } | |
| 175 } | |
| OLD | NEW |