Skip to content

Instantly share code, notes, and snippets.

@munro
munro / asyncMap.js
Created July 15, 2011 17:57
Array.prototype.asyncMap
Array.prototype.asyncMap = function (fn, callback) {
var that = this, i, count = 0, errors = [], map = [];
for (i = 0; i < this.length; i += 1) {
(function iter(i) {
fn(that[i], function (err, value) {
err && errors.push(err);
map[i] = value;
count += 1;
if (count === that.length) {
callback(errors.length ? errors : false, map);
@munro
munro / memcache_dump.php
Created July 18, 2011 19:26
dump memcache
<?php
$memcache = new Memcache;
$memcache->connect("localhost", 11211);
$memcache->flush();
$list = array();
$allSlabs = $memcache->getExtendedStats('slabs');
$items = $memcache->getExtendedStats('items');
foreach($allSlabs as $server => $slabs) {
foreach($slabs AS $slabId => $slabMeta) {
@munro
munro / 0_comparison.js
Created July 19, 2011 21:16
Async join comparison with TameJS
var db = {
get: function (key, callback) {
setTimeout(function () {
callback(false, 'got ' + key);
}, Math.floor(Math.random() * 1000));
}
},
helpers = require('./2_helpers');
// Here is a similar blocking example of what the following algorithms are doing:
@munro
munro / README.md
Created August 4, 2011 17:43
Blog: This is a test

Hello

World

  • this
  • is
  • a
@munro
munro / README.md
Created August 7, 2011 05:46
Blog: Another test
  • Blocking IO

      +--------------------+--------------------+-----+
      | read               | write              | md5 |
      +--------------------+--------------------+-----+
    
  • Asynchronous IO

+--------------------+

@munro
munro / README.md
Created August 7, 2011 08:11
Blog: Hello Adam

THIS

  • is
  • a test test test
@munro
munro / README.md
Created August 7, 2011 17:31
JavaScript OOP

Bad:

{{gist bad_inheritance.js}}

Good:

{{gist inheritance.js}}

@munro
munro / base.py
Created August 10, 2011 20:28
Modular Inheritance
NAME = 'BASE'
def get_name():
return NAME
@munro
munro / kittens.js
Created August 24, 2011 00:25
Kitten Cache
var fs = require('fs'),
http = require('http'),
path = require('path');
var CACHE_DIR = '.kitten_cache';
if (!path.existsSync(CACHE_DIR)) {
fs.mkdirSync('.kitten_cache', 0755);
} else if (!fs.statSync(CACHE_DIR).isDirectory()) {
console.error('Cache dir `%s` must be a directory.', CACHE_DIR)
@munro
munro / seriesCoarse.js
Created November 3, 2011 21:32
seriesCoarse
function seriesCoarse(first, second, interval, obj) {
var start = (new Date()).getTime();
interval = interval || 1000;
obj = obj || this;
first.call(obj, function () {
setTimeout(function () {
}, interval - ((new Date()).getTime() - start) % interval);
});