Нещодавно я реалізував для веселої гри «Життя життя Конвей» у Javascript (насправді coffeescript, але те саме). Оскільки javascript можна використовувати як функціональну мову, я намагався залишитися до цього кінця спектру. Я не був задоволений своїми результатами. Я досить хороший OO програміст, і моє рішення пришпилене «старе-те саме». Отже, коротке запитання: який функціональний стиль (псевдокоду) робити?
Ось псевдокод для моєї спроби:
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)