Skip to content

Instantly share code, notes, and snippets.

@bradphelan
Created January 29, 2020 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradphelan/da494160adb32138b46aba4ed3fff967 to your computer and use it in GitHub Desktop.
Save bradphelan/da494160adb32138b46aba4ed3fff967 to your computer and use it in GitHub Desktop.
#include "boost/type_traits/declval.hpp"
#include "boost/foreach.hpp"
#include <vector>
template <int N, typename T>
struct NVector{
typedef std::vector<typename NVector<N-1,T>::type> type;
};
template <typename T> struct NVector<1,T> {
typedef std::vector<T> type;
};
template <typename T, typename Mapper>
struct MapResult {
typedef decltype(boost::declval<Mapper>()(boost::declval<T>())) type;
};
template <typename T, typename Mapper>
struct MapResult<std::vector<T>, Mapper> {
typedef std::vector<typename MapResult<T, Mapper>::type> type;
};
template <typename T, typename Mapper>
typename MapResult<T, Mapper>::type Map(T const& elem, Mapper&& mapper)
{
return mapper(elem);
}
template <typename T, typename Mapper>
typename MapResult<std::vector<T>, Mapper>::type Map(std::vector<T> const& vector, Mapper&& mapper)
{
MapResult<std::vector<T>, Mapper>::type out;
out.reserve(vector.size());
BOOST_FOREACH(auto& v , vector)
out.push_back(Map(v, mapper));
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment