Dart DocumentationbotEnumerable<T>

Enumerable<T> abstract class

abstract class Enumerable<T> implements Collection<T> {
  // TODO:
  // last
  // take
  // takeWhile
  // skip
  // skipWhile
  // isEmpty

  const Enumerable();

  factory Enumerable.fromIterable(Iterable<T> source) {
    requireArgumentNotNull(source, 'source');
    return new _SimpleEnumerable<T>(source);
  }

  abstract Iterator iterator();

  /**
   * Returns true if every elements of this collection satisify the
   * predicate [f]. Returns false otherwise.
   */
  bool every(Func1<T, bool> f) {
    requireArgumentNotNull(f, 'f');
    for (final e in this) {
      if(!f(e)) {
        return false;
      }
    }
    return true;
  }

  bool contains(T item) {
    for (final e in this) {
      if(e == item) {
        return true;
      }
    }
    return false;
  }

  bool isEmpty() => !some((e) => true);

  /**
   * Returns true if one element of this collection satisfies the
   * predicate [f]. Returns false otherwise.
   */
  bool some(Func1<T, bool> f) {
    requireArgumentNotNull(f, 'f');
    for (final e in this) {
      if(f(e)) {
        return true;
      }
    }
    return false;
  }

  int count([Func1<T, bool> f = null]) {
    if(f == null) {
      f = (a) => true;
    }
    int c = 0;
    for(final e in this) {
      if(f(e)) {
        c++;
      }
    }
    return c;
  }

  String join([String separator = ', ']) {
    final StringBuffer sb = new StringBuffer();
    for(final e in this) {
      if(sb.length > 0) {
        sb.add(separator);
      }
      sb.add(e);
    }
    return sb.toString();
  }

  /**
   * Returns a new collection with the elements [: f(e) :]
   * for each element [:e:] of this collection.
   *
   * Note on typing: the return type of f() could be an arbitrary
   * type and consequently the returned collection's
   * typeis Collection.
   */
  Enumerable map(Func1<T, Object> f) {
    requireArgumentNotNull(f, 'f');
    return new _FuncEnumerable(this, (s) => new _SelectIterator<T, Object>(s, f));
  }

  Enumerable<T> filter(Func1<T, bool> f) {
    requireArgumentNotNull(f, 'f');
    return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f));
  }

  /**
   * 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);
   */
  Dynamic reduce(Dynamic initialValue,
                 Dynamic combine(Dynamic previousValue, T element)) {
    return Collections.reduce(this, initialValue, combine);
  }

  Enumerable<T> exclude(Iterable<T> items) {
    requireArgumentNotNull(items, 'items');
    final iEnum = $(items);
    Func1<T, bool> f = (e) => !iEnum.contains(e);
    return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f));
  }

  Enumerable selectMany(Func1<T, Iterable> f) {
    requireArgumentNotNull(f, 'f');
    return new _FuncEnumerable(this,
      (s) => new _SelectManyIterator._internal(s, f));
  }

  T first([Func1<T, bool> f = null]) {
    if(f == null) {
      f = (e) => true;
    }
    final iter = new _WhereIterator<T>(this.iterator(), f);
    if(!iter.hasNext()) {
      throw const InvalidOperationException('The input sequence is empty.');
    }
    return iter.next();
  }

  T firstOrDefault([Func1<T, bool> f = null, T defaultValue = null]) {
    if(f == null) {
      f = (e) => true;
    }
    final iter = new _WhereIterator<T>(this.iterator(), f);
    if(!iter.hasNext()) {
      return defaultValue;
    }
    return iter.next();
  }

  T single([Func1<T, bool> f = null]) {
    if(f == null) {
      f = (e) => true;
    }
    final iter = new _WhereIterator<T>(this.iterator(), f);
    if(!iter.hasNext()) {
      throw const InvalidOperationException('The input sequence is empty.');
    }
    final value = iter.next();
    if(iter.hasNext()) {
      throw const InvalidOperationException('The input sequence contains more than one element.');
    }
    return value;
  }

  T singleOrDefault([Func1<T, bool> f = null, T defaultValue = null]) {
    if(f == null) {
      f = (e) => true;
    }
    final iter = new _WhereIterator<T>(this.iterator(), f);
    if(!iter.hasNext()) {
      return defaultValue;
    }
    final value = iter.next();
    if(iter.hasNext()) {
      throw const InvalidOperationException('The input sequence contains more than one element.');
    }
    return value;
  }

  Enumerable<T> distinct([Func2<T, T, bool> comparer = null]) {
    if(comparer == null) {
      comparer = (a,b) => a == b;
    }
    return new _FuncEnumerable(this, (s) => new _DistinctIterator(s, comparer));
  }

  Object aggregate(Object seed, Func2<Object, T, Object> f) {
    requireArgumentNotNull(f, 'f');
    return CollectionUtil.aggregate(this, seed, f);
  }

  Grouping<Dynamic, T> group([Func1<T, Object> keyFunc = null]) {
    return new Grouping(this, keyFunc);
  }

  ReadOnlyCollection<T> toReadOnlyCollection() => new ReadOnlyCollection<T>(this);

  void forEach(Action1<T> f) {
    for(final e in this) {
      f(e);
    }
  }

  void forEachWithIndex(Action2<T, int> f) {
    int i = 0;
    for(final e in this) {
      f(e, i++);
    }
  }

  List<T> toList() => new List<T>.from(this);

  HashSet toHashSet([Func1<T, Hashable> f]) {
    if(f == null) {
      return new HashSet.from(this);
    } else {
      return new HashSet.from(this.map(f));
    }
  }

  HashMap toHashMap(Func1<T, Object> valueFunc, [Func1<T, Hashable> keyFunc]) {
    if(keyFunc == null) {
      keyFunc = (a) => a;
    }

    final map = new HashMap();
    for(final e in this) {
      final k = keyFunc(e);
      if(map.containsKey(k)) {
        throw new UnsupportedOperationException("The key '$k' is duplicated");
      }
      map[k] = valueFunc(e);
    }
    return map;
  }

  NumberEnumerable selectNumbers(Func1<T, num> f) {
    requireArgumentNotNull(f, 'f');
    return new _FuncNumEnumerable<T>(this, (s) => new _SelectIterator<T, num>(s, f));
  }

  String toString() => "[${this.join()}]";
}

