Skip to content

Instantly share code, notes, and snippets.

@fgalassi
Created December 8, 2011 08:52
Show Gist options
  • Save fgalassi/1446498 to your computer and use it in GitHub Desktop.
Save fgalassi/1446498 to your computer and use it in GitHub Desktop.
var Compose = require("compose")
var livingThing = Compose({
alive: false,
isDead: function() {
return !this.alive
},
isAlive: function() {
return this.alive
},
live: function() {
this.alive = true
},
die: function() {
this.alive = false
}
})
var withNeighbors = Compose({
knownNeighbors: 0,
meetNeighbors: function(count) {
count = count || 1
this.knownNeighbors += count
},
forgetNeighbors: function() {
this.knownNeighbors = 0
}
})
var Cell = Compose(
livingThing,
withNeighbors,
{
evolve: function() {
if (this.knownNeighbors < 2) {
this.die()
}
if (this.knownNeighbors == 3) {
this.live()
}
if (this.knownNeighbors > 3) {
this.die()
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment