Skip to content

IMap<K, V>

mixin IMap<K, V> on RIterableOnce<Record>, RIterable<Record>, RMap<K, V>

An immutable key-value map.

IMap uses a small-map optimization for up to four entries, backed by IHashMap (a CHAMP trie) for larger collections. All keys must be distinct; inserting a duplicate key replaces the existing value.

Construct with imap, IMap.fromDart, IMap.from, or IMap.empty. Use IMap.builder when building incrementally.

dart
final m = imap({'a': 1, 'b': 2});
m.get('a');                // Some(1)
(m + ('c', 3)).size;       // 3
(m - 'b').keys.toIList();  // IList('a')

Superclass Constraints

  • RIterableOnce<Record>
  • RIterable<Record>
  • RMap<K, V>

Properties

hashCode no setter inherited

int get hashCode

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.

Inherited from Object.

Implementation
dart
external int get hashCode;

head no setter inherited

Record get head

Returns the first element of this collection, or throws if it is empty.

Inherited from RIterable.

Implementation
dart
A get head => iterator.next();

headOption no setter inherited

Option<Record> get headOption

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
dart
Option<A> get headOption {
  final it = iterator;
  return Option.when(() => it.hasNext, () => it.next());
}

init no setter override

IMap<K, V> get init

Returns all elements from this collection except the last. If this collection is empty, an empty collection is returned.

Implementation
dart
@override
IMap<K, V> get init => IMap.from(super.init);

inits no setter override

RIterator<IMap<K, V>> get inits

Returns an iterator of all potential tails of this collection, starting with the entire collection and ending with an empty one.

Implementation
dart
@override
RIterator<IMap<K, V>> get inits => super.inits.map(IMap.from);

isEmpty no setter inherited

bool get isEmpty

Whether this collection contains no elements.

Inherited from RIterableOnce.

Implementation
dart
bool get isEmpty => switch (knownSize) {
  -1 => !iterator.hasNext,
  0 => true,
  _ => false,
};

isNotEmpty no setter inherited

bool get isNotEmpty

Whether this collection contains at least one element.

Inherited from RIterableOnce.

Implementation
dart
bool get isNotEmpty => !isEmpty;

isTraversableAgain no setter inherited

bool get isTraversableAgain

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
dart
bool get isTraversableAgain => false;

iterator no setter inherited

RIterator<Record> get iterator

Returns an RIterator over the elements of this collection.

Inherited from RIterableOnce.

Implementation
dart
RIterator<A> get iterator;

keys no setter inherited

RIterable<K> get keys

Returns a RIterable of all the keys stored in the map.

Inherited from RMap.

Implementation
dart
RIterable<K> get keys => keysIterator.toISet();

keySet no setter inherited

RSet<K> get keySet

Returns an RSet of all keys in this map.

Inherited from RMap.

Implementation
dart
RSet<K> get keySet => keysIterator.toISet();

keysIterator no setter inherited

RIterator<K> get keysIterator

Returns an RIterator over all keys of this map.

Inherited from RMap.

Implementation
dart
RIterator<K> get keysIterator => iterator.map((kv) => kv.$1);

knownSize no setter inherited

int get knownSize

Returns the number of elements in this collection, if that number is already known. If not, -1 is returned.

Inherited from RIterableOnce.

Implementation
dart
int get knownSize => -1;

last no setter inherited

Record get last

Returns the last element of this collection, or throws if it is empty.

Inherited from RIterable.

Implementation
dart
A get last {
  final it = iterator;
  var lst = it.next();

  while (it.hasNext) {
    lst = it.next();
  }

  return lst;
}

lastOption no setter inherited

Option<Record> get lastOption

Returns the last element of this collection as a Some, or None if this collection is empty.

Inherited from RIterable.

Implementation
dart
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);
  }
}

nonEmpty no setter inherited

bool get nonEmpty

Whether this collection contains at least one element.

Inherited from RIterableOnce.

Implementation
dart
bool get nonEmpty => !isEmpty;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

size no setter inherited

int get size

Returns the number of elements in this collection.

Inherited from RIterableOnce.

Implementation
dart
int get size {
  if (knownSize >= 0) {
    return knownSize;
  } else {
    final it = iterator;
    var len = 0;

    while (it.hasNext) {
      len += 1;
      it.next();
    }

    return len;
  }
}

tail no setter override

IMap<K, V> get tail

Returns a new collection with the first element removed. If this collection is empty, an empty collection is returned.

Implementation
dart
@override
IMap<K, V> get tail => IMap.from(super.tail);

tails no setter override

RIterator<IMap<K, V>> get tails

Returns an iterator of all potential tails of this collection, starting with the entire collection and ending with an empty one.

Implementation
dart
@override
RIterator<IMap<K, V>> get tails => super.tails.map(IMap.from);

values no setter inherited

RIterable<V> get values

Returns a RIterable of all values stored in this map.

Inherited from RMap.

Implementation
dart
RIterable<V> get values => valuesIterator.toSeq();

valuesIterator no setter inherited

RIterator<V> get valuesIterator

Returns a RIterator of all values stored in this map.

Inherited from RMap.

Implementation
dart
RIterator<V> get valuesIterator => iterator.map((kv) => kv.$2);

Methods

andThen()

Option<V2> Function(K) andThen<V2>(V2 Function(V) f)

Returns a new function that will accept a key of type K and apply the value for that key, if it exists as a Some. If this map doesn't contain the key, None will be returned.

Implementation
dart
Function1<K, Option<V2>> andThen<V2>(Function1<V, V2> f) => (key) => get(key).map(f);

collect() inherited

RIterable<B> collect<B>(Option<B> Function(Record) f)

Returns a new collection by applying f to each element an only keeping results of type Some.

Inherited from RIterable.

Implementation
dart
@override
RIterable<B> collect<B>(Function1<A, Option<B>> f) => views.Collect(this, f);

collectFirst() inherited

Option<B> collectFirst<B>(Option<B> Function(Record) f)

Applies f to each element of this collection, returning the first element that results in a Some, if any.

Inherited from RIterableOnce.

Implementation
dart
Option<B> collectFirst<B>(Function1<A, Option<B>> f) {
  final it = iterator;

  while (it.hasNext) {
    final x = f(it.next());
    if (x.isDefined) return x;
  }

  return none();
}

compose()

Option<V> Function(A) compose<A>(K Function(A) f)

Composes the given function f with get and returns the result.

Implementation
dart
Function1<A, Option<V>> compose<A>(Function1<A, K> f) => (a) => get(f(a));

concat() override

IMap<K, V> concat(RIterableOnce<Record> suffix)

Returns a copy of this collection, with elems added to the end.

Implementation
dart
@override
IMap<K, V> concat(RIterableOnce<(K, V)> suffix) => IMap.from(super.concat(suffix));

contains() override

bool contains(K key)

Returns true if this map contains the key key, false otherwise.

Implementation
dart
@override
bool contains(K key) => get(key).isDefined;

copyToArray() inherited

int copyToArray(Array<Record> xs, [int start = 0, int? n])

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
dart
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

bool corresponds<B>(RIterable<B> that, bool Function(Record, B) p)

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 RIterableOnce.

Implementation
dart
bool corresponds<B>(
  covariant RIterable<B> that,
  Function2<A, B, bool> p,
) {
  final a = iterator;
  final b = that.iterator;

  while (a.hasNext && b.hasNext) {
    if (!p(a.next(), b.next())) return false;
  }

  return !a.hasNext && !b.hasNext;
}

count() inherited

int count(bool Function(Record) p)

Return the number of elements in this collection that satisfy the given predicate.

Inherited from RIterableOnce.

Implementation
dart
int count(Function1<A, bool> p) {
  var res = 0;
  final it = iterator;

  while (it.hasNext) {
    if (p(it.next())) res += 1;
  }

  return res;
}

defaultValue() inherited

V defaultValue(K key)

Called by operator [] when key is absent. Throws by default; override to supply a fallback value.

Inherited from RMap.

Implementation
dart
V defaultValue(K key) => throw Exception("No such value for key: '$key'");

drop() override

IMap<K, V> drop(int n)

Returns a new collection with the first n elements removed.

Implementation
dart
@override
IMap<K, V> drop(int n) => IMap.from(super.drop(n));

dropRight() override

IMap<K, V> dropRight(int n)

Return a new collection with the last n elements removed.

Implementation
dart
@override
IMap<K, V> dropRight(int n) => IMap.from(super.dropRight(n));

dropWhile() override

IMap<K, V> dropWhile(bool Function(Record) p)

Returns a new collection with leading elements satisfying p removed.

