Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / echo.py
Created July 15, 2013 09:06
An extremely simple HTTP echo server in Python for testing requests.
import socket
port = 3001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', port))
s.listen(5)
while 1:
client, address = s.accept()
data = client.recv(1024)
if data:
@mudge
mudge / test.retry.php
Created July 8, 2013 13:27
A retry function for PHP.
<?php
require_once dirname(__FILE__) . '/../lib/simpletest/autorun.php';
function retry($f, $delay = 10, $retries = 3)
{
try {
return $f();
} catch (Exception $e) {
if ($retries > 0) {
sleep($delay);
@mudge
mudge / eventemitter.js
Last active May 12, 2025 15:15
A very simple EventEmitter in pure JavaScript (suitable for both node.js and browsers).
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
@mudge
mudge / nrepl.rb
Last active December 18, 2015 03:59
A very dumb way of talking to a Clojure nREPL through Ruby and the bencode gem.
require "socket"
require "bencode"
class Nrepl
attr_reader :socket, :stream
def initialize(hostname, port)
@socket = TCPSocket.open(hostname, port)
@stream = BEncode::Parser.new(socket)
end
@mudge
mudge / object_merge.js
Last active December 18, 2015 03:19
Based on a suggestion from @tomstuart, a basic implementation of Ruby's Hash#merge for JavaScript (and a bonus implementation of Clojure's assoc) that uses prototypes to share data structures.
/* c.f. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto */
/* A version that mutates the argument object. */
Object.prototype.merge = function (other) {
other.__proto__ = this;
return other;
};
/* A version that does not mutate the argument object. */
@mudge
mudge / pacta_fo.js
Last active December 18, 2015 01:08
Playing with Pacta and fo.
var fo = require('fo'),
Promise = require('pacta').Promise,
add = function add (x) {
return function (y) {
return x + y;
};
};
fo.unsafeSetValueOf(Promise.prototype);
fo.unsafeSetValueOf(Function.prototype);
@mudge
mudge / partial.js
Created May 26, 2013 08:47
Partial application in JavaScript.
Function.prototype.partial = function () {
var f = this,
partialArgs = [].slice.call(arguments);
return function () {
var args = [].slice.call(arguments);
return f.apply(null, partialArgs.concat(args));
};
};
@mudge
mudge / client.js
Last active December 17, 2015 18:09
Using Pacta to deal with three asynchronous HTTP requests (two GETS and a POST).
var http = require('./promised-http'),
Promise = require('pacta').Promise;
var promisedPrefixes = http.get('http://codenames.clivemurray.com/data/prefixes.json'),
promisedAnimals = http.get('http://codenames.clivemurray.com/data/animals.json');
promisedPrefixes.conjoin(promisedAnimals).spread(function (prefixes, animals) {
return JSON.stringify({
prefixes: JSON.parse(prefixes),
animals: JSON.parse(animals)
@mudge
mudge / codenames.coffee
Last active December 17, 2015 12:49
A Hubot script to suggest names from Clive Murray's project namer.
# Description:
# Suggesting solid gold, business appropriate business names since 2013. Using Clive Murray's excellent
# http://codenames.clivemurray.com/
#
# Dependencies:
# "rsvp": "1.2.0"
#
# Configuration:
# None
#
@mudge
mudge / gist:5264126
Created March 28, 2013 15:35
Bash shell functions for controlling VirtualBox virtual machines.
function vm() {
caller=$1
vm=$2
command=$3
case "$command" in
start)
VBoxManage startvm "$vm" --type headless
;;
stop)