Skip to content

Instantly share code, notes, and snippets.

@autosquid
Last active November 9, 2016 13:11
Show Gist options
  • Save autosquid/e14d954aaa5a2fc8deb4 to your computer and use it in GitHub Desktop.
Save autosquid/e14d954aaa5a2fc8deb4 to your computer and use it in GitHub Desktop.
my functional
// I hate STL transform.
// I hate iteraters
namespace my{
template<typename T, typename F>
auto map(F f, const std::vector<T> &a ) {
std::vector<std::result_of_t<F(T)>> result;
for(const T & t : a)
result.push_back(f(t));
return result;
}
template<typename Dst_container_t, typename Src_container_t>
Dst_container_t transform(const Src_container_t& _src, std::function<typename Dst_container_t::value_type (typename Src_container_t::const_reference)> F){
Dst_container_t dst(_src.size()); // todo: to do tricks;
std::transform(_src.begin(), _src.end(), dst.begin(), F);
return std::move(dst);
}
inline std::vector<std::string> tokenize( const std::string str, const std::regex regex ){
using namespace std;
vector<string> result;
sregex_token_iterator it( str.begin(), str.end(), regex, -1 );
sregex_token_iterator reg_end;
for ( ; it != reg_end; ++it ) {
if ( !it->str().empty() ) //token could be empty:check
result.emplace_back( it->str() );
}
return std::move(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment