Finite<A> class
class Finite<A> extends IterableBase<A> { final int length; final _Operation op; Finite._(this.length, this.op); factory Finite.empty() => new Finite._(0, new _OEmpty()); factory Finite.singleton(A x) => new Finite._(1, new _OSingleton(x)); /** * Union. */ Finite<A> operator +(Finite<A> fin) => new Finite._(this.length + fin.length, new _OAdd(this, fin)); /** * Cartesian product. */ Finite<Pair> operator *(Finite fin) => new Finite._(this.length * fin.length, new _OMult(this, fin)); /** * [Finite] is a functor. */ Finite map(f(A x)) => new Finite._(this.length, new _OMap(this, f)); /** * [Finite] is an applicative functor. */ Finite apply(Finite fin) => (this * fin).map((pair) => (pair.fst as Function)(pair.snd)); A operator [](int index) => _eval(this, index); static _eval(Finite finite, int index) { bool evalOp = true; var val; final stack = <_Instruction>[new _IDone()]; var op = finite.op; while (true) { if (evalOp) { switch(op.tag) { case _Operation.ADD: if (index < op.fin1.length) { op = op.fin1.op; } else { final f1 = op.fin1; op = op.fin2.op; index = index - f1.length; } break; case _Operation.EMPTY: throw new RangeError(index); break; case _Operation.MULT: int q = index ~/ op.fin2.length; int r = index % op.fin2.length; index = q; stack.add(new _IPair1(op.fin2, r)); op = op.fin1.op; break; case _Operation.SINGLETON: if (index == 0) { val = op.val; evalOp = false; } else { throw new RangeError(index); } break; case _Operation.MAP: stack.add(new _IMap(op.fun)); op = op.fin.op; break; } } else { final instr = stack.removeLast(); switch (instr.tag) { case _Instruction.DONE: return val; case _Instruction.MAP: val = instr.fun(val); break; case _Instruction.PAIR2: val = new Pair(instr.snd, val); break; case _Instruction.PAIR1: op = instr.fin2.op; index = instr.r; stack.add(new _IPair2(val)); evalOp = true; break; } } } } String toString() { final strings = toLazyList().map((f) => f.toString()).toList(); return "{${strings.join(", ")}}"; } Iterator<A> get iterator => new _FiniteIterator<A>(this); LazyList<A> toLazyList() { aux(i) => (i == this.length) ? new LazyList.empty() : new LazyList.cons(this[i], () => aux(i+1)); return aux(0); } // optimized Iterable methods bool get isEmpty => length == 0; A elementAt(int index) => this[index]; A get first { if (isEmpty) { throw new StateError('Cannot access first element of an empty set.'); } return this[0]; } A get last { if (isEmpty) { throw new StateError('Cannot access last element of an empty set.'); } return this[length - 1]; } }
Extends
IterableBase<A> > Finite<A>
Constructors
factory Finite.empty() #
factory Finite.empty() => new Finite._(0, new _OEmpty());
Properties
final A first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
A get first { if (isEmpty) { throw new StateError('Cannot access first element of an empty set.'); } return this[0]; }
final bool isNotEmpty #
final A last #
A get last { if (isEmpty) { throw new StateError('Cannot access last element of an empty set.'); } return this[length - 1]; }
final _Operation op #
final _Operation op
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
E get single { Iterator it = iterator; if (!it.moveNext()) throw new StateError("No elements"); E result = it.current; if (it.moveNext()) throw new StateError("More than one element"); return result; }
Operators
Methods
bool any(bool f(E element)) #
Finite apply(Finite fin) #
Finite is an applicative functor.
Finite apply(Finite fin) => (this * fin).map((pair) => (pair.fst as Function)(pair.snd));
bool contains(Object element) #
A elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
A elementAt(int index) => this[index];
bool every(bool f(E element)) #
Iterable expand(Iterable f(E element)) #
Expands each element of this Iterable into zero or more elements.
The resulting Iterable runs through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and calls f for each element of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
dynamic firstWhere(bool test(E value), {Object orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic firstWhere(bool test(E value), { Object orElse() }) { for (E element in this) { if (test(element)) return element; } if (orElse != null) return orElse(); throw new StateError("No matching element"); }
dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces 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 an iterable:
iterable.fold(0, (prev, element) => prev + element);
dynamic fold(var initialValue, dynamic combine(var previousValue, E element)) { var value = initialValue; for (E element in this) value = combine(value, element); return value; }
void forEach(void f(E element)) #
String join([String separator = ""]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator = ""]) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) return ""; StringBuffer buffer = new StringBuffer(); if (separator == null || separator == "") { do { buffer.write("${iterator.current}"); } while (iterator.moveNext()); } else { buffer.write("${iterator.current}"); while (iterator.moveNext()) { buffer.write(separator); buffer.write("${iterator.current}"); } } return buffer.toString(); }
dynamic lastWhere(bool test(E value), {Object orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic lastWhere(bool test(E value), { Object orElse() }) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { result = element; foundMatching = true; } } if (foundMatching) return result; if (orElse != null) return orElse(); throw new StateError("No matching element"); }
Finite map(f(A x)) #
Finite is a functor.
Finite map(f(A x)) => new Finite._(this.length, new _OMap(this, f));
E reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
E reduce(E combine(E value, E element)) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) { throw new StateError("No elements"); } E value = iterator.current; while (iterator.moveNext()) { value = combine(value, iterator.current); } return value; }
E singleWhere(bool test(E value)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
E singleWhere(bool test(E value)) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { if (foundMatching) { throw new StateError("More than one matching element"); } result = element; foundMatching = true; } } if (foundMatching) return result; throw new StateError("No matching element"); }
Iterable<E> skip(int n) #
Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
Iterable<E> skipWhile(bool test(E value)) { return new SkipWhileIterable<E>(this, test); }
Iterable<E> take(int n) #
Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
Iterable<E> takeWhile(bool test(E value)) { return new TakeWhileIterable<E>(this, test); }
LazyList<A> toLazyList() #
LazyList<A> toLazyList() { aux(i) => (i == this.length) ? new LazyList.empty() : new LazyList.cons(this[i], () => aux(i+1)); return aux(0); }
List<E> toList({bool growable: true}) #
Set<E> toSet() #
String toString() #
Returns a string representation of this object.
String toString() { final strings = toLazyList().map((f) => f.toString()).toList(); return "{${strings.join(", ")}}"; }
Iterable<E> where(bool f(E element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate test
.
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function test
will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the returned Iterable will invoke the supplied
function test
multiple times on the same element.
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);