flinq 0.4.0 flinq: ^0.4.0 copied to clipboard
Extended capabilities for collections. It's a bunch of shortcuts to avoid unnecesary boilerplate work with collections.
flinq #
Extended capabilities for collections. It's a bunch of shortcuts to avoid unnecesary boilerplate work with collections.
Getting Started #
This package will help you to reduce the amount of boilerplate code by adding folowing extensions for Iterable
s:
- getter
firstOrNull
and methodfirstOrNullWhere(bool test(T))
for getting first value, and if it will not be found returnsnull
- getter
lastOrNull
and methodlastOrNullWhere(bool test(T))
for getting last value, and if it will not be found returnsnull
- getter
singleOrNull
and methodsingleOrNullWhere(bool test(T))
for getting single value, and if it will not be found returnsnull
, and if there will be too many elements it'll throw theStateError
- method
mapList
, which maps collection and casts it toList
- getter
min
andmax
for getting minimal or maximal value from collection ofComparable
s - getter
sum
andaverage
for getting sum and average from collection ofnum
s - getter
distinct
which will returnList
with unique values in collection - method
union
which will returnList
with union of two collections with only unique values in resulting collection - method
intersection
which will returnList
with elements that contains both collections with only unique values in resulting collection - method
difference
which will returnList
with difference between two collections with only unique values in resulting collection
Examples #
Iterable
extension can be used like this:
Common #
- firstOrNull and firstOrNullWhere
final firstOrNull = [].firstOrNull; // null
// or
final firstOrNull = [3, 6, 2, 7, 9].firstOrNullWhere((item) => item > 10); // null
- lastOrNull and lastOrNullWhere
final lastOrNull = [].lastOrNull; // null
// or
final lastOrNull = [3, 6, 2, 7, 9].lastOrNullWhere((item) => item > 10); // null
- singleOrNull and singleOrNullWhere
final singleOrNull = [].singleOrNull; // null
// or
final singleOrNull = [3, 6, 2, 7, 9].singleOrNullWhere((item) => item > 3); // null
- mapList
List<double> mappedIterable = [3, 6, 2, 7, 9].mapList((item) => item.toDouble());
Comparable #
- min and minOrNull
final min = [3, 6, 2, 7, 9].min; // 2
// or
final min = [].minOrNull; // null
- max and maxOrNull
final max = [3, 6, 2, 7, 9].max; // 9
// or
final max = [].maxOrNull; // null
- group
final collection = [
Pet("rat", "Mike"),
Pet("dog", "Rex"),
Pet("cat", "Lucy"),
];
/*
<bool, List<Pet>>{
true: [
Pet("rat", "Mike"),
Pet("cat", "Lucy"),
],
false: [
Pet("dog", "Rex"),
],
}
*/
final group = collection.group((item) => item.name.endsWith('at'));
- groupMap
final collection = [
Pet("rat", "Mike"),
Pet("dog", "Rex"),
Pet("cat", "Lucy"),
];
/*
<bool, int>{
true: 2,
false: 1,
}
*/
final groupMapped = collection.groupMap(
(item) => item.name.endsWith('at'), (group) => group.length);
Math #
- sum
final sum = [3, 6, 2, 7, 9].sum; // 27
- average
final average = [1, 3, 5, 7, 4, 4].average; // 4
Set #
- distinct
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final distinctCollection = collectionOne.distinct; // [2, 5, 8]
- union
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final unitedCollection = collectionOne.union(collectionTwo); // [2, 5, 8, 1, 3, 7]
- intersection
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final intersectedCollection = collectionOne.intersection(collectionTwo); // [5]
- difference
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final differedCollection = collectionOne.difference(collectionTwo); // [2, 8]
// or
final differedCollection = collectionTwo.difference(collectionOne); // [1, 3, 7]
Milestones for next releases #
- Add
notNull
getter - Add
minWhere
method - Add
maxWhere
method - Add
minOrNullWhere
method - Add
maxOrNullWhere
method