ILazyList<A> final
An immutable, lazily-evaluated (potentially infinite) sequence.
Elements are computed on demand and memoized after first evaluation. Operations like map, filter, take, and drop return new lazy lists without forcing evaluation. Operations marked "Will force evaluation" in their doc comment traverse the whole list eagerly.
Construct with ILazyList.from, ILazyList.fill, ILazyList.tabulate, ILazyList.continually, ILazyList.ints, ILazyList.iterate, or ILazyList.unfold. Use ILazyList.builder when building incrementally.
// infinite sequence of natural numbers
final nats = ILazyList.ints(0);
nats.take(5).toIList(); // IList(0, 1, 2, 3, 4)Mixed-in types
Available Extensions
Properties
hashCode no setter override
The hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Implementation
@override
int get hashCode => identityHashCode(this);head no setter override
Returns the first element of this collection, or throws if it is empty.
Implementation
@override
A get head => _state.head;headOption no setter inherited
Returns the first element of this collection as a Some if non-empty. If this collction is empty, None is returned.
Inherited from RIterable.
Implementation
Option<A> get headOption {
final it = iterator;
return Option.when(() => it.hasNext, () => it.next());
}init no setter override
Will force evaluation
Implementation
@override
ILazyList<A> get init => ILazyList.from(super.init);inits no setter override
Will force evaluation
Implementation
@override
RIterator<ILazyList<A>> get inits => super.inits.map(ILazyList.from);isEmpty no setter override
Whether this collection contains no elements.
Implementation
@override
bool get isEmpty => _state is _Empty;isNotEmpty no setter inherited
Whether this collection contains at least one element.
Inherited from RIterableOnce.
Implementation
bool get isNotEmpty => !isEmpty;isTraversableAgain no setter inherited
Whether this collection can be traversed more than once.
Always false for a bare RIterableOnce; overridden to true by RIterable and its subtypes.
Inherited from RIterableOnce.
Implementation
bool get isTraversableAgain => false;iterator no setter override
Returns an RIterator over the elements of this collection.
Implementation
@override
RIterator<A> get iterator => knownIsEmpty ? RIterator.empty() : _LazyIterator(this);knownIsEmpty no setter
true if the list has already been evaluated and is empty.
Unlike isEmpty, this does not force evaluation.
Implementation
bool get knownIsEmpty => _stateEvaluated && isEmpty;knownNonEmpty no setter
true if the list has already been evaluated and is non-empty.
Unlike nonEmpty, this does not force evaluation.
Implementation
bool get knownNonEmpty => _stateEvaluated && !isEmpty;knownSize no setter override
Returns the number of elements in this collection, if that number is already known. If not, -1 is returned.
Implementation
@override
int get knownSize => knownIsEmpty ? 0 : -1;last no setter inherited
Returns the last element of this collection, or throws if it is empty.
Inherited from RIterable.
Implementation
A get last {
final it = iterator;
var lst = it.next();
while (it.hasNext) {
lst = it.next();
}
return lst;
}lastOption no setter inherited
Returns the last element of this collection as a Some, or None if this collection is empty.
Inherited from RIterable.
Implementation
Option<A> get lastOption {
if (isEmpty) {
return none();
} else {
final it = iterator;
var last = it.next();
while (it.hasNext) {
last = it.next();
}
return Some(last);
}
}length no setter override
The number of elements in this sequence.
Implementation
@override
int get length {
var these = this;
var len = 0;
while (these.nonEmpty) {
len += 1;
these = these.tail;
}
return len;
}nonEmpty no setter inherited
Whether this collection contains at least one element.
Inherited from RIterableOnce.
Implementation
bool get nonEmpty => !isEmpty;runtimeType no setter inherited
A representation of the runtime type of the object.
Inherited from Object.
Implementation
external Type get runtimeType;size no setter inherited
Returns the number of elements in this collection.
Inherited from RSeq.
Implementation
@override
int get size => length;tail no setter override
Returns a new collection with the first element removed. If this collection is empty, an empty collection is returned.
Implementation
@override
ILazyList<A> get tail => _state.tail;tails no setter override
Returns an iterator of all potential tails of this collection, starting with the entire collection and ending with an empty one.
Implementation
@override
RIterator<ILazyList<A>> get tails => ILazyList.iterate(
() => this,
(ll) => ll.nonEmpty ? ll.tail : ll,
).iterator.takeWhile((ll) => ll.nonEmpty).concat(RIterator.single(ILazyList.empty()));Methods
addStringNoForce()
Implementation
StringBuffer addStringNoForce(
StringBuffer b,
String start,
String sep,
String end,
) {
b.write(start);
if (!_stateEvaluated) {
b.write('<not computed>');
} else if (!isEmpty) {
b.write(head);
var cursor = this;
void appendHead(ILazyList<A> c) =>
b
..write(sep)
..write(c.head);
var scout = tail;
if (!identical(cursor, scout)) {
cursor = scout;
if (scout.knownNonEmpty) {
scout = scout.tail;
// Use 2x 1x iterator trick for cycle detection; slow iterator can add strings
while (!identical(cursor, scout) && scout.knownNonEmpty) {
appendHead(cursor);
cursor = cursor.tail;
scout = scout.tail;
if (scout.knownNonEmpty) scout = scout.tail;
}
}
}
if (!scout.knownNonEmpty) {
// Not a cycle, scout hit an end (empty or non-evaluated)
while (!identical(cursor, scout)) {
appendHead(cursor);
cursor = cursor.tail;
}
// if cursor (eq scout) has state defined, it is empty; else unknown state
if (!cursor._stateEvaluated) {
b
..write(sep)
..write('<not computed>');
}
} else {
if (!identical(cursor, this)) {
var runner = this;
while (!identical(runner, scout)) {
runner = runner.tail;
scout = scout.tail;
}
do {
final ct = cursor.tail;
if (!identical(ct, scout)) appendHead(cursor);
cursor = ct;
} while (!identical(cursor, scout));
}
b
..write(sep)
..write('<cycle>');
}
}
return b..write(end);
}appended() override
Returns a new Seq, with the given elem added to the end.
Implementation
@override
ILazyList<A> appended(A elem) {
if (knownIsEmpty) {
return _newLL(() => _sCons(elem, ILazyList.empty()));
} else {
return lazyAppendedAll(() => RIterator.single(elem));
}
}appendedAll() override
Returns a new Seq, with elems added to the end.
Implementation
@override
ILazyList<A> appendedAll(RIterableOnce<A> suffix) {
if (knownIsEmpty) {
return ILazyList.from(suffix);
} else {
return lazyAppendedAll(() => suffix);
}
}canEqual() inherited
Inherited from RSeq.
Implementation
bool canEqual(Object other) => true;collect() override
Returns a new collection by applying f to each element an only keeping results of type Some.
Implementation
@override
ILazyList<B> collect<B>(Function1<A, Option<B>> f) =>
knownIsEmpty ? empty() : _collectImpl(this, f);collectFirst() override
Applies f to each element of this collection, returning the first element that results in a Some, if any.
Implementation
@override
Option<B> collectFirst<B>(Function1<A, Option<B>> f) {
if (isEmpty) {
return none();
} else {
var res = f(head);
var rest = tail;
while (res.isEmpty && !rest.isEmpty) {
res = f(rest.head);
rest = rest.tail;
}
return res;
}
}combinations() override
Will force evaluation
Implementation
@override
RIterator<ILazyList<A>> combinations(int n) => super.combinations(n).map(ILazyList.from);concat() override
Returns a copy of this collection, with elems added to the end.
Implementation
@override
ILazyList<A> concat(RIterableOnce<A> suffix) => appendedAll(suffix);contains() inherited
Returns true, if any element of this collection equals elem.
Inherited from RSeq.
Implementation
bool contains(A elem) => exists((a) => a == elem);containsSlice() inherited
Returns true if that is contained in this collection, in order.
Inherited from RSeq.
Implementation
bool containsSlice(RSeq<A> that) => indexOfSlice(that).isDefined;copyToArray() inherited
Copies elements into xs starting at start, writing at most n elements (or all remaining capacity when n is omitted).
Returns the number of elements actually copied.
Inherited from RIterableOnce.
Implementation
int copyToArray(Array<A> xs, [int start = 0, int? n]) {
final it = iterator;
final end = start + min(n ?? Integer.maxValue, xs.length - start);
var i = start;
while (i < end && it.hasNext) {
xs[i] = it.next();
i += 1;
}
return i - start;
}corresponds() inherited
Returns true if this collection has the same size as that and each corresponding element from this and that satisfies the given predicate p.
Inherited from RSeq.
Implementation
@override
bool corresponds<B>(RSeq<B> that, Function2<A, B, bool> p) {
final i = iterator;
final j = that.iterator;
while (i.hasNext && j.hasNext) {
if (!p(i.next(), j.next())) return false;
}
return !i.hasNext && !j.hasNext;
}count() inherited
Return the number of elements in this collection that satisfy the given predicate.
Inherited from RIterableOnce.
Implementation
int count(Function1<A, bool> p) {
var res = 0;
final it = iterator;
while (it.hasNext) {
if (p(it.next())) res += 1;
}
return res;
}diff() override
Returns a new collection with the difference of this and that, i.e. all elements that appear in only this collection.
Implementation
@override
ILazyList<A> diff(RSeq<A> that) {
// Re-implemented to preserve laziness
final occ = <A, int>{};
that.foreach((y) => occ.update(y, (value) => value + 1, ifAbsent: () => 1));
final it = iterator.filter((key) {
var include = false;
if (occ.containsKey(key)) {
final value = occ[key]!;
if (value == 1) {
occ.remove(key);
} else {
occ[key] = value - 1;
}
} else {
include = true;
occ.remove(key);
}
return include;
});
return ILazyList.from(it);
}distinct() override
Returns a new collection where every element is distinct according to equality.
Implementation
@override
ILazyList<A> distinct() => distinctBy(identity);distinctBy() override
Returns a new collection where every element is distinct according to the application of f to each element.
Implementation
@override
ILazyList<A> distinctBy<B>(Function1<A, B> f) =>
_newLL(() => _stateFromIterator(iterator.distinctBy(f)));drop() override
Returns a new collection with the first n elements removed.
Implementation
@override
ILazyList<A> drop(int n) {
if (n <= 0) {
return this;
} else if (knownIsEmpty) {
return empty();
} else {
return _dropImpl(this, n);
}
}dropRight() override
Return a new collection with the last n elements removed.
Implementation
@override
ILazyList<A> dropRight(int n) {
if (n <= 0) {
return this;
} else if (knownIsEmpty) {
return empty();
} else {
return _newLL(() {
var scout = this;
var remaining = n;
// advance scout n elements ahead (or until empty)
while (remaining > 0 && !scout.isEmpty) {
remaining -= 1;
scout = scout.tail;
}
return _dropRightState(scout);
});
}
}dropWhile() override
Returns a new collection with leading elements satisfying p removed.
Implementation
@override
ILazyList<A> dropWhile(Function1<A, bool> p) {
if (knownIsEmpty) {
return empty();
} else {
return _dropWhileImpl(this, p);
}
}endsWith() inherited
Returns true if the end of this collection has the same elements in order as that. Otherwise, false is returned.
Inherited from RSeq.
Implementation
bool endsWith(RIterable<A> that) {
if (that.isEmpty) {
return true;
} else {
final i = iterator.drop(length - that.size);
final j = that.iterator;
while (i.hasNext && j.hasNext) {
if (i.next() != j.next()) return false;
}
return !j.hasNext;
}
}exists() inherited
Returns true if any element of this collection satisfies the given predicate, false if no elements satisfy it.
Inherited from RIterableOnce.
Implementation
bool exists(Function1<A, bool> p) {
var res = false;
final it = iterator;
while (!res && it.hasNext) {
res = p(it.next());
}
return res;
}filter() override
Returns a new collection containing only elements that satisfy p.
Implementation
@override
ILazyList<A> filter(Function1<A, bool> p) => knownIsEmpty ? empty() : _filterImpl(this, p, false);filterNot() override
Returns a new collection containing only elements that do not satisfy p.
Implementation
@override
ILazyList<A> filterNot(Function1<A, bool> p) =>
knownIsEmpty ? empty() : _filterImpl(this, p, true);find() override
Returns the first element from this collection that satisfies the given predicate p. If no element satisfies p, None is returned.
Implementation
@override
Option<A> find(Function1<A, bool> p) {
if (isEmpty) {
return none();
} else {
var rest = this;
while (!rest.isEmpty) {
if (p(rest.head)) return Some(rest.head);
rest = rest.tail;
}
return none();
}
}findLast() inherited
Returns the last element satisfying p as Some, or None if none.
Inherited from RSeq.
Implementation
Option<A> findLast(Function1<A, bool> p) {
final it = reverseIterator();
while (it.hasNext) {
final elem = it.next();
if (p(elem)) return Some(elem);
}
return none();
}flatMap() override
Returns a new collection by applying f to each element and concatenating the results.
Implementation
@override
ILazyList<B> flatMap<B>(Function1<A, RIterableOnce<B>> f) =>
knownIsEmpty ? empty() : _flatMapImpl(this, f);fold() inherited
Alias for foldLeft with a same-type accumulator.
Inherited from RIterable.
Implementation
A fold(A init, Function2<A, A, A> op) => foldLeft(init, op);foldLeft() override
Returns a summary value by applying op to all elements of this collection, moving from left to right. The fold uses a seed value of z.
Implementation
@override
B foldLeft<B>(B z, Function2<B, A, B> op) {
var result = z;
var these = this;
while (!these.isEmpty) {
result = op(result, these.head);
these = these.tail;
}
return result;
}foldRight() inherited
Returns a summary value by applying op to all elements of this collection, moving from right to left. The fold uses a seed value of z.
Inherited from RIterableOnce.
Implementation
B foldRight<B>(B z, Function2<A, B, B> op) => _reversed().foldLeft(z, (b, a) => op(a, b));forall() inherited
Returns true if all elements of this collection satisfy the given predicate, false if any elements do not.
Inherited from RIterableOnce.
Implementation
bool forall(Function1<A, bool> p) {
var res = true;
final it = iterator;
while (res && it.hasNext) {
res = p(it.next());
}
return res;
}force()
Forces evaluation of the entire list and returns this.
After calling force, all elements are memoized. Has no effect on an already-evaluated list.
Implementation
ILazyList<A> force() {
ILazyList<A> these = this;
ILazyList<A> those = this;
if (!these.isEmpty) {
these = these.tail;
}
while (those != these) {
if (these.isEmpty) return this;
these = these.tail;
if (these.isEmpty) return this;
these = these.tail;
if (these == those) return this;
those = those.tail;
}
return this;
}foreach() override
Applies f to each element of this collection, discarding any resulting values.
Implementation
@override
void foreach<U>(Function1<A, U> f) {
var these = this;
while (!these.isEmpty) {
f(these.head);
these = these.tail;
}
}groupBy() override
Will force evaluation
Implementation
@override
IMap<K, ILazyList<A>> groupBy<K>(Function1<A, K> f) => super.groupBy(f).mapValues(ILazyList.from);grouped() override
Returns a new iterator where each element is a collection of size elements from the original collection. The last element may contain less than size elements.
Implementation
@override
RIterator<ILazyList<A>> grouped(int size) => _slidingImpl(size, size);groupMap() override
Will force evaluation
Implementation
@override
IMap<K, ILazyList<B>> groupMap<K, B>(Function1<A, K> key, Function1<A, B> f) =>
super.groupMap(key, f).mapValues(ILazyList.from);groupMapReduce() inherited
Partitions all elements of this collection by applying key to each element. Additionally f is applied to each element to generate a value. If multiple values are generating for the same key, those values will be combined using reduce.
Inherited from RIterable.
Implementation
IMap<K, B> groupMapReduce<K, B>(
Function1<A, K> key,
Function1<A, B> f,
Function2<B, B, B> reduce,
) {
final m = <K, B>{};
foreach((elem) {
m.update(key(elem), (b) => reduce(b, f(elem)), ifAbsent: () => f(elem));
});
return IMap.fromDart(m);
}indexOf() inherited
Returns the first index, if any, where the element at that index equals elem. If no index contains elem, None is returned.
Inherited from RSeq.
Implementation
Option<int> indexOf(A elem, [int from = 0]) => indexWhere((a) => a == elem, from);indexOfSlice() inherited
Finds the first index in this collection where the next sequence of elements is equal to that. If that cannot be found in this collection, None is returned.
Inherited from RSeq.
Implementation
Option<int> indexOfSlice(RSeq<A> that, [int from = 0]) {
if (that.isEmpty && from == 0) {
return const Some(0);
} else {
final l = knownSize;
final tl = that.knownSize;
if (l >= 0 && tl >= 0) {
final clippedFrom = max(0, from);
if (from > l) {
return none();
} else if (tl < 1) {
return Some(clippedFrom);
} else if (l < tl) {
return none();
} else {
return _kmpSearch(this, clippedFrom, l, that, 0, tl, true);
}
} else {
var i = from;
var s = drop(i);
while (s.nonEmpty) {
if (s.startsWith(that)) return Some(i);
i += 1;
s = s.tail;
}
return none();
}
}
}indexWhere() inherited
Returns the index of the first element that satisfies the predicate p. If no element satisfies, None is returned.
Inherited from RSeq.
Implementation
Option<int> indexWhere(Function1<A, bool> p, [int from = 0]) => iterator.indexWhere(p, from);indices() inherited
Returns a range of all indices of this sequence
Will force evaluation.
Inherited from RSeq.
Implementation
Range indices() => Range.exclusive(0, length);intersect() override
Returns a new collection with the intersection of this and that, i.e. all elements that appear in both collections.
Implementation
@override
ILazyList<A> intersect(RSeq<A> that) {
// Re-implemented to preserve laziness
final occ = <A, int>{};
that.foreach((y) => occ.update(y, (value) => value + 1, ifAbsent: () => 1));
final it = iterator.filter((key) {
var include = true;
if (occ.containsKey(key)) {
final value = occ[key]!;
if (value == 1) {
occ.remove(key);
} else {
occ[key] = value - 1;
}
} else {
include = false;
occ.remove(key);
}
return include;
});
return ILazyList.from(it);
}intersperse() override
Returns a new collection with sep inserted between each element.
Implementation
@override
ILazyList<A> intersperse(A x) => knownIsEmpty ? this : _intersperseImpl(x, false);isDefinedAt() inherited
Returns true if this collection has an element at the given idx.
Inherited from RSeq.
Implementation
bool isDefinedAt(int idx) => 0 <= idx && idx < size;lastIndexOf() inherited
Returns the last index, if any, where the element at that index equals elem. If no index contains elem, None is returned.
Inherited from RSeq.
Implementation
Option<int> lastIndexOf(A elem, [int end = 2147483647]) => lastIndexWhere((a) => a == elem, end);lastIndexOfSlice() inherited
Finds the last index in this collection where the next sequence of elements is equal to that. If that cannot be found in this collection, None is returned.
Inherited from RSeq.
Implementation
Option<int> lastIndexOfSlice(RSeq<A> that, [int end = 2147483647]) {
final l = length;
final tl = that.length;
final clippedL = min(l - tl, end);
if (end < 0) {
return none();
} else if (tl < 1) {
return Some(clippedL);
} else if (l < tl) {
return none();
} else {
return _kmpSearch(this, 0, clippedL + tl, that, 0, tl, false);
}
}lastIndexWhere() inherited
Returns the index of the last element that satisfies the predicate p. If no element satisfies, None is returned.
Inherited from RSeq.
Implementation
Option<int> lastIndexWhere(Function1<A, bool> p, [int end = 2147483647]) {
var i = length - 1;
final it = reverseIterator();
while (it.hasNext) {
final elem = it.next();
if (i < end && p(elem)) return Some(i);
i -= 1;
}
return none();
}lazyAppendedAll()
Appends all elements produced by suffix lazily, without evaluating suffix until the end of this list is reached.
Implementation
ILazyList<A> lazyAppendedAll(Function0<RIterableOnce<A>> suffix) {
return _newLL(() {
if (isEmpty) {
final foo = suffix();
if (foo is ILazyList<A>) {
return foo._state;
} else if (foo.knownSize == 0) {
return _Empty<A>();
} else {
return _stateFromIterator(foo.iterator);
}
} else {
return _sCons(head, tail.lazyAppendedAll(suffix));
}
});
}lift() inherited
Returns the element at index ix as a Some. If ix is outside the range of this collection, None is returned.
Inherited from RSeq.
Implementation
Option<A> lift(int ix) => Option.when(() => isDefinedAt(ix), () => this[ix]);map() override
Returns a new collection by applying f to each element.
Implementation
@override
ILazyList<B> map<B>(Function1<A, B> f) {
if (knownIsEmpty) {
return empty();
} else {
return _mapImpl(f);
}
}maxByOption() inherited
Finds the largest element in this collection by applying f to each element and using the given Order to find the greatest.
If this collection is empty, None is returned.
Inherited from RIterableOnce.
Implementation
Option<A> maxByOption<B>(Function1<A, B> f, Order<B> order) => _minMaxByOption(f, order.max);maxOption() inherited
Finds the largest element in this collection according to the given Order.
If this collection is empty, None is returned.
Inherited from RIterableOnce.
Implementation
Option<A> maxOption(Order<A> order) => switch (knownSize) {
0 => none(),
_ => _reduceOptionIterator(iterator, order.max),
};minByOption() inherited
Finds the smallest element in this collection by applying f to each element and using the given Order to find the greatest.
If this collection is empty, None is returned.
Inherited from RIterableOnce.
Implementation
Option<A> minByOption<B>(Function1<A, B> f, Order<B> order) => _minMaxByOption(f, order.min);minOption() inherited
Finds the largest element in this collection according to the given Order.
If this collection is empty, None is returned.
Inherited from RIterableOnce.
Implementation
Option<A> minOption(Order<A> order) => switch (knownSize) {
0 => none(),
_ => _reduceOptionIterator(iterator, order.min),
};mkString() inherited
Returns a String by using each elements toString(), adding sep between each element. If start is defined, it will be prepended to the resulting string. If end is defined, it will be appended to the resulting string.
Inherited from RIterableOnce.
Implementation
String mkString({String? start, String? sep, String? end}) {
if (knownSize == 0) {
return '${start ?? ""}${end ?? ""}';
} else {
return _mkStringImpl(StringBuffer(), start ?? '', sep ?? '', end ?? '');
}
}noSuchMethod() inherited
Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);padTo() override
Returns a new collection with a length of at least len.
If this collection is shorter than len, the returned collection will have size len and elem will be used for each new element needed to reach that size.
If this collection is already at least len in size, this collection will be returned.
Implementation
@override
ILazyList<A> padTo(int len, A elem) {
if (len <= 0) {
return this;
} else {
return _newLL(() {
if (isEmpty) {
return ILazyList.fill(len, elem)._state;
} else {
return _sCons(head, tail.padTo(len - 1, elem));
}
});
}
}partition() override
Returns 2 collections as a tuple where the first tuple element will be a collection of elements that satisfy the given predicate p. The second item of the returned tuple will be elements that do not satisfy p.
Implementation
@override
(ILazyList<A>, ILazyList<A>) partition(Function1<A, bool> p) => (filter(p), filterNot(p));partitionMap() override
Applies f to each element of this collection and returns a separate collection for all applications resulting in a Left and Right respectively.
Implementation
@override
(ILazyList<A1>, ILazyList<A2>) partitionMap<A1, A2>(
Function1<A, Either<A1, A2>> f,
) {
final (left, right) = map(f).partition((x) => x.isLeft);
final a1s = left.map((l) => (l as Left<A1, A2>).a);
final a2s = right.map((l) => (l as Right<A1, A2>).b);
return (a1s, a2s);
}patch() override
Returns a new collection with replaced elements starting at from replaced by the elements of other.
Implementation
@override
ILazyList<A> patch(int from, RIterableOnce<A> other, int replaced) {
if (knownIsEmpty) {
return ILazyList.from(other);
} else {
return _patchImpl(from, other, replaced);
}
}permutations() override
Will force evaluation
Implementation
@override
RIterator<ILazyList<A>> permutations() => super.permutations().map(ILazyList.from);prepended() override
Returns a new collection with elem added to the beginning.
Implementation
@override
ILazyList<A> prepended(A elem) => _newLL(() => _sCons(elem, this));prependedAll() override
Returns a new collection with all elems added to the beginning.
Implementation
@override
ILazyList<A> prependedAll(RIterableOnce<A> prefix) {
if (knownIsEmpty) {
return ILazyList.from(prefix);
} else if (prefix.knownSize == 0) {
return this;
} else {
return _newLL(() => _stateFromIteratorConcatSuffix(prefix.iterator, () => _state));
}
}prependedLazy()
Prepends a lazily-computed element f to the front of this list.
f is not called until the first element is demanded.
Implementation
ILazyList<A> prependedLazy(Function0<A> f) => _newLL(() => _sCons(f(), this));reduce() inherited
Reduces this collection to a single value by applying op left to right.
Throws if the collection is empty.
Inherited from RIterableOnce.
Implementation
A reduce(Function2<A, A, A> op) => reduceLeft(op);reduceLeft() override
Reduces from left to right. Throws if empty.
Implementation
@override
A reduceLeft(Function2<A, A, A> op) {
if (isEmpty) {
throw UnsupportedError('empty.reduceLeft');
} else {
var reducedRes = head;
var left = tail;
while (!left.isEmpty) {
reducedRes = op(reducedRes, left.head);
left = left.tail;
}
return reducedRes;
}
}reduceLeftOption() inherited
Returns a summary values of all elements of this collection by applying f to each element, moving left to right.
If this collection is empty, None will be returned.
Inherited from RIterableOnce.
Implementation
Option<A> reduceLeftOption(Function2<A, A, A> op) => switch (knownSize) {
0 => none(),
_ => _reduceOptionIterator(iterator, op),
};reduceOption() inherited
Returns a summary values of all elements of this collection by applying f to each element, moving left to right.
If this collection is empty, None will be returned.
Inherited from RIterableOnce.
Implementation
Option<A> reduceOption(Function2<A, A, A> op) => reduceLeftOption(op);reduceRight() inherited
Reduces from right to left. Throws if empty.
Inherited from RIterableOnce.
Implementation
A reduceRight(Function2<A, A, A> op) => switch (this) {
final IndexedSeq<A> seq when seq.length > 0 => _foldr(seq, op),
_ when knownSize == 0 => throw UnsupportedError('empty.reduceLeft'),
_ => _reversed().reduceLeft((x, y) => op(y, x)),
};reduceRightOption() inherited
Returns a summary values of all elements of this collection by applying f to each element, moving right to left.
If this collection is empty, None will be returned.
Inherited from RIterableOnce.
Implementation
Option<A> reduceRightOption(Function2<A, A, A> op) => switch (knownSize) {
-1 => _reduceOptionIterator(_reversed().iterator, (x, y) => op(y, x)),
0 => none(),
_ => Some(reduceRight(op)),
};removeAt() override
Returns a new collection with the element at idx removed.
Throws RangeError if idx is out of bounds.
Implementation
@override
ILazyList<A> removeAt(int idx) {
if (0 <= idx && !knownIsEmpty) {
return _newLL(() {
if (idx == 0) {
return tail._state;
} else {
return _sCons(head, tail.removeAt(idx - 1));
}
});
} else {
throw RangeError('$idx is out of bounds (min 0, max ${length - 1})');
}
}removeFirst()
Returns a new lazy list with the first element satisfying p removed.
Returns this list unchanged if no element matches.
Implementation
ILazyList<A> removeFirst(Function1<A, bool> p) {
if (knownIsEmpty) {
return this;
} else {
if (p(head)) {
return tail;
} else {
return _newLL(() => _sCons(head, tail.removeFirst(p)));
}
}
}reverse() override
Returns a new collection with the order of the elements reversed.
Implementation
@override
ILazyList<A> reverse() => _reverseOnto(empty());reverseIterator() inherited
Returns an iterator that will emit all elements in this collection, in reverse order.
Inherited from RSeq.
Implementation
RIterator<A> reverseIterator() => reverse().iterator;sameElements() override
Returns true if this collection has the same elements, in the same order, as that.
Implementation
@override
bool sameElements(RIterable<A> that) {
var a = this;
var b = that;
while (a.nonEmpty && b.nonEmpty) {
if (identical(a, b)) return true;
if (a.head != b.head) return false;
a = a.tail;
b = b.tail;
}
return a.isEmpty && b.isEmpty;
}scan() override
Alias for scanLeft.
Implementation
@override
ILazyList<B> scan<B>(B z, Function2<B, A, B> op) => scanLeft(z, op);scanLeft() override
Returns a new collection of running totals starting with z.
The first element of the result is z; each subsequent element is the result of applying op to the previous total and the next element.
Implementation
@override
ILazyList<B> scanLeft<B>(B z, Function2<B, A, B> op) {
if (knownIsEmpty) {
return _newLL(() => _sCons(z, empty()));
} else {
return _newLL(() => _scanLeftState(z, op));
}
}scanRight() override
Will force evaluation
Implementation
@override
ILazyList<B> scanRight<B>(B z, Function2<A, B, B> op) => ILazyList.from(super.scanRight(z, op));segmentLength() inherited
Returns the length of the longest prefix starting at from where every element satisfies p.
Inherited from RSeq.
Implementation
int segmentLength(Function1<A, bool> p, [int from = 0]) {
var i = 0;
final it = iterator.drop(from);
while (it.hasNext && p(it.next())) {
i += 1;
}
return i;
}slice() override
Returns a new collection containing elements in the range [from, until).
Implementation
@override
ILazyList<A> slice(int from, int until) => take(until).drop(from);sliding() override
Returns an iterator where elements are fixed size chunks of size n of the original collection. Each chunk is calculated by sliding a 'window' of size n over the original collection, moving the window step elements at a time.
Implementation
@override
RIterator<ILazyList<A>> sliding(int size, [int step = 1]) => _slidingImpl(size, step);sortBy() override
Will force evaluation
Implementation
@override
ILazyList<A> sortBy<B>(Order<B> order, Function1<A, B> f) => sorted(order.contramap(f));sorted() override
Will force evaluation
Implementation
@override
ILazyList<A> sorted(Order<A> order) => ILazyList.from(super.sorted(order));sortWith() override
Will force evaluation
Implementation
@override
ILazyList<A> sortWith(Function2<A, A, bool> lt) => sorted(Order.fromLessThan(lt));span() override
Returns two collections: elements before and starting from the first element that does not satisfy p.
Implementation
@override
(ILazyList<A>, ILazyList<A>) span(Function1<A, bool> p) => (takeWhile(p), dropWhile(p));splitAt() override
Returns two collections: the first n elements and the remainder.
Implementation
@override
(ILazyList<A>, ILazyList<A>) splitAt(int n) => (take(n), drop(n));startsWith() inherited
Returns true if the beginning of this collection corresponds with that.
Inherited from RSeq.
Implementation
bool startsWith(RIterableOnce<A> that, [int offset = 0]) {
final i = iterator.drop(offset);
final j = that.iterator;
while (j.hasNext && i.hasNext) {
if (i.next() != j.next()) return false;
}
return !j.hasNext;
}take() override
Returns a new collection containing only the first n elements.
Implementation
@override
ILazyList<A> take(int n) {
if (knownIsEmpty) {
return empty();
} else {
return _takeImpl(n);
}
}takeRight() override
Returns a new collection with the last n elements of this collection. If n is greater than the size of this collection, the original collection is returned.
Implementation
@override
ILazyList<A> takeRight(int n) {
if (n <= 0 || knownIsEmpty) {
return empty();
} else {
return _takeRightImpl(this, n);
}
}takeWhile() override
Returns a new collection of leading elements that satisfy p.
Implementation
@override
ILazyList<A> takeWhile(Function1<A, bool> p) {
if (knownIsEmpty) {
return empty();
} else {
return _takeWhileImpl(p);
}
}tapEach() override
Applies f to each element in this collection, discarding any results and returns this collection.
Implementation
@override
ILazyList<A> tapEach<U>(Function1<A, U> f) => map((a) {
f(a);
return a;
});toIList() inherited
Returns an IList with the same elements as this collection.
Inherited from RIterableOnce.
Implementation
IList<A> toIList() => IList.from(this);toIndexedSeq() inherited
Returns an IndexedSeq with the same elements as this collection.
Inherited from RIterableOnce.
Implementation
IndexedSeq<A> toIndexedSeq() => IndexedSeq.from(this);toISet() inherited
Returns an ISet with the same elements as this collection, duplicates removed.
Inherited from RIterableOnce.
Implementation
ISet<A> toISet() => ISet.from(this);toIVector() inherited
Returns an IVector with the same elements as this collection.
Inherited from RIterableOnce.
Implementation
IVector<A> toIVector() => IVector.from(this);toList() inherited
Returns a new List with the same elements as this collection.
Inherited from RIterableOnce.
Implementation
List<A> toList({bool growable = true}) {
if (growable) {
final it = iterator;
final res = List<A>.empty(growable: true);
while (it.hasNext) {
res.add(it.next());
}
return res;
} else {
final it = iterator;
return List.generate(size, (_) => it.next());
}
}toSeq() inherited
Returns a RSeq with the same elements as this collection.
Inherited from RIterableOnce.
Implementation
RSeq<A> toSeq() => RSeq.from(this);toString() override
A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Implementation
@override
String toString() => addStringNoForce(StringBuffer('ILazyList'), '(', ', ', ')').toString();traverseEither() override
Will force evaluation
Implementation
@override
Either<B, ILazyList<C>> traverseEither<B, C>(Function1<A, Either<B, C>> f) =>
super.traverseEither(f).map(ILazyList.from);traverseOption() override
Will force evaluation
Implementation
@override
Option<ILazyList<B>> traverseOption<B>(Function1<A, Option<B>> f) =>
super.traverseOption(f).map(ILazyList.from);updated() override
Returns a new collection with the element at index replaced by elem.
Throws RangeError if index is out of bounds.
Implementation
@override
ILazyList<A> updated(int index, A elem) => _updatedImpl(index, elem, index);zip() override
Returns a new collection that combines corresponding elements from this collection and that as a tuple. The length of the returned collection will be the minimum of this collections size and the size of that.
Implementation
@override
ILazyList<(A, B)> zip<B>(RIterableOnce<B> that) {
if (knownIsEmpty || that.knownSize == 0) {
return empty();
} else {
return _newLL(() => _zipState(that.iterator));
}
}zipAll() override
Returns a new collection that combines corresponding elements from this collection and that as a tuple. The length of the returned collection will be the maximum of this collections size and thes size of that. If this collection is shorter than that, thisElem will be used to fill in the resulting collection. If that is shorter, thatElem will be used to will in the resulting collection.
Implementation
@override
ILazyList<(A, B)> zipAll<B>(RIterableOnce<B> that, A thisElem, B thatElem) {
if (knownIsEmpty) {
if (that.knownSize == 0) {
return empty();
} else {
return ILazyList.continually(thisElem).zip(that);
}
} else {
if (that.knownSize == 0) {
return zip(ILazyList.continually(thatElem));
} else {
return _newLL(() => _zipAllState(that.iterator, thisElem, thatElem));
}
}
}zipWithIndex() override
Return a new collection with each element of this collection paired with it's respective index.
Implementation
@override
ILazyList<(A, int)> zipWithIndex() => zip(ILazyList.ints(0));Extension Methods
flatten() extension
Concatenates all inner iterables into a single IList.
Available on RIterable<A>, provided by the RIterableNested2Ops<A> extension
Implementation
RIterable<A> flatten() {
final it = iterator;
final b = IList.builder<A>();
while (it.hasNext) {
b.addAll(it.next());
}
return b.toIList();
}flatten() extension
Combines all nested lazy lists into one using concatenation.
Available on ILazyList<A>, provided by the ILazyListNestedOps<A> extension
Implementation
ILazyList<A> flatten() => flatMap(identity);product() extension
Returns the product of all elements in this list
Available on RIterableOnce<A>, provided by the RIterableIntOps extension
Implementation
int product() {
var p = 1;
final it = iterator;
while (it.hasNext) {
p *= it.next();
}
return p;
}product() extension
Returns the product of all elements in this list
Available on RIterableOnce<A>, provided by the RIterableDoubleOps extension
Implementation
double product() {
var p = 1.0;
final it = iterator;
while (it.hasNext) {
p *= it.next();
}
return p;
}sum() extension
Returns the sum of all elements in this list
Available on RIterableOnce<A>, provided by the RIterableIntOps extension
Implementation
int sum() {
var s = 0;
final it = iterator;
while (it.hasNext) {
s += it.next();
}
return s;
}sum() extension
Returns the sum of all elements in this list
Available on RIterableOnce<A>, provided by the RIterableDoubleOps extension
Implementation
double sum() {
var s = 0.0;
final it = iterator;
while (it.hasNext) {
s += it.next();
}
return s;
}toIMap() extension
Creates a new IMap where each tuple element of this list is used to create a key and value respectively.
Available on RIterable<A>, provided by the RIterableTuple2Ops<A, B> extension
Implementation
IMap<A, B> toIMap() => IMap.from(this);unzip() extension
Splits a collection of pairs into two separate collections.
Available on RIterable<A>, provided by the RibsIterableTuple2Ops<A, B> extension
Implementation
(RIterable<A>, RIterable<B>) unzip() => (
views.Map(this, (a) => a.$1),
views.Map(this, (a) => a.$2),
);Operators
operator ==() override
The equality operator.
The default behavior for all Objects is to return true if and only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Implementation
@override
bool operator ==(Object other) => identical(this, other);operator []() override
Returns the element at index idx.
Implementation
@override
A operator [](int idx) {
if (idx < 0) throw RangeError.index(idx, 'Invalid ILazyList index: $idx');
final skipped = drop(idx);
if (skipped.isEmpty) {
throw RangeError.index(idx, 'Invalid ILazyList index: $idx');
}
return skipped.head;
}Static Methods
builder()
Returns a mutable builder that accumulates elements into an ILazyList.
Implementation
static ILazyListBuilder<A> builder<A>() => ILazyListBuilder();continually()
Returns an infinite lazy list where every element is elem.
Implementation
static ILazyList<A> continually<A>(A elem) => _newLL(() => _sCons(elem, continually(elem)));empty() override
Returns an empty ILazyList.
Implementation
static ILazyList<A> empty<A>() => _newLL(() => _Empty<A>()).force();fill()
Creates a lazy list of length len where every element is elem.
Implementation
static ILazyList<A> fill<A>(int len, A elem) =>
len > 0 ? _newLL(() => _sCons(elem, fill(len - 1, elem))) : empty<A>();from() override
Creates an ILazyList from any RIterableOnce.
Returns coll directly when it is already an ILazyList.
Implementation
static ILazyList<A> from<A>(RIterableOnce<A> coll) {
if (coll is ILazyList<A>) {
return coll;
} else if (coll.knownSize == 0) {
return empty();
} else {
return _newLL(() => _stateFromIterator(coll.iterator));
}
}ints()
Returns an infinite lazy list of integers beginning at start, incrementing by step (default 1).
Implementation
static ILazyList<int> ints(int start, [int step = 1]) =>
_newLL(() => _sCons(start, ints(start + step, step)));iterate()
Returns an infinite lazy list produced by repeatedly applying f, starting from the value start().
Implementation
static ILazyList<A> iterate<A>(Function0<A> start, Function1<A, A> f) => _newLL(() {
final head = start();
return _sCons(head, iterate(() => f(head), f));
});tabulate()
Creates a lazy list of length n where element i is f(i).
Returns an empty list when n <= 0.
Implementation
static ILazyList<A> tabulate<A>(int n, Function1<int, A> f) {
ILazyList<A> at(int index) {
if (index < n) {
return _newLL(() => _sCons(f(index), at(index + 1)));
} else {
return empty();
}
}
return at(0);
}unfold()
Creates a lazy list by unfolding initial state with f.
Each call to f either returns Some((element, nextState)) to emit an element and continue, or None to terminate the list.
Implementation
static ILazyList<A> unfold<A, S>(
S initial,
Function1<S, Option<(A, S)>> f,
) => _newLL(
() => f(initial).fold(
() => _Empty<A>(),
(t) => _sCons(t.$1, unfold(t.$2, f)),
),
);