(list)+filter function

This generates a new list, and appends to it every time the current
list value satisfies the condition.  It then recursively calls the
filter on the next value.
This commit is contained in:
2021-11-22 06:08:52 +00:00
parent 1d91eb52b6
commit ab5d1c6997

View File

@@ -75,6 +75,16 @@ T reduce(List<T> *lst, T (*reducer) (T, T), T init = 0)
return reduce(lst->next, reducer, init);
}
template <typename T>
List<T> *filter(List<T> *lst, bool (*f)(T), List<T> *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 <typename T>
std::ostream& operator<<(std::ostream& ostream, const List<T> *lst)
{