Skip to content

Instantly share code, notes, and snippets.

View stevemohapibanks's full-sized avatar

Steven Mohapi-Banks stevemohapibanks

View GitHub Profile
(defn rpad-seq
[s n x]
(if (< (count s) n)
(seq (apply conj (vec s) (replicate (- n (count s)) x)))
s))
=> (rpad-seq (list "a" "b" "c") 10 "d")
("a" "b" "c" "d" "d" "d" "d" "d" "d" "d")
@stevemohapibanks
stevemohapibanks / gist:739068
Created December 13, 2010 15:06
Outputting roman numerals in Clojure
(def *all-roman* ["M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I"])
(def *all-decimal* [1000 900 500 400 100 90 50 40 10 9 5 4 1])
(defn output-as-roman
([n] (output-as-roman n *all-roman* *all-decimal*))
([n rx nx]
(let [roman (first rx) divisor (first nx)
remainder (mod n divisor) quotient (quot n divisor)]
(str (apply str (repeat quotient roman))
(if (> remainder 0) (output-as-roman remainder (rest rx) (rest nx)))))))
@stevemohapibanks
stevemohapibanks / process_log
Created March 15, 2011 09:39
Strips out and reformats an NPG access log file
# First reformat in to CSV and remove duff fields
cat npgj2ee1.nature.com_nature_access.log.2011-03-11 | awk -F"( \"| -)" '{ if ($1 ~ /^Timestamp /) {next;}; printf "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n", $1, $2, $4, $5, $6, $7, $9, $10, $11, $12, $13, $14 }' | sed -e 's/"//g' > sample.log
# Now process sensitive fields
require 'csv'
require 'digest/md5'
output_string = CSV.generate do |output|
CSV.foreach(ARGV.first) do |row|
@stevemohapibanks
stevemohapibanks / hack.erl
Created September 14, 2011 14:59
Connecting to a Minecraft server and doing absolutely fuck all
-module(hacking).
-export([connect/0]).
connect() ->
{ok,Socket} = gen_tcp:connect("127.0.0.1", 25565, [binary, {packet, 0}]),
receive_data(Socket).
receive_data(Socket) ->
receive
{tcp,Socket,Bin} ->
@stevemohapibanks
stevemohapibanks / minecraft.erl
Created September 23, 2011 06:53
WIP Minecraft client
-module(minecraft).
-export([start/1, login/2, listen/0]).
-define(KEEPALIVE, 16#00).
-define(LOGIN, 16#01).
-define(HANDSHAKE, 16#02).
-define(TIME_UPDATE, 16#04).
-define(KICK, 16#FF).
-define(HANDSHAKE_MESSAGE(Username), list_to_binary([<<?HANDSHAKE:8>>, string_to_unicode_binary(Username)])).
@stevemohapibanks
stevemohapibanks / prez.markdown
Created March 12, 2012 09:35
Infrastructure presentation

Orchestrating your Infrastructure

Why automation?

  • Manual process is bad

  • Slow

  • Error prone

  • Makes releasing code hard

  • Automated process is good

@stevemohapibanks
stevemohapibanks / gist:4119358
Created November 20, 2012 17:18
Git commands

Managing branches

Checkout a remote branch to a new local branch

git pull
git checkout -b local-branch origin/remote-branch

Remove a remote branch (e.g. from Github once merged)