Implementation
dart
@override
IMap<K, V> dropWhile(Function1<(K, V), bool> p) => IMap.from(super.dropWhile(p));

exists() inherited

bool exists(bool Function(Record) p)

Returns true if any element of this collection satisfies the given predicate, false if no elements satisfy it.

Inherited from RIterableOnce.

Implementation
dart
bool exists(Function1<A, bool> p) {
  var res = false;
  final it = iterator;

  while (!res && it.hasNext) {
    res = p(it.next());
  }

  return res;
}

filter() override

IMap<K, V> filter(bool Function(Record) p)

Returns a new collection containing only elements that satisfy p.

Implementation
dart
@override
IMap<K, V> filter(Function1<(K, V), bool> p) => from(super.filter(p));

filterNot() override

IMap<K, V> filterNot(bool Function(Record) p)

Returns a new collection containing only elements that do not satisfy p.

Implementation
dart
@override
IMap<K, V> filterNot(Function1<(K, V), bool> p) => from(super.filterNot(p));

find() inherited

Option<Record> find(bool Function(Record) p)

Returns the first element from this collection that satisfies the given predicate p. If no element satisfies p, None is returned.

Inherited from RIterableOnce.

Implementation
dart
Option<A> find(Function1<A, bool> p) {
  final it = iterator;

  while (it.hasNext) {
    final a = it.next();
    if (p(a)) return Some(a);
  }

  return none();
}

flatMap() inherited

RIterable<B> flatMap<B>(RIterableOnce<B> Function(Record) f)

Returns a new collection by applying f to each element and concatenating the results.

Inherited from RIterable.

Implementation
dart
@override
RIterable<B> flatMap<B>(Function1<A, RIterableOnce<B>> f) => views.FlatMap(this, f);

fold() inherited

Record fold(Record init, Record Function(Record, Record) op)

Alias for foldLeft with a same-type accumulator.

Inherited from RIterable.

Implementation
dart
A fold(A init, Function2<A, A, A> op) => foldLeft(init, op);

foldLeft() inherited

B foldLeft<B>(B z, B Function(B, Record) op)

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.

Inherited from RIterableOnce.

Implementation
dart
B foldLeft<B>(B z, Function2<B, A, B> op) {
  var result = z;
  final it = iterator;

  while (it.hasNext) {
    result = op(result, it.next());
  }

  return result;
}

foldRight() inherited

B foldRight<B>(B z, B Function(Record, B) op)

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
dart
B foldRight<B>(B z, Function2<A, B, B> op) => _reversed().foldLeft(z, (b, a) => op(a, b));

forall() inherited

bool forall(bool Function(Record) p)

Returns true if all elements of this collection satisfy the given predicate, false if any elements do not.

Inherited from RIterableOnce.

Implementation
dart
bool forall(Function1<A, bool> p) {
  var res = true;
  final it = iterator;

  while (res && it.hasNext) {
    res = p(it.next());
  }

  return res;
}

foreach() inherited

void foreach<U>(U Function(Record) f)

Applies f to each element of this collection, discarding any resulting values.

Inherited from RIterableOnce.

Implementation
dart
void foreach<U>(Function1<A, U> f) {
  final it = iterator;
  while (it.hasNext) {
    f(it.next());
  }
}

get() inherited

Option<V> get(K key)

Returns the value for the given key key as a Some, or None if this map doesn't contain the key.

Inherited from RMap.

Implementation
dart
Option<V> get(K key);

getOrElse() inherited

V getOrElse(K key, V Function() orElse)

Returns the value for the given key key, or orElse if this map doesn't contain the key.

Inherited from RMap.

Implementation
dart
V getOrElse(K key, Function0<V> orElse) => get(key).getOrElse(orElse);

groupBy() override

IMap<K2, IMap<K, V>> groupBy<K2>(K2 Function(Record) f)

Partitions all elements of this collection by applying f to each element and accumulating duplicate keys in the returned IMap.

Implementation
dart
@override
IMap<K2, IMap<K, V>> groupBy<K2>(Function1<(K, V), K2> f) =>
    IMap.from(super.groupBy(f).map((a) => (a.$1, IMap.from(a.$2))));

grouped() override

RIterator<IMap<K, V>> grouped(int size)

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
dart
@override
RIterator<IMap<K, V>> grouped(int size) => iterator.grouped(size).map(IMap.from);

groupMap() inherited

IMap<K, RIterable<B>> groupMap<K, B>(
  K Function(Record) key,
  B Function(Record) f,
)

Creates a new map by generating a key-value pair for each elements of this collection using key and f. Any elements that generate the same key will have the resulting values accumulated in the returned map.

Inherited from RIterable.

Implementation
dart
IMap<K, RIterable<B>> groupMap<K, B>(
  Function1<A, K> key,
  Function1<A, B> f,
) {
  &#47;&#47; TODO: use IMap.builder, revist implementation
  final m = <K, ListBuffer<B>>{};
  final it = iterator;

  while (it.hasNext) {
    final elem = it.next();
    final k = key(elem);
    final bldr = m.putIfAbsent(k, () => ListBuffer<B>());
    bldr.addOne(f(elem));
  }

  return IMap.fromDart(
    m.map((key, value) => MapEntry(key, value.toIList())),
  );
}

groupMapReduce() inherited

IMap<K, B> groupMapReduce<K, B>(
  K Function(Record) key,
  B Function(Record) f,
  B Function(B, B) reduce,
)

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
dart
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);
}

map() inherited

RIterable<B> map<B>(B Function(Record) f)

Returns a new collection by applying f to each element.

Inherited from RIterable.

Implementation
dart
@override
RIterable<B> map<B>(Function1<A, B> f) => views.Map(this, f);

mapValues()

IMap<K, W> mapValues<W>(W Function(V) f)

Applies f to each value in this map and returns a new map with the same keys, with the resulting values of the function application.

Implementation
dart
IMap<K, W> mapValues<W>(Function1<V, W> f) => from(iterator.map((kv) => (kv.$1, f(kv.$2))));

maxByOption() inherited

Option<Record> maxByOption<B>(B Function(Record) f, Order<B> order)

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
dart
Option<A> maxByOption<B>(Function1<A, B> f, Order<B> order) => _minMaxByOption(f, order.max);

maxOption() inherited

Option<Record> maxOption(Order<Record> order)

Finds the largest element in this collection according to the given Order.

If this collection is empty, None is returned.

Inherited from RIterableOnce.

Implementation
dart
Option<A> maxOption(Order<A> order) => switch (knownSize) {
  0 => none(),
  _ => _reduceOptionIterator(iterator, order.max),
};

minByOption() inherited

Option<Record> minByOption<B>(B Function(Record) f, Order<B> order)

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
dart
Option<A> minByOption<B>(Function1<A, B> f, Order<B> order) => _minMaxByOption(f, order.min);

minOption() inherited

Option<Record> minOption(Order<Record> order)

Finds the largest element in this collection according to the given Order.

If this collection is empty, None is returned.

Inherited from RIterableOnce.

Implementation
dart
Option<A> minOption(Order<A> order) => switch (knownSize) {
  0 => none(),
  _ => _reduceOptionIterator(iterator, order.min),
};

mkString() inherited

String mkString({String? start, String? sep, String? end})

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
dart
String mkString({String? start, String? sep, String? end}) {
  if (knownSize == 0) {
    return '${start ?? ""}${end ?? ""}';
  } else {
    return _mkStringImpl(StringBuffer(), start ?? '', sep ?? '', end ?? '');
  }
}

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

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:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This 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:

dart
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
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

partition() override

Record partition(bool Function(Record) p)

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
dart
@override
(IMap<K, V>, IMap<K, V>) partition(Function1<(K, V), bool> p) {
  final (first, second) = super.partition(p);
  return (IMap.from(first), IMap.from(second));
}

partitionMap() inherited

Record partitionMap<A1, A2>(Either<A1, A2> Function(Record) f)

Applies f to each element of this collection and returns a separate collection for all applications resulting in a Left and Right respectively.

Inherited from RIterable.

Implementation
dart
(RIterable<A1>, RIterable<A2>) partitionMap<A1, A2>(
  Function1<A, Either<A1, A2>> f,
) {
  final l = IList.builder<A1>();
  final r = IList.builder<A2>();

  iterator.foreach((x) {
    f(x).fold(
      (x1) => l.addOne(x1),
      (x2) => r.addOne(x2),
    );
  });

  return (l.toIList(), r.toIList());
}

reduce() inherited

Record reduce(Record Function(Record, Record) op)

Reduces this collection to a single value by applying op left to right.

Throws if the collection is empty.

Inherited from RIterableOnce.

Implementation
dart
A reduce(Function2<A, A, A> op) => reduceLeft(op);

reduceLeft() inherited

Record reduceLeft(Record Function(Record, Record) op)

Reduces from left to right. Throws if empty.

Inherited from RIterableOnce.

Implementation
dart
A reduceLeft(Function2<A, A, A> op) => switch (this) {
  final IndexedSeq<A> seq when seq.length > 0 => _foldl(seq, 1, seq[0], op),
  _ when knownSize == 0 => throw UnsupportedError('empty.reduceLeft'),
  _ => _reduceLeftIterator(() => throw UnsupportedError('empty.reduceLeft'), op),
};

reduceLeftOption() inherited

Option<Record> reduceLeftOption(Record Function(Record, Record) op)

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
dart
Option<A> reduceLeftOption(Function2<A, A, A> op) => switch (knownSize) {
  0 => none(),
  _ => _reduceOptionIterator(iterator, op),
};

reduceOption() inherited

Option<Record> reduceOption(Record Function(Record, Record) op)

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
dart
Option<A> reduceOption(Function2<A, A, A> op) => reduceLeftOption(op);

reduceRight() inherited

Record reduceRight(Record Function(Record, Record) op)

Reduces from right to left. Throws if empty.

Inherited from RIterableOnce.

Implementation
dart
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

Option<Record> reduceRightOption(Record Function(Record, Record) op)

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
dart
Option<A> reduceRightOption(Function2<A, A, A> op) => switch (knownSize) {
  -1 => _reduceOptionIterator(_reversed().iterator, (x, y) => op(y, x)),
  0 => none(),
  _ => Some(reduceRight(op)),
};

removed()

IMap<K, V> removed(K key)

Returns a new map with the value for given key removed.

Implementation
dart
IMap<K, V> removed(K key);

removedAll()

IMap<K, V> removedAll(RIterableOnce<K> keys)

Returns a new map with all the given keys removed.

Implementation
dart
IMap<K, V> removedAll(RIterableOnce<K> keys) =>
    keys.iterator.foldLeft(this, (acc, k) => acc.removed(k));

scan() inherited

RIterableOnce<B> scan<B>(B z, B Function(B, Record) op)

Alias for scanLeft.

Inherited from RIterableOnce.

Implementation
dart
RIterableOnce<B> scan<B>(B z, Function2<B, A, B> op) => scanLeft(z, op);

scanLeft() inherited

RIterable<B> scanLeft<B>(B z, B Function(B, Record) op)

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.

Inherited from RIterable.

Implementation
dart
@override
RIterable<B> scanLeft<B>(B z, Function2<B, A, B> op) => views.ScanLeft(this, z, op);

scanRight() inherited

RIterable<B> scanRight<B>(B z, B Function(Record, B) op)

Returns a new collection of running totals starting with z, traversing from right to left.

Inherited from RIterable.

Implementation
dart
RIterable<B> scanRight<B>(B z, Function2<A, B, B> op) {
  var acc = z;
  var scanned = IList.empty<B>().prepended(acc);

  _reversed().foreach((elem) {
    acc = op(elem, acc);
    scanned = scanned.prepended(acc);
  });

  return scanned;
}

slice() override

IMap<K, V> slice(int from, int until)

Returns a new collection containing elements in the range [from, until).

Implementation
dart
@override
IMap<K, V> slice(int from, int until) => IMap.from(super.slice(from, until));

sliding() override

RIterator<IMap<K, V>> sliding(int size, [int step = 1])

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
dart
@override
RIterator<IMap<K, V>> sliding(int size, [int step = 1]) =>
    super.sliding(size, step).map(IMap.from);

span() override

Record span(bool Function(Record) p)

Returns two collections: elements before and starting from the first element that does not satisfy p.

Implementation
dart
@override
(IMap<K, V>, IMap<K, V>) span(Function1<(K, V), bool> p) {
  final (first, second) = super.span(p);
  return (IMap.from(first), IMap.from(second));
}

splitAt() override

Record splitAt(int n)

Returns two collections: the first n elements and the remainder.

Implementation
dart
@override
(IMap<K, V>, IMap<K, V>) splitAt(int n) {
  final (first, second) = super.splitAt(n);
  return (IMap.from(first), IMap.from(second));
}

take() override

IMap<K, V> take(int n)

Returns a new collection containing only the first n elements.

