Skip to content

Instantly share code, notes, and snippets.

View cdhowie's full-sized avatar

Chris Howie cdhowie

View GitHub Profile
#ifndef OPTIONAL_HPP
#define OPTIONAL_HPP
#include <utility>
namespace gamesolver {
template <typename T>
class optional {
public:
@cdhowie
cdhowie / promise-node-shim.js
Created April 13, 2017 13:53
Simple shim for promise-nodecb interop without requiring a full-blown promise library. (Useful as a boilerplate for AWS Lambda functions.)
Promise.fromCallback = fn =>
new Promise((r, j) => {
fn((err, result) => err ? j(err) : r(result));
});
Promise.prototype.nodeify = function (cb) {
cb && this.then(r => cb(null, r), cb);
return cb ? undefined : this;
};
@cdhowie
cdhowie / README.md
Last active October 16, 2016 23:01
ES5-compatible defer shim

ES5-compatible polyfill for Promise.defer() that will work regardless of whether the Promise constructor invokes the passed function synchronously or asynchronously.

@cdhowie
cdhowie / README.md
Last active October 1, 2016 22:18
Simple implementation of a promise queue that allows only some number of concurrent tasks to execute at once

promise-queue.js

Simple implementation of a Promises/A+ queue module.

Exports a single function that creates a new queue, and can be passed a configuration object with the following keys:

  • concurrency: The maximum number of concurrently-executing functions. (Default: 1)
  • Promise: Promise constructor. (Default: global.Promise)

The return value is the enqueue function, which accepts a single function as its argument. It will return a promise for the result of that function. The function is considered executing until this promise resolves; that is, if the function returns a promise, it will still be considered executing until that promise is settled.

> mocha --bail --slow 125 --timeout 10000 --check-leaks api-server/tests/index.js
NET 6043: listen2 null 4567 4 false undefined
NET 6043: _listen2: create a handle
NET 6043: bind to ::
NET 6043: listen2 127.0.0.1 4568 4 false undefined
NET 6043: _listen2: create a handle
NET 6043: bind to 127.0.0.1
@cdhowie
cdhowie / README.md
Last active July 17, 2019 02:27
DNS proxy to fix Netflix stupidity
#!/bin/env python
import fileinput
counts = {}
for line in fileinput.input():
chomped = line.rstrip('\r\n')
counts[chomped] = counts.get(chomped, 0) + 1
do
-- Don't allow error() to be replaced out from underneath the security checks!
local real_error = error
do
local real_collectgarbage = collectgarbage
collectgarbage = function(oper)
if oper == 'collect' or oper == 'count' then
return real_collectgarbage(oper)
@cdhowie
cdhowie / lua-printproxy.cs
Created November 15, 2013 15:36
Proxying print() calls in Lua to C# (using Eluant)
using System;
using System.Text;
using Eluant;
class Example {
static void Main() {
using (var runtime = new LuaRuntime()) {
using (var proxy = runtime.CreateFunctionFromDelegate(new Action<LuaTable>(PrintProxy))) {
runtime.Globals["printproxy"] = proxy;
}
@cdhowie
cdhowie / Gate.cs
Last active December 16, 2015 21:09
Gate class
/*
The MIT License (MIT)
Copyright (c) 2013 Chris Howie <me@chrishowie.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is