#ifndef _functools_H_ #define _functools_H_ #include #include #include #include namespace sc { // std::vector versions template std::vector map(const std::vector& seq, std::function fun) { std::vector result; std::transform(seq.begin(), seq.end(), std::back_inserter(result), fun); return result; } template std::vector filter(const std::vector& seq, std::function fun) { std::vector result; std::copy_if(seq.begin(), seq.end(), std::back_inserter(result), fun); return result; } template R reduce(const std::vector& seq, R seed, std::function fun) { R result {seed}; for (const auto& elem : seq) { result = fun(elem, result); } 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_