Subclasses

ListBase<T>, NumberEnumerable<T>

Implements

Collection<E>

Constructors

const Enumerable() #

const Enumerable();

factory Enumerable.fromIterable(Iterable<T> source) #

factory Enumerable.fromIterable(Iterable<T> source) {
  requireArgumentNotNull(source, 'source');
  return new _SimpleEnumerable<T>(source);
}

Properties

final int length #

inherited from Collection

Returns the number of elements in this collection.

int get length;

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

Operators

bool operator ==(other) #

inherited from Object

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

bool operator ==(other) => identical(this, other);

Methods

Object aggregate(Object seed, Func2 f) #

Object aggregate(Object seed, Func2<Object, T, Object> f) {
  requireArgumentNotNull(f, 'f');
  return CollectionUtil.aggregate(this, seed, f);
}

bool contains(T item) #

bool contains(T item) {
  for (final e in this) {
    if(e == item) {
      return true;
    }
  }
  return false;
}

int count([Func1 f = null]) #

int count([Func1<T, bool> f = null]) {
  if(f == null) {
    f = (a) => true;
  }
  int c = 0;
  for(final e in this) {
    if(f(e)) {
      c++;
    }
  }
  return c;
}

Enumerable<T> distinct([Func2 comparer = null]) #

Enumerable<T> distinct([Func2<T, T, bool> comparer = null]) {
  if(comparer == null) {
    comparer = (a,b) => a == b;
  }
  return new _FuncEnumerable(this, (s) => new _DistinctIterator(s, comparer));
}

const Enumerable() #

const Enumerable();

factory Enumerable.fromIterable(Iterable<T> source) #

factory Enumerable.fromIterable(Iterable<T> source) {
  requireArgumentNotNull(source, 'source');
  return new _SimpleEnumerable<T>(source);
}

bool every(Func1 f) #

Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.

bool every(Func1<T, bool> f) {
  requireArgumentNotNull(f, 'f');
  for (final e in this) {
    if(!f(e)) {
      return false;
    }
  }
  return true;
}

Enumerable<T> exclude(Iterable<T> items) #

Enumerable<T> exclude(Iterable<T> items) {
  requireArgumentNotNull(items, 'items');
  final iEnum = $(items);
  Func1<T, bool> f = (e) => !iEnum.contains(e);
  return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f));
}

Enumerable<T> filter(Func1 f) #

Returns a new collection with the elements of this collection that satisfy the predicate f.

An element satisfies the predicate f if f(element) returns true.

docs inherited from Collection<E>
Enumerable<T> filter(Func1<T, bool> f) {
  requireArgumentNotNull(f, 'f');
  return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f));
}

T first([Func1 f = null]) #

T first([Func1<T, bool> f = null]) {
  if(f == null) {
    f = (e) => true;
  }
  final iter = new _WhereIterator<T>(this.iterator(), f);
  if(!iter.hasNext()) {
    throw const InvalidOperationException('The input sequence is empty.');
  }
  return iter.next();
}

T firstOrDefault([Func1 f = null, T defaultValue = null]) #

T firstOrDefault([Func1<T, bool> f = null, T defaultValue = null]) {
  if(f == null) {
    f = (e) => true;
  }
  final iter = new _WhereIterator<T>(this.iterator(), f);
  if(!iter.hasNext()) {
    return defaultValue;
  }
  return iter.next();
}

