Skip to content

Instantly share code, notes, and snippets.

@echojc
Last active August 29, 2015 14:07
Show Gist options
  • Save echojc/29eaf5e85e1db1b709cf to your computer and use it in GitHub Desktop.
Save echojc/29eaf5e85e1db1b709cf to your computer and use it in GitHub Desktop.
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 = {
val n = expand(c) count w
n == 3 || (n == 4 && w(c))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment