Эта страница ещё не переведена, поэтому показана на английском. Помогите с переводом

distinctBy

Removes duplicates as determined by a key function, rather than value equality.

Iterable<A> distinctBy<A, B>(B Function(A a) f, Iterable<A> iterable) FxAsyncIterable<A> distinctByAsync<A, B>(FutureOr<B> Function(A a) f, FxAsyncIterable<A> iterable) Fx<T> Fx.distinctBy<B>(B Function(T a) f) // chain FxAsync<T> FxAsync.distinctBy<B>(FutureOr<B> Function(T a) f) Iterable<A> uniqBy<A, B>(B Function(A a) f, Iterable<A> iterable) // FxTS alias

Lecture

distinctBy is uniq's general form: instead of comparing whole elements, it runs each element through f and keeps only the first element to produce each distinct key. distinctBy is the Dart-idiomatic name; fxdart also accepts the FxTS spelling uniqBy — they're the same operator. It's the tool for "one row per customer," "one event per type," or dedupe by any field or computed value — uniq itself is just distinctBy((a) => a, iterable).

As with uniq, it's lazy and order-preserving: the first element for each key wins, and the internal Set<B> of seen keys only grows as you pull.

The same async rule applies here as with uniq: put the concurrency in an upstream fetch (.map(...).concurrent(n)), then apply distinctBy/distinctByAsync to the already-resolved, in-order stream.

Demo 1 · Basics

Demo 2 · Async, with concurrency upstream

Try it yourself

Exercise: use distinctBy to keep only the first event of each 'type'.

Related: uniq — dedupe by value equality · differenceBy — remove by a computed key against another iterable · intersectionBy — keep by a computed key shared with another iterable · groupBy — keep every element, grouped by key