Added iterator version of all 3 funtions
This commit is contained in:
parent
77c69b721f
commit
acec279889
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
namespace sc {
|
namespace sc {
|
||||||
|
|
||||||
|
// std::vector versions
|
||||||
|
|
||||||
template<class T, class M>
|
template<class T, class M>
|
||||||
std::vector<M> map(const std::vector<T>& seq, std::function<M(T)> fun) {
|
std::vector<M> map(const std::vector<T>& seq, std::function<M(T)> fun) {
|
||||||
std::vector<M> result;
|
std::vector<M> result;
|
||||||
@ -39,6 +41,31 @@ namespace sc {
|
|||||||
return result;
|
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_
|
#endif // _functools_H_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user