void forEach(Action1 f) #

Applies the function f to each element of this collection.

docs inherited from Collection<E>
void forEach(Action1<T> f) {
  for(final e in this) {
    f(e);
  }
}

void forEachWithIndex(Action2 f) #

void forEachWithIndex(Action2<T, int> f) {
  int i = 0;
  for(final e in this) {
    f(e, i++);
  }
}

Grouping<Dynamic, T> group([Func1 keyFunc = null]) #

Grouping<Dynamic, T> group([Func1<T, Object> keyFunc = null]) {
  return new Grouping(this, keyFunc);
}

int hashCode() #

inherited from Object

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

external int hashCode();

bool isEmpty() #

Returns true if there is no element in this collection.

docs inherited from Collection<E>
bool isEmpty() => !some((e) => true);

abstract Iterator iterator() #

Returns an Iterator that iterates over this Iterable object.

docs inherited from Iterable<E>

String join([String separator = ', ']) #

String join([String separator = ', ']) {
  final StringBuffer sb = new StringBuffer();
  for(final e in this) {
    if(sb.length > 0) {
      sb.add(separator);
    }
    sb.add(e);
  }
  return sb.toString();
}

Enumerable map(Func1 f) #

Returns a new collection with the elements f(e) for each element e of this collection.

Note on typing: the return type of f() could be an arbitrary type and consequently the returned collection's typeis Collection.

Enumerable map(Func1<T, Object> f) {
  requireArgumentNotNull(f, 'f');
  return new _FuncEnumerable(this, (s) => new _SelectIterator<T, Object>(s, f));
}

noSuchMethod(String name, List args) #

inherited from Object

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

external Dynamic noSuchMethod(String name, List args);

const Object() #

inherited from Object

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

const Object();

reduce(initialValue, combine(previousValue, T 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);

Dynamic reduce(Dynamic initialValue,
               Dynamic combine(Dynamic previousValue, T element)) {
  return Collections.reduce(this, initialValue, combine);
}

Enumerable selectMany(Func1 f) #

Enumerable selectMany(Func1<T, Iterable> f) {
  requireArgumentNotNull(f, 'f');
  return new _FuncEnumerable(this,
    (s) => new _SelectManyIterator._internal(s, f));
}

NumberEnumerable selectNumbers(Func1 f) #

NumberEnumerable selectNumbers(Func1<T, num> f) {
  requireArgumentNotNull(f, 'f');
  return new _FuncNumEnumerable<T>(this, (s) => new _SelectIterator<T, num>(s, f));
}

T single([Func1 f = null]) #

T single([Func1<T, bool> f = null]) {
  if(f == null) {
    f = (e) => true;
  }
  final iter = new _WhereIterator<T>(this.iterator(), f);
  if(!iter.hasNext()) {
    throw const InvalidOperationException('The input sequence is empty.');
  }
  final value = iter.next();
  if(iter.hasNext()) {
    throw const InvalidOperationException('The input sequence contains more than one element.');
  }
  return value;
}

T singleOrDefault([Func1 f = null, T defaultValue = null]) #

T singleOrDefault([Func1<T, bool> f = null, T defaultValue = null]) {
  if(f == null) {
    f = (e) => true;
  }
  final iter = new _WhereIterator<T>(this.iterator(), f);
  if(!iter.hasNext()) {
    return defaultValue;
  }
  final value = iter.next();
  if(iter.hasNext()) {
    throw const InvalidOperationException('The input sequence contains more than one element.');
  }
  return value;
}

bool some(Func1 f) #

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

bool some(Func1<T, bool> f) {
  requireArgumentNotNull(f, 'f');
  for (final e in this) {
    if(f(e)) {
      return true;
    }
  }
  return false;
}

HashMap toHashMap(Func1 valueFunc, [Func1 keyFunc]) #

HashMap toHashMap(Func1<T, Object> valueFunc, [Func1<T, Hashable> keyFunc]) {
  if(keyFunc == null) {
    keyFunc = (a) => a;
  }

  final map = new HashMap();
  for(final e in this) {
    final k = keyFunc(e);
    if(map.containsKey(k)) {
      throw new UnsupportedOperationException("The key '$k' is duplicated");
    }
    map[k] = valueFunc(e);
  }
  return map;
}

HashSet toHashSet([Func1 f]) #

HashSet toHashSet([Func1<T, Hashable> f]) {
  if(f == null) {
    return new HashSet.from(this);
  } else {
    return new HashSet.from(this.map(f));
  }
}

List<T> toList() #

List<T> toList() => new List<T>.from(this);

ReadOnlyCollection<T> toReadOnlyCollection() #

ReadOnlyCollection<T> toReadOnlyCollection() => new ReadOnlyCollection<T>(this);

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => "[${this.join()}]";