Dart API Referencedart:coreSequenceCollection<E>

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

SequenceList<E>

Implements

Sequence<T>, Collection<E>

Properties

final int length #

inherited from Sequence

The limit of valid indices of the sequence.

The length getter should be efficient.

int get length;

Operators

T operator [](int index) #

inherited from Sequence

Returns the value at the given index.

Valid indices must be in the range 0..length - 1. The lookup operator should be efficient.

T operator[](int index);

Methods

bool contains(E value) #

Check whether the collection contains an element equal to element.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
bool isEmpty() {
  return this.length == 0;
}

Iterator<E> iterator() #

Returns an Iterator that iterates over this Iterable object.

docs inherited from Iterable<E>
Iterator<E> iterator() => new SequenceIterator(sequence);

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.

docs inherited from Collection<E>
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);

docs inherited from Collection<E>
reduce(initialValue, combine(previousValue, E element)) {
  var value = initialValue;
  for (int i = 0; i < this.length; i++) {
    value = combine(value, this[i]);
  }
  return value;
}

bool some(bool f(E element)) #

Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.

docs inherited from Collection<E>
bool some(bool f(E element)) {
  for (int i = 0; i < this.length; i++) {
    if (f(this[i])) return true;
  }
  return false;
}