compress
Keeps the elements of an iterable wherever a parallel list of booleans is true.
Lecture
compress is a positional mask: element i of
iterable survives only if selectors[i] is
true. It's built directly out of two functions you already
know — map((r) => r.$2, filter((r) => r.$1, zip(selectors, iterable))) —
zip the mask with the data, filter to the true pairs, then unwrap. That
also means it inherits zip's behavior on mismatched
lengths: iteration stops as soon as the shorter of
selectors or iterable runs out, so a short
selector list silently truncates the result. Reach for it when you
already have (or can cheaply compute) a boolean mask up front — for
example, "which quiz answers were correct" — rather than recomputing a
predicate per element the way filter would.
There's no chain method; call the data-first function or its async
counterpart directly, or wrap the result with fx(...) /
fxAsync(...) to keep chaining.
Demo 1 · Basics & length mismatch
Demo 2 · Async, with concurrency
Try it yourself
Exercise: use compress to keep only the correct
answers.
filter — filter by a predicate instead of a precomputed mask ·
zip — the function compress is built on ·
differenceBy — filter by membership against another iterable ·
partition — split into kept/rejected in one pass