Recientemente implementé por diversión el Juego de la vida de Conway en Javascript (en realidad coffeescript pero lo mismo). Como javascript se puede usar como un lenguaje funcional, estaba tratando de mantenerme en ese extremo del espectro. No estaba contento con mis resultados. Soy un programador de OO bastante bueno y mi solución parecía la misma de siempre. Tan larga pregunta corta: ¿cuál es el estilo funcional (pseudocódigo) de hacerlo?
Aquí está el pseudocódigo para mi intento:
class Node
update: (board) ->
get number_of_alive_neighbors from board
get this_is_alive from board
if this_is_alive and number_of_alive_neighbors < 2 then die
if this_is_alive and number_of_alive_neighbors > 3 then die
if not this_is_alive and number_of_alive_neighbors == 3 then alive
class NodeLocations
at: (x, y) -> return node value at x,y
of: (node) -> return x,y of node
class Board
getNeighbors: (node) ->
use node_locations to check 8 neighbors
around node and return count
nodes = for 1..100 new Node
state = new NodeState(nodes)
locations = new NodeLocations(nodes)
board = new Board(locations, state)
executeRound:
state = clone state
accumulated_changes = for n in nodes n.update(board)
apply accumulated_changes to state
board = new Board(locations, state)