Chromium Code Reviews| Index: sdk/lib/core/list.dart |
| diff --git a/sdk/lib/core/list.dart b/sdk/lib/core/list.dart |
| index 3c27d04e1ab7c00c51d7a26fea58eff7e1fd55f4..1b9fbcf69b138eed9fa8eecceaea7c1728632bc8 100644 |
| --- a/sdk/lib/core/list.dart |
| +++ b/sdk/lib/core/list.dart |
| @@ -19,42 +19,50 @@ part of dart.core; |
| * The following code illustrates that some List implementations support |
| * only a subset of the API. |
| * |
| - * var fixedLengthList = new List(5); |
| - * fixedLengthList.length = 0; // Error. |
| - * fixedLengthList.add(499); // Error. |
| - * fixedLengthList[0] = 87; |
| - * |
| - * var growableList = [1, 2]; |
| - * growableList.length = 0; |
| - * growableList.add(499); |
| - * growableList[0] = 87; |
| + List<int> fixedLengthList = new List(5); |
| + fixedLengthList.length = 0; // Error. |
| + fixedLengthList.add(499); // Error. |
| + fixedLengthList[0] = 87; |
| + |
| + List<int> growableList = [1, 2]; |
| + growableList.length = 0; |
| + growableList.add(499); |
| + growableList[0] = 87; |
| * |
| - * Lists are [Iterable]. |
| - * Iteration occurs over values in index order. |
| - * Changing the values does not affect iteration, |
| - * but changing the valid indices—that is, |
| - * changing the list's length—between |
| - * iteration steps |
| - * causes a [ConcurrentModificationError]. |
| - * This means that only growable lists can throw ConcurrentModificationError. |
| - * If the length changes temporarily |
| - * and is restored before continuing the iteration, |
| - * the iterator does not detect it. |
| + * Lists are [Iterable]. Iteration occurs over values in index order. Changing |
| + * the values does not affect iteration, but changing the valid |
| + * indices—that is, changing the list's length—between iteration |
| + * steps causes a [ConcurrentModificationError]. This means that only growable |
| + * lists can throw ConcurrentModificationError. If the length changes |
| + * temporarily and is restored before continuing the iteration, the iterator |
| + * does not detect it. |
| */ |
| abstract class List<E> implements Iterable<E> { |
| /** |
| * Creates a list of the given _length_. |
| * |
| * The created list is fixed-length if _length_ is provided. |
| + * |
| + * List fixedLengthList = new List(3); |
| + * fixedLengthList.length; // 3 |
| + fixedLengthList.length = 1; // Error |
|
mem
2013/09/06 16:28:16
Error.
With a period. We're using this convention
shailentuli
2013/09/23 11:48:54
Done.
|
| + * |
| + * |
| * The list has length 0 and is growable if _length_ is omitted. |
| * |
| + * List growableList = new List(); |
| + * growableList.length; // 0; |
| + * growableList.length = 3; |
| + * |
| * An error occurs if _length_ is negative. |
| */ |
| external factory List([int length]); |
| /** |
| * Creates a fixed-length list of the given _length_ |
| - * and initializes the value at each position with [fill]. |
| + * and initializes the value at each position with [fill]: |
| + * |
| + * new List<int>.filled(3, 0); // [0, 0, 0] |
| */ |
| external factory List.filled(int length, E fill); |
| @@ -88,6 +96,8 @@ abstract class List<E> implements Iterable<E> { |
| * for each index in the range `0` .. `length - 1` |
| * in increasing order. |
| * |
| + * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4] |
| + * |
| * The created list is fixed-length unless [growable] is true. |
| */ |
| factory List.generate(int length, E generator(int index), |
| @@ -158,9 +168,18 @@ abstract class List<E> implements Iterable<E> { |
| * Sorts this list according to the order specified by the [compare] function. |
| * |
| * The [compare] function must act as a [Comparator]. |
| + |
| + * List<String> numbers = ['one', 'two', 'three', 'four']; |
| + * // Sort from shortest to longest. |
| + * numbers.sort((x, y) => x.length.compareTo(y.length)); |
|
mem
2013/09/06 16:28:16
it's a pity sort() doesn't return the list.
shailentuli
2013/09/23 11:48:54
Yup, it's an in-place sort.
|
| + * numbers.join(', '); // 'one, two, four, three' |
| * |
| * The default List implementations use [Comparable.compare] if |
| * [compare] is omitted. |
| + * |
| + * List<int> nums = [13, 2, -11]; |
| + * nums.sort(); |
|
mem
2013/09/06 16:28:16
add comment->
nums.sort(); // [ -11, 2, 13]
shailentuli
2013/09/23 11:48:54
This goes against our convention that the // signi
|
| + nums.join(', '); // '-11, 2, 13' |
| */ |
| void sort([int compare(E a, E b)]); |
| @@ -170,7 +189,14 @@ abstract class List<E> implements Iterable<E> { |
| * Searches the list from index [start] to the length of the list. |
|
mem
2013/09/06 16:28:16
length -> end
|
| * The first time an object [:o:] is encountered so that [:o == element:], |
| * the index of [:o:] is returned. |
| + * |
| + * List<String> notes = ['do', 're', 'mi', 're']; |
| + * notes.indexOf('re'); // 1 |
| + * notes.indexOf('re', 1); // 1 |
|
mem
2013/09/06 16:28:16
better if start is 2 and it finds the second one a
shailentuli
2013/09/23 11:48:54
Done.
|
| + * |
| * Returns -1 if [element] is not found. |
| + * |
| + * notes.indexOf('fa'); // -1 |
| */ |
| int indexOf(E element, [int start = 0]); |
| @@ -182,9 +208,16 @@ abstract class List<E> implements Iterable<E> { |
| * The first time an object [:o:] is encountered so that [:o == element:], |
| * the index of [:o:] is returned. |
| * |
| + * List<String> notes = ['do', 're', 'mi', 're']; |
| + * notes.lastIndexOf('re', 2); // 1 |
| + * |
| * If [start] is not provided, it defaults to [:this.length - 1:]. |
|
mem
2013/09/06 16:28:16
it defaults ... -> this method searches from the e
shailentuli
2013/09/23 11:48:54
Done.
|
| * |
| + * notes.lastIndexOf('re'); // 3 |
| + * |
| * Returns -1 if [element] is not found. |
| + * |
| + * notes.lastIndexOf('fa'); // -1 |
| */ |
| int lastIndexOf(E element, [int start]); |
| @@ -192,7 +225,7 @@ abstract class List<E> implements Iterable<E> { |
| * Removes all objects from this list; |
| * the length of the list becomes zero. |
| * |
| - * Throws an [UnsupportedError], and retains all objects, if this |
| + * Throws an [UnsupportedError], and retains all objects, if this |
| * is a fixed-length list. |
| */ |
| void clear(); |
| @@ -223,6 +256,10 @@ abstract class List<E> implements Iterable<E> { |
| * Overwrites objects of `this` with the objects of [iterable], starting |
| * at position [index] in this list. |
| * |
| + * List<String> list = ['a', 'b', 'c']; |
| + * list.setAll(1, ['bee', 'sea']); |
| + * list.join(', '); // 'a, bee, sea' |
| + * |
| * This operation does not increase the length of `this`. |
| * |
| * An error occurs if the [index] is less than 0 or greater than length. |
| @@ -236,8 +273,16 @@ abstract class List<E> implements Iterable<E> { |
| * Returns true if [value] was in the list. |
|
mem
2013/09/06 16:28:16
... in the list, false otherwise.
then delete "Re
shailentuli
2013/09/23 11:48:54
Done.
|
| * Returns false otherwise. |
| * |
| + * List<String> parts = ['head', 'shoulders', 'knees', 'toes']; |
| + * parts.remove('head'); // true |
|
mem
2013/09/06 16:28:16
oh no! a decapitation!
shailentuli
2013/09/23 11:48:54
oh yes!
|
| + * parts.join(', '); // 'shoulders, knees, toes' |
| + * |
| * The method has no effect if [value] was not in the list. |
| * |
| + * // Note: 'head' has already been removed. |
| + * parts.remove('head'); // false |
|
mem
2013/09/06 16:28:16
maybe use 'elbow' here and you can remove the "not
shailentuli
2013/09/23 11:48:54
No, it has to be 'head' to show that `remove()` re
|
| + * parts.join(', '); // 'shoulders, knees, toes' |
| + * |
| * An [UnsupportedError] occurs if the list is fixed-length. |
| */ |
| bool remove(Object value); |
| @@ -269,6 +314,10 @@ abstract class List<E> implements Iterable<E> { |
| * |
| * An object [:o:] satisfies [test] if [:test(o):] is true. |
| * |
| + * List<String> numbers = ['one', 'two', 'three', 'four']; |
| + * numbers.removeWhere((item) => item.length == 3); |
| + * numbers.join(', '); // 'three, four' |
| + * |
| * Throws an [UnsupportedError] if this is a fixed-length list. |
| */ |
| void removeWhere(bool test(E element)); |
| @@ -278,16 +327,25 @@ abstract class List<E> implements Iterable<E> { |
| * |
| * An object [:o:] satisfies [test] if [:test(o):] is true. |
| * |
| + * List<String> numbers = ['one', 'two', 'three', 'four']; |
| + * numbers.retainWhere((item) => item.length == 3); |
| + * numbers.join(', '); // 'one, two' |
| + * |
| * Throws an [UnsupportedError] if this is a fixed-length list. |
| */ |
| void retainWhere(bool test(E element)); |
| /** |
| - * Returns a new list containing the objects |
| - * from [start] inclusive to [end] exclusive. |
| + * Returns a new list containing the objects from [start] inclusive to [end] |
| + * exclusive. |
| + * |
| + * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; |
| + * colors.sublist(1, 3); // ['green', 'blue'] |
| * |
| * If [end] is omitted, the [length] of `this` is used. |
| * |
| + * colors.sublist(1); // ['green', 'blue', 'orange', 'pink'] |
| + * |
| * An error occurs if [start] is outside the range `0` .. `length` or if |
| * [end] is outside the range `start` .. `length`. |
| */ |
| @@ -304,13 +362,11 @@ abstract class List<E> implements Iterable<E> { |
| * `skip(start).take(end - start)`. That is, it does not throw exceptions |
| * if `this` changes size. |
| * |
| - * Example: |
| - * |
| - * var list = [1, 2, 3, 4, 5]; |
| - * var range = list.getRange(1, 4); |
| - * print(range.join(', ')); // => 2, 3, 4 |
| - * list.length = 3; |
| - * print(range.join(', ')); // => 2, 3 |
| + * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink']; |
| + * Iterable<String> range = colors.getRange(1, 4); |
| + * range.join(', '); // 'green, blue, orange' |
| + * colors.length = 3; |
| + * range.join(', '); // 'green, blue' |
| */ |
| Iterable<E> getRange(int start, int end); |
| @@ -318,6 +374,13 @@ abstract class List<E> implements Iterable<E> { |
| * Copies the objects of [iterable], skipping [skipCount] objects first, |
| * into the range [start] inclusive to [end] exclusive of `this`. |
| * |
| + * List<int> list1 = [1, 2, 3, 4]; |
| + * List<int> list2 = [5, 6, 7, 8, 9]; |
| + * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items of |
| + * // list1 |
|
mem
2013/09/06 16:28:16
Orphan! Bad form to put one word on a line by itse
shailentuli
2013/09/23 11:48:54
Done.
|
| + * list1.setRange(1, 3, list2, 3); |
| + * list1.join(', '); // '1, 8, 9, 4' |
| + * |
| * If [start] equals [end] and [start]..[end] represents a legal range, this |
| * method has no effect. |
| * |
| @@ -325,12 +388,6 @@ abstract class List<E> implements Iterable<E> { |
| * An error occurs if the [iterable] does not have enough objects after |
| * skipping [skipCount] objects. |
| * |
| - * Example: |
| - * |
| - * var list = [1, 2, 3, 4]; |
| - * var list2 = [5, 6, 7, 8, 9]; |
| - * list.setRange(1, 3, list2, 3); |
| - * print(list); // => [1, 8, 9, 4] |
| */ |
| void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]); |
| @@ -354,13 +411,11 @@ abstract class List<E> implements Iterable<E> { |
| * Removes the objects in the range [start] inclusive to [end] exclusive |
| * and replaces them with the contents of the [iterable]. |
| * |
| - * An error occurs if [start]..[end] is not a valid range for `this`. |
| + * List<int> list = [1, 2, 3, 4]; |
| + * list.replaceRange(1, 3, [6, 7]); |
| + * list.join(', '); // '1, 6, 7, 4' |
| * |
| - * Example: |
| - * |
| - * var list = [1, 2, 3, 4, 5]; |
| - * list.replaceRange(1, 3, [6, 7, 8, 9]); |
| - * print(list); // [1, 6, 7, 8, 9, 4, 5] |
| + * An error occurs if [start]..[end] is not a valid range for `this`. |
| */ |
| void replaceRange(int start, int end, Iterable<E> iterable); |
| @@ -370,6 +425,11 @@ abstract class List<E> implements Iterable<E> { |
| * The map uses the indices of this list as keys and the corresponding objects |
| * as values. The `Map.keys` [Iterable] iterates the indices of this list |
| * in numerical order. |
| + * |
| + * List<String> words = ['fee', 'fi', 'fo', 'fum']; |
| + * Map<int, String> map = words.asMap(); |
| + * map[0] + map[1]; // 'feefi'; |
| + * map.keys.toList(); // [0, 1, 2, 3] |
| */ |
| Map<int, E> asMap(); |
| } |