I've been playing around with CoffeeScript and docco a bit lately and I thought I'd share this little project. Thanks to Jeremy Ashkenas for creating these fantastic tools.
Be sure to check out the annotated source - <a href="http://willbailey.name/conway/docs/conway.html" rel="nofollow">http://willbailey.name/conway/docs/conway.html</a>
I love CoffeeScript, except that it makes me hate writing javascript code now :) Thanks @jashkenas for an amazing language.<p>I personally like code like this:<p><pre><code> countNeighbors: (cell) ->
neighbors = 0
neighbors += @isAlive cell.row+x, cell.col+y for x in [-1..1] when x || y for y in [-1..1]
neighbors
isAlive: (row, col) -> if @world[row] and @world[row][col] and @world[row][col].live then 1 else 0
</code></pre>
but you have to be careful, since putting a space before the "+x" will cause some bad stuff to happen. Maybe I shouldn't drop so many parenthesis.<p>@jashkenas can you fix [-10..10] (constant boundary) loops to not check for increasing/decreasing :) I can't think of an edge case that breaks it.
Pretty clean code! A small suggestion:<p><pre><code> cell.live = false if count < 2 or count > 3
cell.live = true if count == 3
</code></pre>
Could become:<p><pre><code> cell.live = count < 2 or count > 3
cell.live = count == 3
</code></pre>
since it's a bit redundant to write "true" since it's already a boolean expression. It's a little bit like saying:<p><pre><code> if a == true:
return true
else:
return false
</code></pre>
instead of:<p><pre><code> return a // Agreed that a could be a "truth" value without being the real "true". Still:
return !!a // With a hack</code></pre>
It's no APL... conway's game of life in a single line of code
<a href="http://news.ycombinator.com/item?id=1041500" rel="nofollow">http://news.ycombinator.com/item?id=1041500</a>
Thanks for the code review folks. I made the countNeighbors method a bit more concise and removed some unnecessary binding code from the travelWorld method.