Skip to content

Instantly share code, notes, and snippets.

View echojc's full-sized avatar

Jonathan Chow echojc

View GitHub Profile
@echojc
echojc / implicit-json-format.scala
Last active August 29, 2015 14:00
Fundep materialiser for spray-json's JsonFormat via superclass implicit (for Scala 2.10.3)
import scala.language.experimental.macros
import scala.reflect.macros.Context
import spray.json._
trait ImplicitJsonFormat
object ImplicitJsonFormat {
implicit def materializeJsonFormat[T]: JsonFormat[T] = macro jfImpl[T]
@echojc
echojc / conway.scala
Last active August 29, 2015 14:07
Straight to the point implementation of Conway's Game of Life
object Conway {
type Cell = (Int, Int)
type World = Set[Cell]
def next(w: World): World = w flatMap expand filter alive(w)
private def expand(c: Cell): Set[Cell] =
(for (x <- -1 to 1; y <- -1 to 1) yield (c._1 + x, c._2 + y)).toSet
private def alive(w: World)(c: Cell): Boolean = {
@echojc
echojc / pbday
Last active December 15, 2015 05:58
Simple empirical test of birthday problem probabilities.
def bday = new Random().nextInt(365)
def uniq(n: Int) = (1 to n).map(_ => bday).distinct.size == n
def puniq(n: Int, c: Int = 1000000) = (1 to c).map(_ => uniq(n)).count(_ == true).toDouble / c
@echojc
echojc / git aliases
Created March 26, 2013 03:16
I don't think this is how they intended you to use it.
# ~/.bashrc
alias g='git'
# ~/.gitconfig
[alias]
l = log
o = checkout
p = pull --rebase
s = status
c = commit
@echojc
echojc / cdiff.js
Last active December 15, 2015 12:19
Bookmarklet to add simple diff highlighting to emails sent to Gmail by Git's default post-receive hook.
javascript:(function(){var a=document.getElementsByClassName("gt ii adP adO");for(b in a){var e=a[b];e.innerHTML="<pre style=\"font-family:Consolas;font-size:9pt\">"+e.innerHTML.replace(/^\s*(\+.*)$/gm,"<span style=\"color:green\">$1</span>").replace(/^\s*(-.*)$/gm,"<span style=\"color:red\">$1</span>").replace(/^\s*(@@.*)$/gm,"<span style=\"color:blue\">$1</span>").replace(/<br>/g,"")+"</pre>";}}());
@echojc
echojc / conway_liveneighbour_test_repeating.scala
Created May 12, 2013 04:32
Repetitive way to test different inputs for a function that determines whether a cell in Conway's Game of Life should become alive or stay dead given the number of live neighbours.
describe ("alive cells") {
val cell = Cell(Alive)
it ("should become Dead when there are 0 live neighbours") {
cell.next(0) should be (Cell(Dead))
}
it ("should become Dead when there are 1 live neighbours") {
cell.next(1) should be (Cell(Dead))
}
it ("should become Alive when there are 2 live neighbours") {
cell.next(2) should be (Cell(Alive))
@echojc
echojc / roman_numerals_test.rb
Last active December 17, 2015 05:59
How Corey Haines structed his tests for the Roman Numerals kata according to http://youtu.be/vX-Yym7166Y
describe "Converting arabic numbers to roman numerals" do
{
1 => "I",
2 => "II",
5 => "V"
#...
}.each_pair do |arabic, roman|
it "converts #{arabic} to #{roman}" do
expect(convert(arabic)).to eq(roman)
end
@echojc
echojc / conway_liveneighbour_test.scala
Created May 12, 2013 04:34
A concise way to test different inputs for a function that determines whether a cell in Conway's Game of Life should become alive or stay dead given the number of live neighbours.
describe ("alive cells") {
val cell = Cell(Alive)
Seq(
(0, Dead),
(1, Dead),
(2, Alive),
(3, Alive)
// ...
) foreach { case (count, state) =>
transpose :: [[a]] -> [a] -> [a]
transpose [] output = output
transpose input output = transpose
(filter (\x -> not (null x)) (map (\x -> tail x) input))
(output ++ (map (\x -> head x) input))
print (transpose [[1, 2, 3], [4, 5], [6, 7, 8]] [])
-- prints [1, 4, 6, 2, 5, 7, 3, 8]
def transpose[T](input: List[List[T]]) = {
def iter[T](input: List[List[T]], output: List[T]): List[T] =
if (input.isEmpty) output
else iter(
input map { _.tail } filter { !_.isEmpty },
output ::: (input map { _.head })
)
iter(input, Nil)
}