partition 1.0.1 copy "partition: ^1.0.1" to clipboard
partition: ^1.0.1 copied to clipboard

A library for partitioning iterables based on predicates. By default, partitioning happens lazily over the source iterable but eager partitioning can be used by setting the lazy parameter or using the [...]

Partition - split iterables based on predicates #

Pub Style: Pedantic FOSSA Status CI

Splits one list into two; no more, no less #

But how?

final source = Iterable<int>.generate(10);
final result = source.partition((i) => i.isEven);
print(result.matching); // [ 0, 2, 4, 6, 8 ]
print(result.nonMatching); // [ 1, 3, 5 ,7 ,9 ]

By default, partitioning will happen lazily. This means that every time you access either matching or nonMatching (even to check the length), it will iterate over the entire source iterable.

To partition eagerly (and only iterate over the source once):

final source = Iterable<int>.generate(10);
final result = source.partition((i) => i.isEven, lazy: false);
// alteratively: final result = source.partitionNow((i) => i.isEven);
print(result.matching); // [ 0, 2, 4, 6, 8 ]
print(result.nonMatching); // [ 1, 3, 5 ,7 ,9 ]

The PartitionResult that is returned is just a List<Iterable<T>> with a fixed length of 2, where the first element is an Iterable (or List if partitioning was eager) of all the elements that matched the predicate, and where the second iterable contains all the elements that did not match the predicate.

The PartitionResult will always have a length of 2, even if you tried to partition an empty source iterable or if nothing/everything matched the predicate.

License #

FOSSA Status

2
likes
130
pub points
53%
popularity

Publisher

verified publisheralexmeuer.com

A library for partitioning iterables based on predicates. By default, partitioning happens lazily over the source iterable but eager partitioning can be used by setting the lazy parameter or using the partionNow extension method.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

collection

More

Packages that depend on partition