Last active
August 29, 2015 14:15
-
-
Save kamiyaowl/34ac191e5ecf53bd9b8a to your computer and use it in GitHub Desktop.
Y Combinator: http://yuroyoro.hatenablog.com/entry/20100204/1265285318
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 Y[A,B]( f:((A => B), A ) => B, x:A ):B = f( (y:A) => Y(f,y),x) | |
((size:(Int,Int)) => Y( | |
(f:Map[(Int,Int),Boolean] => Stream[Map[(Int,Int),Boolean]], seed:Map[(Int,Int),Boolean]) => | |
((curr : Map[(Int,Int),Boolean]) => Stream.cons(curr,f(curr))) { | |
( for (x <- 0 to size._1; y <- 0 to size._2) yield List( | |
seed.get(x - 1, y - 1), seed.get(x, y - 1), seed.get(x + 1, y - 1), seed.get(x - 1, y), seed.get(x + 1, y), seed.get(x - 1, y + 1), seed.get(x, y + 1), seed.get(x + 1, y + 1)) | |
count { case Some(z) => z; case None => false } | |
match { case z if !seed(x, y) && z == 3 => (x, y) -> true ; case z if seed(x, y) && (z == 2 || z == 3) => (x, y) -> true ; case _ => (x, y) -> false } | |
).toMap }, | |
((s:(Int,Int)) => { for(x <- 0 to size._1 ; y <- 0 to size._2) yield (x,y) -> util.Random.nextBoolean }.toMap)(size) | |
).map { _.toIndexedSeq.sortBy{ case ((x,y),_) => x + y * size._1 }.map{ case ((_,_),b) => if(b) "#" else " " }.grouped(size._1).map(_.mkString("","","\n")).mkString("=" * size._1 + "\n","","") }.foreach(x => { readLine(); println(x) }) | |
)(30,10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment