Chromium Code Reviews| Index: pkg/serialization/lib/src/serialization_helpers.dart |
| =================================================================== |
| --- pkg/serialization/lib/src/serialization_helpers.dart (revision 0) |
| +++ pkg/serialization/lib/src/serialization_helpers.dart (revision 0) |
| @@ -0,0 +1,175 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * This contains extra functions and classes useful for implementing |
| + * 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.
|
| + * core library. |
| + */ |
| +library serialization_helpers; |
| + |
| +/** |
| + * A named function of one argument that just returns it. Useful for using |
| + * as a default value for a function parameter or other places where you want |
| + * to concisely provide a function that just returns its argument. |
| + */ |
| +doNothing(x) => x; |
| + |
| +/** 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
|
| +append(List a, List b) { |
|
Jennifer Messerly
2012/11/20 01:20:47
return type List
|
| + if (a == null) { |
| + 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 ? :
|
| + } |
| + if (b == null) return new List.from(a); |
| + var result = new List.from(a); |
| + result.addAll(b); |
| + return result; |
| +} |
| + |
| +/** |
| + * Return a sorted version of [aCollection], using the default sort criterion. |
| + * Always returns a List, regardless of the type of [aCollection]. |
| + */ |
| +List sorted(aCollection) { |
| + var result = new List.from(aCollection); |
| + result.sort(); |
| + return result; |
| +} |
| + |
| +/** |
| + * Be able to iterate polymorphically between List-like and Map-like things. |
| + * For example, keysAndValues(["a", "b", "c"]).forEach((key, value) => ...); |
| + * will loop over the key/value pairs 1/"a", 2/"b", 3/"c", as if the argument |
| + * was a Map from integer keys to string values. |
| + * Only supports forEach() and map() operations because that was all the code |
| + * needed for the moment. |
| + */ |
| +MapLikeIterable keysAndValues(x) => new MapLikeIterable(x); |
| + |
| +/** |
| + * A class for iterating over things as if they were Maps, which primarily |
| + * means that forEach() and map() pass two arguments, and map() returns a new |
| + * Map with the same keys as [collection] and values which have been transformed |
| + * by the argument to map(). |
| + */ |
| +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
|
| + MapLikeIterable(this.collection); |
| + final collection; |
| + |
| + /** Iterate over the collection, passing both key and value parameters. */ |
| + void forEach(Function f) { |
| + 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
|
| + 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
|
| + } else { |
| + Iterator iterator = collection.iterator(); |
| + for (var i = 0; i < collection.length; i++) { |
| + f(i, iterator.next()); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Return a new collection whose keys are the same as [collection], but whose |
| + * values are the result of applying [f] to the key/value pairs. So, if |
| + * [collection] is a List, it will be the same as the map() method if the |
| + * [key] parameter wasn't passed. |
| + */ |
| + map(Function f) { |
| + var result = copyEmpty(); |
| + forEach((key, value) { |
| + result[key] = f(key, value); |
| + }); |
| + return result; |
| + } |
| + |
| + /** |
| + * Return an empty copy of our collection. Very limited, only knows enough |
| + * to return a Map or List as appropriate. |
| + */ |
| + copyEmpty() { |
| + if (collection is Map) return new Map(); |
| + return new List(collection.length); |
| + } |
| + |
| + /** |
| + * Return an iterator for [collection]. If it's a Map, then return an iterator |
| + * on its values. Otherwise just return the collection's normal iterator. |
| + */ |
| + Iterator iterator() { |
| + if (collection is Map) { |
| + return collection.values.iterator(); |
| + } else { |
| + return collection.iterator(); |
| + } |
| + } |
| +} |
| + |
| +/** |
| + * An inverse of MapLikeIterable. Lets you iterate polymorphically between |
| + * List-like and Map-like things, but making them behave like Lists, instead |
| + * of behaving like Maps. |
| + * So values(["a", "b", "c"]).forEach((value) => ...); |
| + * will loop over the values "a", "b", "c", as if it were a List of values. |
| + * Only supports forEach() and map() operations because that was all I needed |
| + * for the moment. |
| + */ |
| +ListLikeIterable values(x) => new ListLikeIterable(x); |
| + |
| +/** |
| + * A class for iterating over things as if they were Lists, which primarily |
| + * means that forEach passes one argument, and map() returns a new Map |
| + * with the same keys as [collection] and values which haev been transformed |
| + * by the argument to map(). |
| + */ |
| +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
|
| + ListLikeIterable(this.collection); |
| + final collection; |
| + |
| + /** Iterate over the collection, passing just the value parameters. */ |
| + forEach(f) { |
| + if (collection is Map) { |
| + 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
|
| + } else { |
| + collection.forEach(f); |
| + } |
| + } |
| + |
| + /** |
| + * Return a new collection whose keys are the same as [collection], but whose |
| + * values are the result of applying [f] to the key/value pairs. So, if |
| + * [collection] is a List, it will be the same as if map() had been called |
| + * directly on [collection]. |
| + */ |
| + map(Function f) { |
| + if (collection is Map) { |
| + var result = copyEmpty(); |
| + collection.forEach((key, value) { |
| + result[key] = f(value); |
| + }); |
| + return result; |
| + } else { |
| + return collection.map(f); |
| + } |
| + } |
| + |
| + /** |
| + * Return an empty copy of our collection. Very limited, only knows enough |
| + * to return a Map or List as appropriate. |
| + */ |
| + copyEmpty() { |
| + if (collection is Map) return new Map(); |
| + return new List(collection.length); |
| + } |
| + |
| + /** |
| + * Return an iterator that behaves like a List iterator, taking one parameter. |
| + */ |
| + Iterator iterator() { |
| + if (collection is Map) { |
| + return collection.values.iterator(); |
| + } else { |
| + return collection.iterator(); |
| + } |
| + } |
| +} |