Skip to content

Instantly share code, notes, and snippets.

View lcapaldo's full-sized avatar

Logan Capaldo lcapaldo

View GitHub Profile
package main
import (
"github.com/golang/protobuf/proto"
"github.com/lightstep/lightstep-tracer-go/collectorpb"
"io/ioutil"
"log"
"net/http"
"os"
"time"
@lcapaldo
lcapaldo / pdt.cpp
Created December 30, 2012 22:44
(Pseudo-) Path Dependent types, C++11 style.
include <set>
#include <assert.h>
template<typename Unutterable>
struct Board
{
int length;
int height;
struct Coordinate
@lcapaldo
lcapaldo / gist:2065956
Created March 17, 2012 22:37
State Monad in C++11
namespace hask {
struct Unit { };
template<typename S>
struct Stateful {
template<typename V>
struct Value
{
std::function<std::pair<S,V>(S)> runState;
};
};
struct counter : resource { int c; } r;
counter r2;
std::function<bool(int&)> is7 = [](int& x) -> bool { return x == 7; };
r2.with_do([&]() {
if( r2.c != 7 )
{
std::cout << "Waiting for c to become 7.\n";
}
r.await(std::bind(is7, std::ref(r2.c))); // r != r2
void await(std::function<bool()> cond)
{
with_when_do(cond, []() { });
}
r.await( [&r]() { return r.c != 0; });
class resource {
recursive_mutex m_;
condition_variable cv_;
public:
resource() {}
void with_do(std::function<void()> body)
{
m_.lock();
at_scope_exit u( [&]() { cv_.notify_all(); m_.unlock(); } );
class resource {
recursive_mutex m_;
condition_variable cv_;
public:
resource() {}
void with_do(std::function<void()> body)
{
m_.lock();
at_scope_exit u( [&]() { cv_.notify_all(); m_.unlock(); } );
auto incr = [&]() { r.c += 1; };
r.with_do([&]()
{
r.c += 1;
r.with_do(incr);
});
r.with_do([&]()
{
r.c += 1;
counter& shadow = r;
r.with_do([&]() { shadow.c += 1; });
});