SequenceCollection<E> abstract class
A skeleton class for a Collection that is also a Sequence.
abstract class SequenceCollection<E> implements Collection<E>, Sequence<E> { // The class is intended for use as a mixin as well. Iterator<E> iterator() => new SequenceIterator(sequence); void forEach(f(E element)) { for (int i = 0; i < this.length; i++) f(this[i]); } Collection map(f(E element)) { List result = new List(); for (int i = 0; i < this.length; i++) { result.add(f(this[i])); } return result; } bool contains(E value) { for (int i = 0; i < sequence.length; i++) { if (sequence[i] == value) return true; } return false; } reduce(initialValue, combine(previousValue, E element)) { var value = initialValue; for (int i = 0; i < this.length; i++) { value = combine(value, this[i]); } return value; } Collection<E> filter(bool f(E element)) { List<E> result = <E>[]; for (int i = 0; i < this.length; i++) { E element = this[i]; if (f(element)) result.add(element); } return result; } bool every(bool f(E element)) { for (int i = 0; i < this.length; i++) { if (!f(this[i])) return false; } return true; } bool some(bool f(E element)) { for (int i = 0; i < this.length; i++) { if (f(this[i])) return true; } return false; } bool isEmpty() { return this.length == 0; } }
Subclasses
Implements
Properties
Operators
Methods
bool contains(E value) #
Check whether the collection contains an element equal to element
.
bool contains(E value) { for (int i = 0; i < sequence.length; i++) { if (sequence[i] == value) return true; } return false; }
bool every(bool f(E element)) #
Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.
bool every(bool f(E element)) { for (int i = 0; i < this.length; i++) { if (!f(this[i])) return false; } return true; }
Collection<E> filter(bool f(E element)) #
Returns a collection with the elements of this collection that satisfy the predicate f.
The returned collection should be of the same type as the collection creating it.
An element satisfies the predicate
f if f(element)
returns true.
Collection<E> filter(bool f(E element)) { List<E> result = <E>[]; for (int i = 0; i < this.length; i++) { E element = this[i]; if (f(element)) result.add(element); } return result; }
void forEach(f(E element)) #
Applies the function f to each element of this collection.
void forEach(f(E element)) { for (int i = 0; i < this.length; i++) f(this[i]); }
bool isEmpty() #
Returns true if there is no element in this collection.
bool isEmpty() { return this.length == 0; }
Collection map(f(E element)) #
Returns a new collection with the elements f(e)
for each element e
of this collection.
Subclasses of Collection should implement the map method
to return a collection of the same general type as themselves.
E.g., List.map
should return a List.
Collection map(f(E element)) { List result = new List(); for (int i = 0; i < this.length; i++) { result.add(f(this[i])); } return result; }
reduce(initialValue, combine(previousValue, E element)) #
Reduce a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function. Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of a collection:
collection.reduce(0, (prev, element) => prev + element);
reduce(initialValue, combine(previousValue, E element)) { var value = initialValue; for (int i = 0; i < this.length; i++) { value = combine(value, this[i]); } return value; }