uniqStrict
Dedupes the whole iterable immediately and returns a List — the eager counterpart of distinct.
Lecture
uniqStrict produces exactly the same elements, in the same
order, as distinct followed by
toList(). What differs is when the work happens and
who can stop it. uniqByStrict is the same deal for
distinctBy: dedupe on a computed
key, eagerly.
A lazy chain re-runs its upstream on every iteration. Iterate
distinct(...) twice and the source is walked twice. The strict
form walks it once, at the call, and hands you a List — so a
result you are going to index, measure, or scan more than once costs you
one pass instead of n. Demo 2 counts the callbacks to make this
concrete.
The price is that nothing downstream can cut the work short.
distinct(xs).take(3) stops pulling xs as soon as
3 distinct values have appeared; uniqStrict(xs).take(3) dedupes
all of xs first and then takes 3. Never put the strict form
ahead of a short-circuiting consumer, and never point it at an unbounded
iterable — it will not terminate.
distinct(...).toList()
already runs the dedupe and the accumulation as a single pass, so it is not
paying for laziness. Reach for uniqStrict only when the deduped
List is itself the thing you want, or when it is iterated more
than once.
Demo 1 · Basics
Demo 2 · When it pays, and what it costs
Try it yourself
Exercise: use uniqByStrict to keep each visitor's first
visit, as a List you can index without iterating again.
distinct — the lazy default ·
distinctBy — dedupe by a computed key ·
uniqAdjacent — drop only adjacent duplicates ·
toList — materialize any chain