intersect method

Iterable<E> intersect(
  1. Iterable<E> other
)

Returns a new lazy Iterable containing all elements that are contained by both this collection and the other collection.

The returned collection preserves the element iteration order of the this collection.

Implementation

Iterable<E> intersect(Iterable<E> other) sync* {
  var second = HashSet<E>.from(other);
  var output = HashSet<E>();
  for (var current in this) {
    if (second.contains(current)) {
      if (output.add(current)) {
        yield current;
      }
    }
  }
}