This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transpose :: [[a]] -> [a] | |
transpose [] = [] | |
transpose input = map head input ++ | |
transpose (filter (not . null) (map tail input)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transpose[T](list: List[List[T]]): List[T] = | |
if (list.isEmpty) Nil | |
else (list map { _.head }) ::: | |
transpose(list map { _.tail} filter { !_.isEmpty }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transpose :: [[a]] -> [a] | |
transpose [] = [] | |
transpose input = (map (\x -> head x) input) ++ | |
transpose (filter (\x -> not (null x)) (map (\x -> tail x) input)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe ("alive cells") { | |
val cell = Cell(Alive) | |
Seq( | |
(0, Dead), | |
(1, Dead), | |
(2, Alive), | |
(3, Alive) | |
// ... | |
) foreach { case (count, state) => |