PersistentSet<E> abstract class
abstract class PersistentSet<E> implements Iterable<E> { /// Creates an empty [PersistentSet] using its default implementation. factory PersistentSet() => new _PersistentSetImpl<E>(); /** * Creates an immutable copy of [elements] using the default implementation * of [PersistentSet]. */ factory PersistentSet.from(Iterable<E> elements) { PersistentSet<E> result = new _PersistentSetImpl<E>(); for (E element in elements) { result = result.insert(element); } return result; } PersistentSet<E> insert(E element); PersistentSet<E> delete(E element); PersistentSet<E> union(PersistentSet<E> persistentSet); /// Alias for [union]. PersistentSet<E> operator +(PersistentSet<E> persistentSet); PersistentSet<E> difference(PersistentSet<E> persistentSet); /// Alias for [difference]. PersistentSet<E> operator -(PersistentSet<E> persistentSet); PersistentSet<Pair> cartesianProduct(PersistentSet persistentSet); /// Alias for [cartesianProduct]. PersistentSet<Pair> operator *(PersistentSet persistentSet); PersistentSet<E> intersection(PersistentSet<E> persistentSet); /// Randomly picks an element of [this]. E pickRandomElement([Random random]); }
Subclasses
Implements
Constructors
factory PersistentSet() #
Creates an empty PersistentSet using its default implementation.
factory PersistentSet() => new _PersistentSetImpl<E>();
factory PersistentSet.from(Iterable<E> elements) #
Creates an immutable copy of elements using the default implementation of PersistentSet.
factory PersistentSet.from(Iterable<E> elements) { PersistentSet<E> result = new _PersistentSetImpl<E>(); for (E element in elements) { result = result.insert(element); } return result; }
Properties
final E first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first;
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty;
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty;
final Iterator<E> iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator<E> get iterator;
final int length #
Returns the number of elements in this
.
Counting all elements may be involve running through all elements and can therefore be slow.
int get length;
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;
Operators
abstract PersistentSet<E> operator +(PersistentSet<E> persistentSet) #
Alias for union.
abstract PersistentSet<E> operator -(PersistentSet<E> persistentSet) #
Alias for difference.
abstract PersistentSet<Pair> operator *(PersistentSet persistentSet) #
Alias for cartesianProduct.
Methods
abstract bool any(bool test(E element)) #
Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.
abstract PersistentSet<Pair> cartesianProduct(PersistentSet persistentSet) #
abstract bool contains(Object element) #
Returns true if the collection contains an element equal to element.
abstract PersistentSet<E> delete(E element) #
abstract PersistentSet<E> difference(PersistentSet<E> persistentSet) #
abstract E 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
.
abstract bool every(bool test(E element)) #
Returns true if every elements of this collection satisify the
predicate
test. Returns false
otherwise.
abstract E firstWhere(bool test(E element), {E 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.
abstract 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);
abstract void forEach(void f(E element)) #
Applies the function f to each element of this collection.
abstract PersistentSet<E> insert(E element) #
abstract PersistentSet<E> intersection(PersistentSet<E> persistentSet) #
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 = ""]) { StringBuffer buffer = new StringBuffer(); buffer.writeAll(this, separator); return buffer.toString(); }
abstract E lastWhere(bool test(E element), {E 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.
abstract Iterable map(f(E element)) #
Returns a lazy Iterable where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
abstract 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);
abstract E singleWhere(bool test(E element)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
abstract Iterable<E> skip(int n) #
Returns an Iterable that skips the first n elements.
If this
has fewer than
n elements, then the resulting Iterable is
empty.
It is an error if n is negative.
abstract 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.
abstract 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.
abstract PersistentSet<E> union(PersistentSet<E> persistentSet) #
abstract Iterable<E> where(bool test(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.