Dart DocumentationpersistentPersistentMapBase<K, V>

PersistentMapBase<K, V> abstract class

A base class for implementations of PersistentMap.

abstract class PersistentMapBase<K, V>
   extends IterableBase<Pair<K, V>>
   implements PersistentMap<K, V> {

 Map<K, V> toMap() {
   Map<K, V> result = new Map<K, V>();
   this.forEachKeyValue((K k, V v) { result[k] = v; });
   return result;
 }

 String toString() {
   StringBuffer buffer = new StringBuffer('{');
   bool comma = false;
   this.forEachKeyValue((K k, V v) {
     if (comma) buffer.write(', ');
     buffer.write('$k: $v');
     comma = true;
   });
   buffer.write('}');
   return buffer.toString();
 }

 // Optimized version of Iterable's contains
 bool contains(Pair<K, V> entry) {
   final value = this.lookup(entry.fst);
   return value.isDefined && value.value == entry.snd;
 }

 Iterable<K> get keys => this.map((Pair<K, V> pair) => pair.fst);

 Iterable<V> get values => this.map((Pair<K, V> pair) => pair.snd);

 Pair<K, V> pickRandomEntry([Random random]) =>
     elementAt((random != null ? random : _random).nextInt(this.length));
}

Extends

IterableBase<Pair<K, V>> > PersistentMapBase<K, V>

Implements

PersistentMap<K, V>

Properties

final E first #

inherited from IterableBase

Returns the first element.

If this is empty throws a StateError. Otherwise this method is equivalent to this.elementAt(0)

docs inherited from Iterable<E>
E get first {
 Iterator it = iterator;
 if (!it.moveNext()) {
   throw new StateError("No elements");
 }
 return it.current;
}

final bool isEmpty #

inherited from IterableBase

Returns true if there is no element in this collection.

docs inherited from Iterable<E>
bool get isEmpty => !iterator.moveNext();

final bool isNotEmpty #

inherited from IterableBase

Returns true if there is at least one element in this collection.

docs inherited from Iterable<E>
bool get isNotEmpty => !isEmpty;

final Iterator<E> iterator #

inherited from Iterable

Returns an Iterator that iterates over this Iterable object.

Iterator<E> get iterator;

final Iterable<K> keys #

The keys of this.

docs inherited from PersistentMap<K, V>
Iterable<K> get keys => this.map((Pair<K, V> pair) => pair.fst);

final E last #

inherited from IterableBase

Returns the last element.

If this is empty throws a StateError.

docs inherited from Iterable<E>
E get last {
 Iterator it = iterator;
 if (!it.moveNext()) {
   throw new StateError("No elements");
 }
 E result;
 do {
   result = it.current;
 } while(it.moveNext());
 return result;
}

final int length #

inherited from IterableBase

Returns the number of elements in this.

Counting all elements may be involve running through all elements and can therefore be slow.

docs inherited from Iterable<E>
int get length {
 int count = 0;
 Iterator it = iterator;
 while (it.moveNext()) {
   count++;
 }
 return count;
}

final E single #

inherited from IterableBase

Returns the single element in this.

If this is empty or has more than one element throws a StateError.

docs inherited from Iterable<E>
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;
}

final Iterable<V> values #

The values of this.

docs inherited from PersistentMap<K, V>
Iterable<V> get values => this.map((Pair<K, V> pair) => pair.snd);

Methods

abstract PersistentMap<K, V> adjust(K key, V update(V value)) #

inherited from PersistentMap

Returns a new map identical to this except that the value it possibly binds to key has been adjusted by update.

{'a': 1, 'b': 2}.adjust('b', (x) => x + 1) == {'a', 1, 'b', 3}
{'a': 1}.adjust('b', (x) => x + 1) == {'a', 1}

bool any(bool f(E element)) #

inherited from IterableBase

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

docs inherited from Iterable<E>
bool any(bool f(E element)) {
 for (E element in this) {
   if (f(element)) return true;
 }
 return false;
}

bool contains(Pair<K, V> entry) #

Returns true if the collection contains an element equal to element.

docs inherited from Iterable<E>
bool contains(Pair<K, V> entry) {
 final value = this.lookup(entry.fst);
 return value.isDefined && value.value == entry.snd;
}

abstract PersistentMap<K, V> delete(K key) #

inherited from PersistentMap

Returns a new map identical to this except that it doesn't bind key anymore.

{'a': 1, 'b': 2}.delete('b') == {'a': 1}
{'a': 1}.delete('b') == {'a': 1}

E elementAt(int index) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
E elementAt(int index) {
 if (index is! int || index < 0) throw new RangeError.value(index);
 int remaining = index;
 for (E element in this) {
   if (remaining == 0) return element;
   remaining--;
 }
 throw new RangeError.value(index);
}

bool every(bool f(E element)) #

inherited from IterableBase

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

docs inherited from Iterable<E>
bool every(bool f(E element)) {
 for (E element in this) {
   if (!f(element)) return false;
 }
 return true;
}

Iterable expand(Iterable f(E element)) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable expand(Iterable f(E element)) =>
   new ExpandIterable<E, dynamic>(this, f);

dynamic firstWhere(bool test(E value), {Object orElse()}) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
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)) #

inherited from IterableBase

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);
docs inherited from Iterable<E>
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)) #

inherited from IterableBase

Applies the function f to each element of this collection.

