Skip to content

Instantly share code, notes, and snippets.

View echojc's full-sized avatar

Jonathan Chow echojc

View GitHub Profile
trait MapReader[T] {
// should really return Option[T], but let's keep it simple in this example
def read(map: Map[String, Any], key: String): T
}
object MapReader extends MapReaderLowPriorityImplicits {
// convenience method to retrieve implicit instance explicitly
def apply[T: MapReader]: MapReader[T] = implicitly[MapReader[T]]
// these implicits are always implicitly available as long as `MapReader` is in scope
@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 / 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]
transpose :: [[a]] -> [a]
transpose [] = []
transpose input = map head input ++
transpose (filter (not . null) (map tail input))
def transpose[T](list: List[List[T]]): List[T] =
if (list.isEmpty) Nil
else (list map { _.head }) :::
transpose(list map { _.tail} filter { !_.isEmpty })
transpose :: [[a]] -> [a]
transpose [] = []
transpose input = (map (\x -> head x) input) ++
transpose (filter (\x -> not (null x)) (map (\x -> tail x) input))
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)
}
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]
@echojc
echojc / listen.py
Created May 23, 2013 03:58
Quick 'n' dirty Python script to listen on a port and do nothing with the connection, simulating a server that allows you to connect but does not reply.
#!/usr/bin/python
import socket
import sys
if (len(sys.argv) != 2 or not sys.argv[1].isdigit()):
print 'Usage: listen <port>',
exit()
p = int(sys.argv[1])
l = []
@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) =>