Map is a functional operator where a function will be applied to a list. Filter will apply a predicate to a list and return another list. They are the cornerstone of any functional processing pipeline. They are also called higher-order functions. We can write a generic Map function, using std::transform
for std::list
and the std::vector
:
template <typename R, typename F> R Map(R r , F&& fn) { std::transform(std::begin(r), std::end(r), std::begin(r), std::forward<F>(fn)); return r; }
Let's also write a function to filter a std::list
(we assume only a list will be passed). The same can work on std::vector
. We can compose a higher-order function using the pipe operator. The composite function can also be passed as a predicate:
template <typename R, typename F> R Filter( R r , F&& fn ) { R ret(r.size()); auto first = std::begin(r), last = std::end(r) , result = std::begin(ret); bool...