combine method

Multiset<E> combine(
  1. Iterable<E> other,
  2. int callback(
    1. E element,
    2. int a,
    3. int b
    )
)

Returns a new Multiset by evaluating callback element-wise for each distinct element of this and other.

Throws an ArgumentError if callback returns a negative count.

Implementation

Multiset<E> combine(
  Iterable<E> other,
  int Function(E element, int a, int b) callback,
) {
  if (other is Multiset<E> && other._isIdentity == _isIdentity) {
    final result = _createEmpty();
    for (final entry in _container.entries) {
      result.add(
        entry.key,
        callback(entry.key, entry.value, other[entry.key]),
      );
    }
    for (final entry in other._container.entries) {
      if (!_container.containsKey(entry.key)) {
        result.add(entry.key, callback(entry.key, 0, entry.value));
      }
    }
    return result;
  } else {
    final otherSet = _createEmpty()..addAll(other);
    return combine(otherSet, callback);
  }
}