Added iterator version of all 3 funtions

This commit is contained in:
Bob Polis 2021-07-26 21:45:13 +02:00
parent 77c69b721f
commit acec279889

View File

@ -16,6 +16,8 @@
namespace sc {
// std::vector versions
template<class T, class M>
std::vector<M> map(const std::vector<T>& seq, std::function<M(T)> fun) {
std::vector<M> result;
@ -39,6 +41,31 @@ namespace sc {
return result;
}
// iterator versions
template<class T, class M, class FwdIter>
std::vector<M> map(FwdIter& begin, FwdIter& end, std::function<M(T)> fun) {
std::vector<M> result;
std::transform(begin, end, std::back_inserter(result), fun);
return result;
}
template<class T, class FwdIter>
std::vector<T> filter(FwdIter& begin, FwdIter& end, std::function<bool(T)> fun) {
std::vector<T> result;
std::copy_if(begin, end, std::back_inserter(result), fun);
return result;
}
template<class T, class R, class FwdIter>
R reduce(FwdIter& begin, FwdIter& end, R seed, std::function<R(T, R)> fun) {
R result {seed};
for (FwdIter it = begin; it != end; ++it) {
result = fun(*it, result);
}
return result;
}
}
#endif // _functools_H_