Skip to content

Instantly share code, notes, and snippets.

@axross
Created March 29, 2018 10:27
Show Gist options
  • Save axross/fbab2c5cd1d5f05126f958b41050021f to your computer and use it in GitHub Desktop.
Save axross/fbab2c5cd1d5f05126f958b41050021f to your computer and use it in GitHub Desktop.
typedef bool Predicate(T);
List<T> filter<T>(List<T> list, Predicate predicate) {
final newList = [];
for (final item in list) {
if (predicate(item)) {
newList.add(item);
}
}
return newList;
}
void main() {
final filtered = filter([1, 2, 3, 4, 5], (item) => item % 2 == 1);
assert(filtered.length == 3);
print(filtered);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment