Skip to content

Instantly share code, notes, and snippets.

View enobayram's full-sized avatar

Enis Bayramoğlu enobayram

View GitHub Profile
@enobayram
enobayram / maybe.c
Created May 21, 2017 15:57
Maybe monad in C
#include <stdlib.h>
#include <stdio.h>
// return :: Monad m => a -> m a
typedef void * /* m a */ return_t(void * /* a */);
// type kleisli_arrow a b = a -> m b
typedef void * /* m b */ kleisli_arrow(void * /* a */);
// bind :: Monad m => m a -> (a -> m b) -> m b
// We create a global char for each type
// But we actually care about its address to be used as a constexpr token
template <class T> char token;
using type_token = const void *;
// A utility to get a list of tokens for a list of types
template <class ... Elems> constexpr type_token tokens[sizeof...(Elems)] = {&token<Elems>...};
// A utility template to pass around a list of types as a value
template <class ... T> struct list{};
#include <string>
#include <stdio.h>
template<class Func>
struct overload_base {
Func f;
template <class ... T>
friend decltype(f(std::declval<T>()...)) call(overload_base * b, T ... in) {
return b->f(in...);
}
#include <functional>
#include <list>
#include <boost/any.hpp>
using boost::any;
using std::list;
// list_cast :: list<any> -> list<T>
template<class T>