Skip to content

Instantly share code, notes, and snippets.

@munro
munro / class.js
Created November 18, 2011 05:00
JavaScript Class Abstraction
// Class sugar
var Class = (function () {
'use strict';
// Base class
function Class() {
throw new Error('Cannot instantiate `Class`.');
}
@munro
munro / foobar.cpp
Created November 18, 2011 06:01
Why not?
class FooA {
public:
int a;
}
class FooB : public FooA {
public:
int b;
}
@munro
munro / lambda.cpp
Created November 22, 2011 23:47
c++11 lambda
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
std::function<void()> createCounter() {
std::shared_ptr<int> count(new int(0));
return [count](){
(*count) += 1;
cout << "count = " << (*count) << "\n";
@munro
munro / regression.js
Created November 23, 2011 05:53
Regression
var sample_set = (function () {
'use strict';
var sample_set = [];
function generateSample(not_recursive) {
var overhead_usage = Math.random() * 2 + 5,
game_usage = Math.random() * 2 + 3,
idle_usage = Math.random(),
games = Math.floor(Math.random() * 30) + 10,
@munro
munro / wtf.cs
Created November 25, 2011 21:39
Wtf CoffeeScript
fnMaker = (name) => (args...) => [name, args]
what = fnMaker 'what'
the = fnMaker 'the'
fuck = 'fuck'
_is = fnMaker 'is'
going = fnMaker 'going'
to = 'to'
happen = 'happen'
@munro
munro / signals.cpp
Created December 9, 2011 21:34
C++11 Signals
#ifndef __signals_h__
#define __signals_h__
#include <list>
#include <memory>
#include <iostream>
#include <functional>
namespace signals {
template<typename... Values> class Signal {
@munro
munro / a_python9001.py
Created December 14, 2011 02:19
python9001
# named functions
def HelloWorld(str, fn, str2):
pass
# single statement anonymous function
helloWorld('foo', lambda x, y: foo(x, y), 'bar')
# multi statement anonymous functions
helloWorld('foo', (lambda x, y:
arbitraryMulti(x)
@munro
munro / speed.py
Created December 14, 2011 20:12
Lambda Speed Comparison
import timeit
print 'Lambda: %f' % timeit.Timer('fn = lambda x: x * x\n'
'map(fn, xrange(10))', 'gc.enable()').timeit()
print 'Named function: %f' % timeit.Timer('def fn(x): return x * x;\n'
'map(fn, xrange(10))', 'gc.enable()').timeit()
@munro
munro / .gitignore
Created January 4, 2012 06:53
Git dotfiles .gitignore
/*
!.*
.gitignore
@munro
munro / cut_off.cpp
Created January 4, 2012 21:05
Stripping off extended class properties when copying
#include <iostream>
using namespace std;
class Foo {
public:
int a;
Foo(int a) : a(a) {
}
virtual void print() {
cout << " I am Foo: " << a << "\n";