Skip to content

Instantly share code, notes, and snippets.

View echojc's full-sized avatar

Jonathan Chow echojc

View GitHub Profile
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 head input ++
transpose (filter (not . null) (map tail input))
@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 = {
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

Keybase proof

I hereby claim:

  • I am echojc on github.
  • I am echojc (https://keybase.io/echojc) on keybase.
  • I have a public key whose fingerprint is E148 7B9D 3EA0 06A2 CF53 9A81 389D F30E 7772 F372

To claim this, I am signing this object:

@echojc
echojc / prototyping-area-0.txt
Created November 27, 2016 12:45
Stoked that I hacked together a rudimentary Simon Says game in Shenzhen I/O! http://puu.sh/sw3gW/107bebc92d.png
[name] simon says
[puzzle] SzSandbox
[traces]
......................
..154.....1555554.....
......14..1555D54.15C.
...94.154..14.2...1CA.
..16...14..955555556A.
.....1414.161414....A.
<!doctype html>
<html>
<head>
<title>vim demo</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet">
<style>
html,
body {
height: 100%;
{
init: function (elevators, floors) {
const floor_up_pressed = {};
const floor_down_pressed = {};
floors.forEach(floor => {
const f = floor;
f.on('up_button_pressed', () => {
floor_up_pressed[f.floorNum()] = true;
});
@media print {
a {
text-decoration: none;
}
a::after {
display: none;
content: '';
}