Add example int doc for Iterable.expand

The expand function is not trivial and an example helps flesh out its
behaviour.

BUG=
R=floitsch@google.com

Review URL: https://codereview.chromium.org/2154933002 .
This commit is contained in:
Stan Manilov 2016-08-05 10:34:38 -07:00
parent fc42f03c13
commit 0648e2622b

View file

@ -181,6 +181,17 @@ abstract class Iterable<E> {
* *
* The returned [Iterable] is lazy, and calls [f] for each element * The returned [Iterable] is lazy, and calls [f] for each element
* of this every time it's iterated. * of this every time it's iterated.
*
* Example:
*
* var pairs = [[1, 2], [3, 4]];
* var flattened = pairs.expand((pair) => pair).toList();
* print(flattened); // => [1, 2, 3, 4];
*
* var input = [1, 2, 3];
* var duplicated = input.expand((i) => [i, i]).toList();
* print(duplicated); // => [1, 1, 2, 2, 3, 3]
*
*/ */
Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) =>
new ExpandIterable<E, dynamic/*=T*/>(this, f); new ExpandIterable<E, dynamic/*=T*/>(this, f);
@ -208,7 +219,6 @@ abstract class Iterable<E> {
return false; return false;
} }
/** /**
* Applies the function [f] to each element of this collection in iteration * Applies the function [f] to each element of this collection in iteration
* order. * order.
@ -316,7 +326,6 @@ abstract class Iterable<E> {
return buffer.toString(); return buffer.toString();
} }
/** /**
* Checks whether any element of this iterable satisfies [test]. * Checks whether any element of this iterable satisfies [test].
* *
@ -382,7 +391,7 @@ abstract class Iterable<E> {
*/ */
bool get isNotEmpty => !isEmpty; bool get isNotEmpty => !isEmpty;
/** /**
* Returns a lazy iterable of the [count] first elements of this iterable. * Returns a lazy iterable of the [count] first elements of this iterable.
* *
* The returned `Iterable` may contain fewer than `count` elements, if `this` * The returned `Iterable` may contain fewer than `count` elements, if `this`
@ -480,7 +489,7 @@ abstract class Iterable<E> {
E result; E result;
do { do {
result = it.current; result = it.current;
} while(it.moveNext()); } while (it.moveNext());
return result; return result;
} }
@ -506,7 +515,7 @@ abstract class Iterable<E> {
* function is returned. * function is returned.
* If [orElse] is omitted, it defaults to throwing a [StateError]. * If [orElse] is omitted, it defaults to throwing a [StateError].
*/ */
E firstWhere(bool test(E element), { E orElse() }) { E firstWhere(bool test(E element), {E orElse()}) {
for (E element in this) { for (E element in this) {
if (test(element)) return element; if (test(element)) return element;
} }