diff --git a/functools.hpp b/functools.hpp index 138abf1..c82214b 100644 --- a/functools.hpp +++ b/functools.hpp @@ -16,6 +16,8 @@ namespace sc { + // std::vector versions + template std::vector map(const std::vector& seq, std::function fun) { std::vector result; @@ -39,6 +41,31 @@ namespace sc { return result; } + // iterator versions + + template + std::vector map(FwdIter& begin, FwdIter& end, std::function fun) { + std::vector result; + std::transform(begin, end, std::back_inserter(result), fun); + return result; + } + + template + std::vector filter(FwdIter& begin, FwdIter& end, std::function fun) { + std::vector result; + std::copy_if(begin, end, std::back_inserter(result), fun); + return result; + } + + template + R reduce(FwdIter& begin, FwdIter& end, R seed, std::function fun) { + R result {seed}; + for (FwdIter it = begin; it != end; ++it) { + result = fun(*it, result); + } + return result; + } + } #endif // _functools_H_