diff --git a/list.cpp b/list.cpp index b83b018..caa4fbc 100644 --- a/list.cpp +++ b/list.cpp @@ -75,6 +75,16 @@ T reduce(List *lst, T (*reducer) (T, T), T init = 0) return reduce(lst->next, reducer, init); } +template +List *filter(List *lst, bool (*f)(T), List *new_lst = nullptr) +{ + if (!lst) + return new_lst; + if (f(lst->value)) + new_lst = append(new_lst, lst->value); + return filter(lst->next, f, new_lst); +} + template std::ostream& operator<<(std::ostream& ostream, const List *lst) {