Skip to content

Instantly share code, notes, and snippets.

@hawx
hawx / quit.go
Created January 2, 2016 14:25
Example showing how to provide a blocking-close for long running processes in golang. The process will run until Close() is called, this call blocks until the process has actually stopped.
package main
import (
"log"
"time"
)
type Process struct {
quit chan struct{}
}
@hawx
hawx / poker_test.py
Last active December 2, 2015 19:40
Suit-blind poker hands kata solution in simple (fors and ifs as much as possible) Python
import unittest
def winner(hand1, hand2):
hand1 = sorted(hand1, reverse=True)
hand2 = sorted(hand2, reverse=True)
result = try_winner([four_of_a_kind, full_house, straight, three_of_a_kind,
two_pairs, pair], hand1, hand2)
if result != 0:
@hawx
hawx / reboot.sh
Last active November 16, 2015 13:32
Reboot sonoses in ip range
#/usr/bin/env bash
#
# eg. ./reboot.sh 192.168.1
for ip in $1.{1..254}; do
curl "http://$ip:1400/reboot" -m 2 2>/dev/null &
done
@hawx
hawx / build.sh
Last active October 13, 2015 18:55
Building evmpd on raspbian
git clone https://github.com/hawx/evmpd
cd evmpd
sudo apt-get install libmpdclient-dev
wget http://mirror.ox.ac.uk/sites/archive.raspbian.org/archive/raspbian/pool/main/libe/libevdev/libevdev-dev_1.3+dfsg-1_armhf.deb
sudo apt-get install libjs-jquery # WHAT?
sudo dpkg -i libevdev-dev_1.3+dfsg-1_armhf.deb
make
@hawx
hawx / counts.sh
Last active August 29, 2015 14:26
List links with counts
grep 'http[^, ]*' *.txt | sed -ne 's/.*\(http[^, ]*\).*/\1/p' | sort | uniq -c | sort -r -t ' ' > counts.txt
@hawx
hawx / subclass.rb
Created February 12, 2015 18:26
adds Object#subclasses
class Object
# A method that adds a #subclass class method allowing
# you to find the subclasses of a particular class.
#
# @example
#
# class String2 < String; end
# class String3 < String; end
# String.subclasses
@hawx
hawx / runner.rb
Created February 12, 2015 18:26
simple runner for before/after each/all tasks
# Sometimes you want to run a set of blocks before/after
# each/all of something. Problem solved.
#
# @example
#
# class CountDown < Runner
# before :all do |arr|
# arr.collect {|i| i + 1 }
# end
#
@hawx
hawx / proc_injector.rb
Created February 12, 2015 18:25
'set' local variables within a proc before it runs
# @param args [Hash{Symbol=>Object}]
def run_proc_with_locals(args, proc)
k = Class.new
args.each do |sym, v|
k.send(:define_method, sym) { v }
end
k.new.instance_exec(&proc)
end
tha_proc = proc do
@hawx
hawx / maths.lisp
Created February 5, 2015 20:00
Old maths homework solving
#!/usr/bin/env newlisp
;; Prints the nth fibonacci number
(define (fib n)
(if (< n 2)
1
(+ (fib (- n 1))
(fib (- n 2)))))
;; Solves equations of the form
@hawx
hawx / jsx.js
Last active August 29, 2015 14:06
What JSX should be
// Given we have some,
// function el(name) { return function() { ... } }
var Dropdown = el('Dropdown'),
Menu = el('Menu'),
MenuItem = el('MenuItem');
var dropdown = Dropdown(
'A dropdown list',
Menu(