Iterable and Map extensions for groupBy, chunk, zip, partition, sliding, frequencies, and more
dart pub add philiprehberger_collection_extIterable and Map extensions for groupBy, chunk, zip, partition, sliding, frequencies, and more
Add to your pubspec.yaml:
dependencies:
philiprehberger_collection_ext: ^0.6.0
Then run:
dart pub get
import 'package:philiprehberger_collection_ext/philiprehberger_collection_ext.dart';
final grouped = [1, 2, 3, 4, 5, 6].groupBy((n) => n.isEven ? 'even' : 'odd');
// {odd: [1, 3, 5], even: [2, 4, 6]}
final chunks = [1, 2, 3, 4, 5].chunk(2);
// [[1, 2], [3, 4], [5]]
final pairs = [1, 2, 3].zip(['a', 'b', 'c']).toList();
// [(1, 'a'), (2, 'b'), (3, 'c')]
final unique = [1, 2, 3, 2, 1].distinctBy((n) => n).toList();
// [1, 2, 3]
final (evens, odds) = [1, 2, 3, 4, 5].partition((n) => n.isEven);
// evens: [2, 4], odds: [1, 3, 5]
final result = [1, 2, 3].intersperse(0).toList();
// [1, 0, 2, 0, 3]
final windows = [1, 2, 3, 4].sliding(2).toList();
// [[1, 2], [2, 3], [3, 4]]
final stepped = [1, 2, 3, 4].sliding(2, step: 2).toList();
// [[1, 2], [3, 4]]
final total = [1, 2, 3].sumBy((n) => n); // 6
final avg = [2, 4, 6].averageBy((n) => n); // 4.0
final counts = ['a', 'b', 'a', 'c', 'a'].frequencies();
// {a: 3, b: 1, c: 1}
final byInitial = ['apple', 'banana', 'cherry'].associateBy((s) => s[0]);
// {a: apple, b: banana, c: cherry}
final labeled = ['a', 'b', 'c'].mapIndexed((i, e) => '$i:$e').toList();
// ['0:a', '1:b', '2:c']
final evens = [10, 20, 30, 40].whereIndexed((i, e) => i.isEven).toList();
// [10, 30]
final flat = [[1, 2], [3, 4]].flatMap((e) => e).toList();
// [1, 2, 3, 4]
final allOdd = [1, 3, 5].none((n) => n.isEven); // true
final items = [1, 2, 3, 4, 5].takeWhileInclusive((n) => n < 3).toList();
// [1, 2, 3]
final running = [1, 2, 3, 4].scan(0, (acc, n) => acc + n).toList();
// [1, 3, 6, 10]
final runs = [1, 1, 2, 2, 2, 3, 1, 1].chunkWhile((a, b) => a == b).toList();
// [[1, 1], [2, 2, 2], [3], [1, 1]]
final filtered = {'a': 1, 'b': 2, 'c': 3}.filterValues((v) => v > 1);
// {b: 2, c: 3}
final uppered = {'a': 1, 'b': 2}.mapKeys((k) => k.toUpperCase());
// {A: 1, B: 2}
final doubled = {'a': 1, 'b': 2}.mapValues((v) => v * 10);
// {a: 10, b: 20}
| Method | Description |
|---|---|
groupBy(key) | Group elements by a key function |
countBy(key) | Count elements by a key function |
chunk(size) | Split into chunks of the given size |
distinctBy(key) | Remove duplicates by a key function |
minBy(key) | Find element with minimum key value |
maxBy(key) | Find element with maximum key value |
zip(other) | Pair elements with another iterable |
firstWhereOrNull(test) | Safe lookup returning null instead of throwing |
sortedBy(key) | Return a sorted copy by a key function |
partition(predicate) | Split elements into matching and non-matching lists |
intersperse(separator) | Insert a separator between each pair of elements |
sliding(size, step) | Overlapping windows of elements |
sumBy(selector) | Sum of values by a selector function |
averageBy(selector) | Average of values by a selector function |
frequencies() | Count occurrences of each element |
associateBy(keySelector) | Create a map from elements using a key selector |
mapIndexed(transform) | Transform elements with access to their index |
whereIndexed(test) | Filter elements with access to their index |
flatMap(transform) | Map each element to an iterable and flatten |
none(test) | Returns true if no element satisfies the predicate |
takeWhileInclusive(test) | Like takeWhile but includes the first failing element |
scan(initial, combine) | Running fold that emits each intermediate accumulator value |
chunkWhile(test) | Group consecutive elements while predicate holds between adjacent pairs |
| Method | Description |
|---|---|
filterKeys(test) | Filter entries by key predicate |
filterValues(test) | Filter entries by value predicate |
mapKeys(transform) | Transform keys while keeping values |
mapValues(transform) | Transform values while keeping keys |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: