Created
March 5, 2016 15:54
-
-
Save KrzaQ/0e078cb1b172bb11ecfd to your computer and use it in GitHub Desktop.
This file contains 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 <cassert> | |
#include <climits> | |
#include <cstdlib> | |
#include <cstring> | |
#include <cctype> | |
#include <algorithm> | |
#include <array> | |
#include <atomic> | |
#include <chrono> | |
#include <deque> | |
#include <fstream> | |
#include <iomanip> | |
#include <iostream> | |
#include <iterator> | |
#include <list> | |
#include <map> | |
#include <memory> | |
#include <mutex> | |
#include <random> | |
#include <regex> | |
#include <set> | |
#include <sstream> | |
#include <string> | |
#include <utility> | |
#include <vector> | |
#include <thread> | |
#include <type_traits> | |
#include <typeinfo> | |
#include <tuple> | |
#include <unordered_set> | |
using namespace std; | |
constexpr int Width = 60; | |
#define DBG(x) { cout << left << setw(Width) << #x << boolalpha << (x) << endl; } | |
#define DBG_CONT(x) { cout << left << setw(Width) << #x; for(auto const& _name : (x)) \ | |
cout << boolalpha << _name << " "; cout << endl; } | |
#define BARK cout << __PRETTY_FUNCTION__ << endl; | |
namespace kq{ | |
template<typename T> | |
struct UfcsFunc | |
{ | |
T func; | |
}; | |
template<typename Func, typename... Args> | |
auto ufcs(Func&& f, Args&&... args){ | |
auto lambda = [&](auto&& param){ | |
return f(std::forward<decltype(param)>(param), std::forward<Args>(args)...); | |
}; | |
return UfcsFunc<decltype(lambda)>{ lambda }; | |
} | |
struct UfcsFuncTag{}; | |
struct CallableTag{}; | |
template<typename T> struct DetectFunc{ using Tag = CallableTag; }; | |
template<typename T> struct DetectFunc<UfcsFunc<T>> { using Tag = UfcsFuncTag; }; | |
template<typename T, typename U> | |
auto call(UfcsFunc<T>&& f, U&& u, UfcsFuncTag){ | |
return f.func(std::forward<U>(u)); | |
} | |
template<typename T, typename U> | |
auto call(T&& t, U&& u, CallableTag){ | |
return t(std::forward<U>(u)); | |
} | |
template<typename T> | |
auto toUfcs(T&&); | |
struct UfcsEnd{}; | |
template<typename T> | |
struct UfcsHelper | |
{ | |
UfcsHelper(T&& value) : value(std::forward<T>(value)) {} | |
template<typename Func> | |
auto operator|(Func&& func){ | |
using Tag = typename DetectFunc<Func>::Tag; | |
return toUfcs(call(std::forward<Func>(func), this->get(), Tag{})); | |
} | |
auto operator|(UfcsEnd){ | |
return get(); | |
} | |
// operator T(){ | |
// return value; | |
// } | |
private: | |
auto get(){ | |
return std::forward<T>(value); | |
} | |
T value; | |
}; | |
template<typename T> | |
auto toUfcs(T&& t){ | |
return UfcsHelper<T>{ std::forward<T>(t) }; | |
} | |
template<typename T, typename U> | |
auto scast(U&& u){ | |
return static_cast<T>(u); | |
} | |
} | |
string reverse(string const& s){ | |
return string(s.crbegin(), s.crend()); | |
} | |
string toUpper(string const& s){ | |
string ret(s.size(), '0'); | |
transform(s.cbegin(), s.cend(), ret.begin(), ::toupper); | |
return ret; | |
} | |
auto main() -> int | |
{ | |
string s = "Ala ma kota"; | |
char c = 'x'; | |
// auto c2 = kq::toUfcs(c) | |
// | kq::ufcs(::toupper); | |
// | kq::ufcs(&::kq::scast<char, int>) | |
// | kq::UfcsEnd{}; | |
// DBG(c2); | |
auto s2 = kq::toUfcs(s) | |
| &::reverse | |
| kq::ufcs(&toUpper) | |
| kq::UfcsEnd{}; | |
DBG(s2); | |
string num = "1a"; | |
auto ret = kq::toUfcs(num) | |
| kq::ufcs(static_cast<int(*)(string const&, size_t*, int)>(&::stoi), nullptr, 11) | |
| kq::UfcsEnd{}; | |
DBG(ret); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment