distinctByTo<K> method

Iterable<E> distinctByTo<K>(
  1. List<E> destination,
  2. K selector(
    1. E element
    )
)

Populates and returns the destination list with containing only elements from the given iterable having distinct keys returned by the given selector function.

Implementation

Iterable<E> distinctByTo<K>(
    List<E> destination, K Function(E element) selector) {
  final set = HashSet<K>();
  for (var element in this) {
    final key = selector(element);
    if (set.add(key)) destination.add(element);
  }
  return destination;
}