Implementation
dart
@override
IMap<K, V> take(int n) => IMap.from(super.take(n));

takeRight() override

IMap<K, V> takeRight(int n)

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
dart
@override
IMap<K, V> takeRight(int n) => IMap.from(super.takeRight(n));

takeWhile() override

IMap<K, V> takeWhile(bool Function(Record) p)

Returns a new collection of leading elements that satisfy p.

Implementation
dart
@override
IMap<K, V> takeWhile(Function1<(K, V), bool> p) => IMap.from(super.takeWhile(p));

tapEach() override

IMap<K, V> tapEach<U>(U Function(Record) f)

Applies f to each element in this collection, discarding any results and returns this collection.

Implementation
dart
@override
IMap<K, V> tapEach<U>(Function1<(K, V), U> f) {
  foreach(f);
  return this;
}

toIList() inherited

IList<Record> toIList()

Returns an IList with the same elements as this collection.

Inherited from RIterableOnce.

Implementation
dart
IList<A> toIList() => IList.from(this);

toIndexedSeq() inherited

IndexedSeq<Record> toIndexedSeq()

Returns an IndexedSeq with the same elements as this collection.

Inherited from RIterableOnce.

Implementation
dart
IndexedSeq<A> toIndexedSeq() => IndexedSeq.from(this);

toISet() inherited

ISet<Record> toISet()

Returns an ISet with the same elements as this collection, duplicates removed.

Inherited from RIterableOnce.

Implementation
dart
ISet<A> toISet() => ISet.from(this);

toIVector() inherited

IVector<Record> toIVector()

Returns an IVector with the same elements as this collection.

Inherited from RIterableOnce.

Implementation
dart
IVector<A> toIVector() => IVector.from(this);

toList() inherited

List<Record> toList({bool growable = true})

Returns a new List with the same elements as this collection.

Inherited from RIterableOnce.

Implementation
dart
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());
  }
}

toMap() inherited

Map<K, V> toMap()

Returns a new Map containing the same key-value pairs.

Inherited from RMap.

Implementation
dart
Map<K, V> toMap() => Map.fromEntries(iterator.map((a) => MapEntry(a.$1, a.$2)).toList());

toSeq() inherited

RSeq<Record> toSeq()

Returns a RSeq with the same elements as this collection.

Inherited from RIterableOnce.

Implementation
dart
RSeq<A> toSeq() => RSeq.from(this);

toString() inherited

String toString()

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.

Inherited from Object.

Implementation
dart
external String toString();

transform()

IMap<K, W> transform<W>(W Function(K, V) f)

Return a new map where the keys and values are creating by applying f to every key-value pair in this map.

Implementation
dart
IMap<K, W> transform<W>(Function2<K, V, W> f) =>
    from(iterator.map((kv) => (kv.$1, f(kv.$1, kv.$2))));

unzip()

Record unzip()

Returns a tuple of 2 RIterables where the first item is the keys from this map, and the second is the corresponding values.

Implementation
dart
(RIterable<K>, RIterable<V>) unzip() {
  final (bldr1, bldr2) = (IList.builder<K>(), IList.builder<V>());

  iterator.foreach((kv) {
    bldr1.addOne(kv.$1);
    bldr2.addOne(kv.$2);
  });

  return (bldr1.toIList(), bldr2.toIList());
}

updated()

IMap<K, V> updated(K key, V value)

Returns a new map with the given key set to the given value.

Implementation
dart
IMap<K, V> updated(K key, V value);

updatedWith()

IMap<K, V> updatedWith(K key, Option<V> Function(Option<V>) remappingFunction)

Returns a new map with remappingFunction applied to the value for the given key in this map.

If remappingFunction returns None, the returned map will not have a value associated with the given key.

Implementation
dart
IMap<K, V> updatedWith(
  K key,
  Function1<Option<V>, Option<V>> remappingFunction,
) {
  final previousValue = get(key);

  return remappingFunction(previousValue).fold(
    () => previousValue.fold(() => this, (_) => removed(key)),
    (nexValue) => updated(key, nexValue),
  );
}

withDefault()

IMap<K, V> withDefault(V Function(K) f)

Returns a new map where key lookups will return f if the map doesn't contain a value for the corresponding key.

Implementation
dart
IMap<K, V> withDefault(Function1<K, V> f) => _WithDefault(this, f);

withDefaultValue()

IMap<K, V> withDefaultValue(V value)

