- 581 名前:デフォルトの名無しさん mailto:sage [2012/04/29(日) 00:31:18.37 ]
- >557をscalaでベタ移植してみた。
case class Cell(var alive:Int) { var neighborhood = List[Cell]() var alive_ = 0 def evaluate() { val s = neighborhood.map(_.alive).sum alive_ = if(s == 3 || s == 4 && alive == 1) 1 else 0 } def update() { alive = alive_ } } case class Board(val lattice:List[List[Int]]) { val cells = lattice.map(_.map(x => Cell(x))) val m = lattice.size; val n = lattice(0).size for(i <- 0 until m; j <- 0 until n; k <- -1 to 1; l <- -1 to 1) cells(i)(j).neighborhood ::= cells((i+k+m)%m)((j+l+n)%n) def life() { cells.foreach(_.foreach(_.evaluate)) cells.foreach(_.foreach(_.update)) } override def toString() = { cells.map(_.map(_.alive.toString).reduce(_ + "," + _)).reduce(_ + "\n" + _) } } val board = Board(List(List(0,1,1,1,0),List(0,1,0,0,0),List(0,0,1,0,0),List(0,0,0,0,0),List(0,0,0,0,0))) board.life print(board) scalaの機能を余り活用できていない感じ。
|

|