Last active
August 29, 2015 14:07
-
-
Save echojc/29eaf5e85e1db1b709cf to your computer and use it in GitHub Desktop.
Straight to the point implementation of Conway's Game of Life
This file contains hidden or 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 = { | |
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