Returns a new map where key lookups will return f if the map doesn't contain a value for the corresponding key.

Implementation
dart
IMap<K, V> withDefaultValue(V value) => _WithDefault(this, (_) => value);

zip() inherited

RIterable<Record> zip<B>(RIterableOnce<B> that)

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.

Inherited from RIterable.

Implementation
dart
RIterable<(A, B)> zip<B>(RIterableOnce<B> that) {
  return switch (that) {
    final RIterable<B> that => views.Zip(this, that),
    _ => RIterable.from(iterator.zip(that)),
  };
}

zipAll() inherited

RIterable<Record> zipAll<B>(RIterableOnce<B> that, Record thisElem, B thatElem)

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.

Inherited from RIterable.

Implementation
dart
RIterable<(A, B)> zipAll<B>(
  RIterableOnce<B> that,
  A thisElem,
  B thatElem,
) => views.ZipAll(this, that, thisElem, thatElem);

zipWithIndex() inherited

RIterable<Record> zipWithIndex()

Return a new collection with each element of this collection paired with it's respective index.

Inherited from RIterable.

Implementation
dart
RIterable<(A, int)> zipWithIndex() => views.ZipWithIndex(this);

Extension Methods

toIMap() extension

IMap<A, B> toIMap()

Creates a new IMap where element 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
dart
IMap<A, B> toIMap() => IMap.from(this);

unzip() extension

Record unzip()

Splits a collection of pairs into two separate collections.

Available on RIterable<A>, provided by the RibsIterableTuple2Ops<A, B> extension

Implementation
dart
(RIterable<A>, RIterable<B>) unzip() => (
  views.Map(this, (a) => a.$1),
  views.Map(this, (a) => a.$2),
);

Operators

operator +()

IMap<K, V> operator +(Record elem)

Returns a new map with the given value for the given key.

Implementation
dart
IMap<K, V> operator +((K, V) elem) => updated(elem.$1, elem.$2);

operator -()

IMap<K, V> operator -(K key)

Returns a new map with the value for given key removed.

Implementation
dart
IMap<K, V> operator -(K key) => removed(key);

operator ==() override

bool operator ==(Object other)

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 == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must 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
dart
@override
bool operator ==(Object other) =>
    identical(this, other) ||
    switch (other) {
      final IMap<K, dynamic> that =>
        size == that.size && forall((kv) => that.get(kv.$1) == Some(kv.$2)),
      _ => false,
    };

operator inherited

V operator [](K key)

Returns the value associated with the given key, or the defaultValue of this map (which could potentially throw).

Inherited from RMap.

Implementation
dart
V operator [](K key) => get(key).getOrElse(() => defaultValue(key));

Static Methods

builder()

IMapBuilder<K, V> builder<K, V>()

Returns a mutable builder that accumulates entries into an IMap.

Implementation
dart
static IMapBuilder<K, V> builder<K, V>() => IMapBuilder();

empty()

IMap<K, V> empty<K, V>()

Returns an empty IMap.

Implementation
dart
static IMap<K, V> empty<K, V>() => _EmptyMap();

from() override

IMap<K, V> from<K, V>(RIterableOnce<Record> elems)

Creates an IMap from any RIterableOnce of key-value pairs.

Returns elems directly when it is already an IMap.

Implementation
dart
static IMap<K, V> from<K, V>(RIterableOnce<(K, V)> elems) => switch (elems) {
  final _EmptyMap<K, V> m => m,
  final _Map1<K, V> m => m,
  final _Map2<K, V> m => m,
  final _Map3<K, V> m => m,
  final _Map4<K, V> m => m,
  final IHashMap<K, V> hs => hs,
  _ => IMapBuilder<K, V>().addAll(elems).result(),
};

fromDart() override

IMap<K, V> fromDart<K, V>(Map<K, V> m)

Creates an IMap from a Dart Map.

Implementation
dart
static IMap<K, V> fromDart<K, V>(Map<K, V> m) =>
    fromDartIterable(m.entries.map((e) => (e.key, e.value)));

fromDartIterable()

IMap<K, V> fromDartIterable<K, V>(Iterable<Record> elems)

Creates an IMap from a Dart Iterable of key-value pairs.

Implementation
dart
static IMap<K, V> fromDartIterable<K, V>(Iterable<(K, V)> elems) =>
    from(RIterator.fromDart(elems.iterator));