docs inherited from Iterable<E>
void forEach(void f(E element)) {
 for (E element in this) f(element);
}

abstract void forEachKeyValue(f(K key, V value)) #

inherited from PersistentMap

Evaluates f(key, value) for each (key, value) pair in this.

abstract PersistentMap<K, V> insert(K key, V value, [V combine(V oldvalue, V newvalue)]) #

inherited from PersistentMap

Returns a new map identical to this except that it binds key to value.

If key was bound to some oldvalue in this, it is nevertheless bound to value in the new map. If key was bound to some oldvalue in this and if combine is provided then key it is bound to combine(oldvalue, value) in the new map.

{'a': 1}.insert('b', 2) == {'a': 1, 'b', 2}
{'a': 1, 'b': 2}.insert('b', 3) == {'a': 3, 'b', 3}
{'a': 1, 'b': 2}.insert('b', 3, (x,y) => x - y) == {'a': 3, 'b', -1}

abstract PersistentMap<K, V> intersection(PersistentMap<K, V> other, [V combine(V left, V right)]) #

inherited from PersistentMap

Returns a new map whose (key, value) pairs are the intersection of those of this and other.

The intersection is right-biased: values from other are retained. If combine is provided, the retained value for a key present in both this and other is then combine(leftvalue, rightvalue) where leftvalue is the value bound to key in this and rightvalue is the one bound to key in other.

{'a': 1}.intersection({'b': 2}) == {}
{'a': 1}.intersection({'a': 3, 'b': 2}) == {'a': 3}
{'a': 1}.intersection({'a': 3, 'b': 2}, (x,y) => x + y) == {'a': 4}

Note that intersection is commutative if and only if combine is provided and if it is commutative.

String join([String separator = ""]) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
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()}) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
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");
}

abstract Option<V> lookup(K key) #

inherited from PersistentMap

Looks up the value possibly bound to key in this. Returns Option.some(value) if it exists, Option.none() otherwise.

{'a': 1}.lookup('b') == Option.none()
{'a': 1, 'b': 2}.lookup('b') == Option.some(2)

Iterable map(f(E element)) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);

abstract PersistentMap mapValues(f(V value)) #

inherited from PersistentMap

Returns a new map identical to this where each value has been updated by f.

{'a': 1, 'b': 2}.mapValues((x) => x + 1) == {'a', 2, 'b', 3}
{}.mapValues((x) => x + 1) == {}

Pair<K, V> pickRandomEntry([Random random]) #

Randomly picks an entry of this.

docs inherited from PersistentMap<K, V>
Pair<K, V> pickRandomEntry([Random random]) =>
   elementAt((random != null ? random : _random).nextInt(this.length));

E reduce(E combine(E value, E element)) #

inherited from IterableBase

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);
docs inherited from Iterable<E>
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)) #

inherited from IterableBase

Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.

docs inherited from Iterable<E>
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) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable<E> skip(int n) {
 return new SkipIterable<E>(this, n);
}

Iterable<E> skipWhile(bool test(E value)) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable<E> skipWhile(bool test(E value)) {
 return new SkipWhileIterable<E>(this, test);
}

Iterable<E> take(int n) #

inherited from IterableBase

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

docs inherited from Iterable<E>
Iterable<E> take(int n) {
 return new TakeIterable<E>(this, n);
}

Iterable<E> takeWhile(bool test(E value)) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable<E> takeWhile(bool test(E value)) {
 return new TakeWhileIterable<E>(this, test);
}

List<E> toList({bool growable: true}) #

inherited from IterableBase

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

docs inherited from Iterable<E>
List<E> toList({ bool growable: true }) =>
   new List<E>.from(this, growable: growable);

Map<K, V> toMap() #

Returns a mutable copy of this.

docs inherited from PersistentMap<K, V>
Map<K, V> toMap() {
 Map<K, V> result = new Map<K, V>();
 this.forEachKeyValue((K k, V v) { result[k] = v; });
 return result;
}

Set<E> toSet() #

inherited from IterableBase

Creates a Set containing the elements of this Iterable.

docs inherited from Iterable<E>
Set<E> toSet() => new Set<E>.from(this);

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
 StringBuffer buffer = new StringBuffer('{');
 bool comma = false;
 this.forEachKeyValue((K k, V v) {
   if (comma) buffer.write(', ');
   buffer.write('$k: $v');
   comma = true;
 });
 buffer.write('}');
 return buffer.toString();
}

abstract PersistentMap<K, V> union(PersistentMap<K, V> other, [V combine(V left, V right)]) #

inherited from PersistentMap

Returns a new map whose (key, value) pairs are the union of those of this and other.

The union is right-biased: if a key is present in both this and other, the value from other is retained. If combine is provided, the retained value for a key present in both this and other is then combine(leftvalue, rightvalue) where leftvalue is the value bound to key in this and rightvalue is the one bound to key in other.

{'a': 1}.union({'b': 2}) == {'a': 1, 'b': 2}
{'a': 1}.union({'a': 3, 'b': 2}) == {'a': 3, 'b': 2}
{'a': 1}.union({'a': 3, 'b': 2}, (x,y) => x + y) == {'a': 4, 'b': 2}

Note that union is commutative if and only if combine is provided and if it is commutative.

Iterable<E> where(bool f(E element)) #

inherited from IterableBase

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.

docs inherited from Iterable<E>
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);