TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Conway's Game of Life in CoffeeScript

54 pointsby wsbail29about 14 years ago

8 comments

wsbail29about 14 years ago
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.
评论 #2555179 未加载
starnix17about 14 years ago
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>
评论 #2555181 未加载
amirshimabout 14 years ago
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) -&#62; 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) -&#62; 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.
评论 #2555435 未加载
评论 #2556063 未加载
d0mabout 14 years ago
Pretty clean code! A small suggestion:<p><pre><code> cell.live = false if count &#60; 2 or count &#62; 3 cell.live = true if count == 3 </code></pre> Could become:<p><pre><code> cell.live = count &#60; 2 or count &#62; 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>
评论 #2555284 未加载
评论 #2555219 未加载
eschulteabout 14 years ago
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>
wsbail29about 14 years ago
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.
truthseekerabout 14 years ago
And here I was thinking it must be about Ron Conway. Too much startup news.
xtatabout 14 years ago
heh, reminds me of my js version <a href="http://rapidpacket.com/" rel="nofollow">http://rapidpacket.com/</a>