Skip to content

Instantly share code, notes, and snippets.

@pfultz2
Created February 27, 2015 05:04
Show Gist options
  • Save pfultz2/0dfdd8204b10c0dd038e to your computer and use it in GitHub Desktop.
Save pfultz2/0dfdd8204b10c0dd038e to your computer and use it in GitHub Desktop.
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <tuple>
#include <fit/always.h>
#include <fit/capture.h>
#include <fit/identity.h>
#include <fit/fix.h>
#include <fit/function.h>
#include <fit/fuse.h>
#include <fit/each_arg.h>
#include <fit/infix.h>
#include <fit/conditional.h>
#include <fit/compose.h>
#include <fit/placeholders.h>
namespace adl {
using std::begin;
using std::end;
template<class R>
auto adl_begin(R&& r) -> decltype(begin(r));
template<class R>
auto adl_end(R&& r) -> decltype(end(r));
}
FIT_STATIC_FUNCTION(for_each_tuple) = [](auto&& tuple, auto f) FIT_RETURNS
(fit::fuse(fit::capture(f)(fit::each_arg))(tuple));
// FIT_STATIC_FUNCTION(print) = fit::conditional(
// [](const auto& x) -> decltype(std::cout << x, void())
// {
// std::cout << x << std::endl;
// },
// [](const auto& range) -> decltype(std::cout << *adl::adl_begin(range), void())
// {
// for(const auto& x:range) std::cout << x << std::endl;
// },
// [](const auto& tuple) -> decltype(for_each_tuple(tuple, fit::identity), void())
// {
// return for_each_tuple(tuple, [](const auto& x)
// {
// std::cout << x << std::endl;
// });
// }
// );
// FIT_STATIC_FUNCTION(print) = fit::fix(fit::conditional(
// [](auto, const auto& x) -> decltype(std::cout << x, void())
// {
// std::cout << x << std::endl;
// },
// [](auto self, const auto& range) -> decltype(self(*adl::adl_begin(range)), void())
// {
// for(const auto& x:range) self(x);
// },
// [](auto self, const auto& tuple) -> decltype(for_each_tuple(tuple, self), void())
// {
// return for_each_tuple(tuple, self);
// }
// ));
FIT_STATIC_FUNCTION(simple_print) = fit::fix(fit::conditional(
[](auto, const auto& x) -> decltype(std::cout << x, void())
{
std::cout << x << std::endl;
},
[](auto self, const auto& range) -> decltype(self(*adl::adl_begin(range)), void())
{
for(const auto& x:range) self(x);
},
[](auto self, const auto& tuple) -> decltype(for_each_tuple(tuple, self), void())
{
return for_each_tuple(tuple, self);
}
));
const constexpr auto print = fit::capture(simple_print)(fit::each_arg);
int main()
{
print(5, "Hello world");
print(5);
std::vector<int> v = { 1, 2, 3, 4 };
print(v);
auto t = std::make_tuple(1, 2, 3, 4);
print(t);
auto m = std::make_tuple(3, v, t);
print(m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment