Extn: Add SplitMatch extn method for lists

This commit is contained in:
Neeraj Gupta 2022-11-14 12:54:36 +05:30
parent 51d0ee0d20
commit eaeee9959d
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1

View file

@ -8,4 +8,24 @@ extension ListExtension<E> on List<E> {
}
return result;
}
// splitMatch, based on the matchFunction, split the input list in two
// lists. result.matched contains items which matched and result.unmatched
// contains remaining items.
ListMatch<E> splitMatch(bool Function(E element) matchFunction) {
final listMatch = ListMatch<E>();
for (final element in this) {
if (matchFunction(element)) {
listMatch.matched.add(element);
} else {
listMatch.unmatched.add(element);
}
}
return listMatch;
}
}
class ListMatch<T> {
List<T> matched = <T>[];
List<T> unmatched = <T>[];
}