From acec2798899f2ccde0dacc07ca4519bf21249630 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Mon, 26 Jul 2021 21:45:13 +0200 Subject: [PATCH] Added iterator version of all 3 funtions --- functools.hpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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_