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

compress

Keeps the elements of an iterable wherever a parallel list of booleans is true.

Iterable<B> compress<B>(List<bool> selectors, Iterable<B> iterable) FxAsyncIterable<B> compressAsync<B>(List<bool> selectors, FxAsyncIterable<B> iterable)

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.

Related: 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