Skip to content

Instantly share code, notes, and snippets.

View jamesdavidson's full-sized avatar

James Davidson jamesdavidson

View GitHub Profile
@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 / 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 / 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 / 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;
{-# OPTIONS -Wall #-}
module Main where
import Control.Concurrent (threadDelay)
import System.Environment (getArgs)
import System.INotify
main :: IO ()
main = do
@jamesdavidson
jamesdavidson / gist:c24b6e97f4f0cad593a2
Created February 26, 2015 00:41
FizzBuzz in Haskell
main = mapM_ putStrLn $ take 25 fizzbuzz
fizzbuzz = map (\(f,b) -> f ++ b) $ zip fizzes buzzes
fizzes = map (moddy "fizz" 3) [1..]
buzzes = map (moddy "buzz" 5) [1..]
moddy str b i = if i `mod` b == 0 then str else ""
@jamesdavidson
jamesdavidson / foo.clj
Created March 23, 2015 12:16
Get AWS resource data using Amazonica
; Use Amazonica from https://github.com/mcohen01/amazonica
(ns com.example
(:use amazonica.aws.ec2
amazonica.aws.s3
amazonica.aws.route53))
(describe-instances)
(list-buckets)
(map :name (list-buckets))
(list-hosted-zones)
@jamesdavidson
jamesdavidson / gist:65b16dc4c5f964cb3ead
Created March 25, 2015 04:56
Make a HTML list of names from S3 bucket meta-data.
; Retrieve a list of buckets.
; Retrieve the access-control-list.
; For each, print the owner's display-name.
(ns com.example
(:use amazonica.core
amazonica.aws.s3
clojure.pprint))
(defn retrieve-name [bucket]
@jamesdavidson
jamesdavidson / fruit
Last active August 29, 2015 14:17
Recognising single spoken words using CMU Sphinx4.
APPLE AE P AH L
ORANGE AO R AH N JH
PEAR P EH R
MANDARIN M AE N D ER AH N
GRAPE G R EY P
BANANA B AH N AE N AH
@jamesdavidson
jamesdavidson / async-factorial.js
Created April 7, 2015 04:27
Recursion is fun! Node javascript factorial, sync and async.
module.exports = function (n,cb) {
function helper (sum,i) {
if (i == 0) return cb(sum);
else process.nextTick( helper.bind(null,sum*i,i-1) );
};
return helper(1,n);
}