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

partition

Splits a pipeline into two lists at once, by a single predicate.

(List<A>, List<A>) partition<A>(bool Function(A a) f, Iterable<A> iterable) Future<(List<A>, List<A>)> partitionAsync<A>(FutureOr<bool> Function(A a) f, FxAsyncIterable<A> iterable) (List<T>, List<T>) Fx.partition(bool Function(T a) f) Future<(List<T>, List<T>)> FxAsync.partition(FutureOr<bool> Function(T a) f)

Lecture

partition is a terminal operator that walks the pipeline once and sorts every element into one of two lists based on a predicate: elements the predicate returns true for go into the first list, everything else into the second. It's equivalent to calling filter and reject separately, but in a single pass over the data.

FxTS returns a two-element tuple, [pass, fail]. Dart has no tuple type built into JS-style arrays, so FxDart uses a native Dart record: (List<A>, List<A>). Access the two lists with .$1 (pass) and .$2 (fail), or destructure them directly with pattern-matching syntax: final (pass, fail) = partition(f, iterable);. This is the same tuple-to-record convention used by zip and entries elsewhere in FxDart.

As with every terminal in this section, it pulls the whole lazy pipeline upstream of it, sync or async.

Demo 1 · Basics & destructuring

Demo 2 · Async

Try it yourself

Exercise: partition the scores into pass (>= 60) and fail (< 60).

Related: filter · reject — the two halves partition combines · groupBy — grouping into more than two buckets · sort — order within each side, if you need it