Recentemente ho implementato per divertimento Conway's Game of Life in Javascript (in realtà coffeescript ma stessa cosa). Dato che javascript può essere usato come linguaggio funzionale, stavo cercando di rimanere fino a quell'estremità dello spettro. Non ero contento dei miei risultati. Sono un programmatore OO abbastanza bravo e la mia soluzione sa di vecchio-stesso-vecchio-vecchio. Quindi lunga domanda breve: qual è lo stile funzionale (pseudocodice) di farlo?
Ecco Pseudocode per il mio tentativo:
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)