NumberEnumerable<T extends num> class
Extends
Enumerable<T> > NumberEnumerable<T>
Constructors
const NumberEnumerable() #
const NumberEnumerable() : super();
factory NumberEnumerable.from(Iterable<T> source) #
factory NumberEnumerable.from(Iterable<T> source) { requireArgumentNotNull(source, 'source'); return new _SimpleNumEnumerable<T>(source); }
factory NumberEnumerable.fromRange(int start, int count) #
factory NumberEnumerable.fromRange(int start, int count) { return new _RangeEnumerable(start, count); }
Methods
bool operator ==(other) #
The equality operator.
The default behavior for all Object
s 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);
Object aggregate(Object seed, Func2 f) #
Object aggregate(Object seed, Func2<Object, T, Object> f) { requireArgumentNotNull(f, 'f'); return CollectionUtil.aggregate(this, seed, f); }
num average() #
num average() { int theCount = 0; num theSum = 0; for(final n in this) { theSum += n; theCount++; } return theSum / theCount; }
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)); }
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.
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.
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() #
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.
bool isEmpty() => !some((e) => true);
abstract Iterator iterator() #
Returns an Iterator
that iterates over this Iterable
object.
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(); }
int get length() #
Returns the number of elements in this collection.
int get length;
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)); }
num max() #
num max() { num theMax = null; for(final n in this) { theMax = theMax == null ? n : math.max(theMax, n); } return theMax; }
num min() #
num min() { num theMin = null; for(final n in this) { theMin = theMin == null ? n : math.min(theMin, n); } return theMin; }
noSuchMethod(String name, List args) #
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);
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); }
Type get runtimeType() #
A representation of the runtime type of the object.
external Type get runtimeType;
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; }
num sum() #
num sum() { num theSum = 0; for(final n in this) { theSum += n; } return theSum; }
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)); } }
ReadOnlyCollection<T> toReadOnlyCollection() #
ReadOnlyCollection<T> toReadOnlyCollection() => new ReadOnlyCollection<T>(this);
String toString() #
Returns a string representation of this object.
String toString() => "[${this.join()}]";