partition
Splits a pipeline into two lists at once, by a single predicate.
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).