Skip to content

Instantly share code, notes, and snippets.

View jamesdavidson's full-sized avatar

James Davidson jamesdavidson

View GitHub Profile
@jamesdavidson
jamesdavidson / basic_tree_sort.c
Created January 24, 2015 08:10
A basic tree sort algorithm.
/* A Saturday afternoon hack project.
* Sort integers from stdin using a binary tree.
* This algorithm is O(n^2) in the worst case though!
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int value;
@jamesdavidson
jamesdavidson / chat_server_test.js
Created January 22, 2015 03:52
Some test cases for a TCP chat server.
var net = require('net');
var async = require('async');
function produceClient (callback) {
var client = net.connect({port:1337, host:process.env['HOSTNAME']});
client.on('connect', function(data) { callback(null,client) });
client.on('error', function() { callback('failed') });
};
async.series([produceClient,produceClient,produceClient],
@jamesdavidson
jamesdavidson / chat_server_test.rb
Created January 20, 2015 09:26
Some test cases for a TCP chat server
require 'open3'
IP_ADDRESS = '192.168.1.103'
PORT = 1337
WELCOME_MSG_LENGTH = 'Welcome to chat server'.length + 1
TEST_MSG = 'hello'
RECVD_MSG = 'Message Sent'
def assert(thing,msg)
unless thing
@jamesdavidson
jamesdavidson / contacts.clj
Created December 14, 2014 13:22
Amateur Lisp
; scrape some VCF in to Clojure data
(def raw_data (slurp "/Users/jd/Documents/contacts_dump.vcf"))
(defn deets [args]
{ :name (first (filter #(re-matches #"FN.*" %) args))
:number (first (filter #(re-matches #"TEL.*" %) args))})
(def contacts
(map deets
@jamesdavidson
jamesdavidson / foo_spec.rb
Created December 9, 2014 04:26
rspec i'm confused
describe 'itself' do
it 'should not work but it does' do
expect(1 > 2)
end
it 'it fails as it should' do
expect(1 > 2).to be(true)
end
end
@jamesdavidson
jamesdavidson / monkey_patch.rb
Last active August 29, 2015 14:10
A monkey-patch for debugging mongo criteria thingies
class String
def fix(size, padstr=' ')
self[0...size].ljust(size, padstr)
end
end
class Mongoid::Criteria
# pull values out into easy-to-read columns
def interrogate(*syms)
syms = [:id,:created_at,:updated_at] if syms.empty?
context "When I count" do
(0..10).each do |i|
puts i
let (:num) { i + 1}
it "gets cached." do
puts num
end
end
end
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://code.jquery.com/jquery.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
@jamesdavidson
jamesdavidson / README.md
Last active August 29, 2015 13:57 — forked from mbostock/.block

This simple force-directed graph shows character co-occurence in Les Misérables. A physical simulation of charged particles and springs places related characters in closer proximity, while unrelated characters are farther apart. Layout algorithm inspired by Tim Dwyer and Thomas Jakobsen. Data based on character coappearence in Victor Hugo's Les Misérables, compiled by Donald Knuth.

Compare this display to a force layout with curved links, a force layout with fisheye distortion and a matrix diagram.

@jamesdavidson
jamesdavidson / cow-chap.js
Created July 7, 2012 15:47
Cow-Chap. Versatile Javascript. Compiles web-apps from an assortment of resources. Deploys to CouchDB. More info at http://cow-chap-js.info
/* ---------------------
# NOTICE:
This script can be run in the browser or on the server.
Edit this script before you use it.
I can not stress this enough!