Last active
December 20, 2020 10:11
-
-
Save KaiserKatze/74246201c0ccaff22ab5840f62401237 to your computer and use it in GitHub Desktop.
Python-like functional functions implemented in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> | |
template <class _StrTyp, class _SeqTyp, class... _Arg> | |
_StrTyp join(_StrTyp&& seperator, _SeqTyp&& sequence, _Arg&&... args) | |
{ | |
using _Elem = typename _StrTyp::value_type; | |
using _Traits = typename _StrTyp::traits_type; | |
using _Alloc = typename _StrTyp::allocator_type; | |
std::basic_stringstream<_Elem, _Traits, _Alloc> oss; | |
((oss << args), ...); | |
auto it = std::begin(sequence); | |
oss << *it++; | |
while (it != std::end(sequence)) | |
oss << seperator << *it++; | |
return oss.str(); | |
} | |
#include <iostream> | |
int main() | |
{ | |
std::cout << join( | |
std::string{ "-" }, std::vector<int>{101, 202, 303, 404}, | |
std::showbase, std::hex | |
) << std::endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iterator> | |
#include <algorithm> | |
#include <functional> | |
template <class _Map> | |
std::vector<typename _Map::key_type> keyset(const _Map& map) | |
{ | |
std::vector<typename _Map::key_type> result; | |
result.reserve(map.size()); | |
std::transform(map.cbegin(), map.cend(), std::back_inserter(result), [](typename _Map::const_reference kvpair) { | |
return kvpair.first; | |
}); | |
return result; | |
} | |
template <class _Map> | |
std::vector<typename _Map::mapped_type> valueset(const _Map& map) | |
{ | |
std::vector<typename _Map::mapped_type> result; | |
result.reserve(map.size()); | |
std::transform(map.cbegin(), map.cend(), std::back_inserter(result), [](typename _Map::const_reference kvpair) { | |
return kvpair.second; | |
}); | |
return result; | |
} | |
template <class _Map> | |
std::vector<std::reference_wrapper<typename _Map::mapped_type>> valueset(_Map& map) | |
{ | |
std::vector<std::reference_wrapper<typename _Map::mapped_type>> result; | |
result.reserve(map.size()); | |
std::transform(map.begin(), map.end(), std::back_inserter(result), [](typename _Map::reference kvpair) { | |
return std::ref(kvpair.second); | |
}); | |
return result; | |
} | |
#include <iostream> | |
int main() | |
{ | |
std::map<int, double> map{ | |
{1, 9.0}, | |
{2, 9.9}, | |
{3, 9.99}, | |
{4, 9.999}, | |
}; | |
auto ks = keyset(map); | |
auto vs = valueset(map); | |
for (auto& k : ks) std::cout << k << '\n'; | |
std::cout << "------------------\n"; | |
for (auto& v : vs) std::cout << v << '\n'; | |
for (auto& v : vs) v += 100.0; | |
std::cout << "------------------\n"; | |
for (auto& v : vs) std::cout << v << '\n'; | |
std::cout << "------------------\n"; | |
for (auto& [k, v] : map) std::cout << v << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment