News for hackers. Stuff that matters.
Re:Conway's Game of Life in CoffeeScript by wsbail29 on Monday May 16, 2011 @10:52PM (#2555083)
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.
Re:Conway's Game of Life in CoffeeScript by spatten on Monday May 16, 2011 @11:43PM (#2555179)
Nice code!

Docco always impresses too, eh?

I really like the use of:

this[key] = value for key, value of options

I'm going to use that for sure :)

Re:Conway's Game of Life in CoffeeScript by starnix17 on Monday May 16, 2011 @11:35PM (#2555164)
Be sure to check out the annotated source - http://willbailey.name/conway/docs/conway.html
Re:Conway's Game of Life in CoffeeScript by pan69 on Monday May 16, 2011 @11:45PM (#2555181)
I really like how that is done. It would be great to be able to annotate source code like this within your IDE.
Re:Conway's Game of Life in CoffeeScript by AlecSchueler on Tuesday May 17, 2011 @12:10AM (#2555226)
Looks like it was created using Docco - http://jashkenas.github.com/docco/
Re:Conway's Game of Life in CoffeeScript by alttab on Tuesday May 17, 2011 @12:11AM (#2555238)
the newest version of vim has this seamlessly integrated :)
Re:Conway's Game of Life in CoffeeScript by heimidal on Tuesday May 17, 2011 @12:44AM (#2555304)
For real? If so, how do you access it?
Re:Conway's Game of Life in CoffeeScript by freyrs3 on Tuesday May 17, 2011 @12:55AM (#2555322)
!docco %
Re:Conway's Game of Life in CoffeeScript by amirshim on Tuesday May 17, 2011 @01:04AM (#2555339)
I love CoffeeScript, except that it makes me hate writing javascript code now :) Thanks @jashkenas for an amazing language.

I personally like code like this:

  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
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.

@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.

Re:Conway's Game of Life in CoffeeScript by jacobolus on Tuesday May 17, 2011 @01:50AM (#2555435)
If you add a space after the + also, it’ll be fine (indeed, better).

But to be honest, I hate code that tries to do 5 bits of logic on one 100-character-long line, instead of just breaking into two more readable lines. [Edit: looks like that was fixed in the version at http://willbailey.name/conway/docs/conway.html]

Also, can’t your isAlive function look like:

  isAlive: (row, col) -> @world[row]?[col]?.live
Re:Conway's Game of Life in CoffeeScript by jashkenas on Tuesday May 17, 2011 @07:47AM (#2556063)
@amirshim:

Yes, range comprehensions were recently optimized to remove direction-checking when possible. On CoffeeScript master ...

    x for x in [-10..10]
... compiles into:

    var x;
    for (x = -10; x <= 10; x++) {
      x;
    }
Even if the start/end values are variables, the check can be avoided by specifying the step as a number. It'll go out with the next release.
Re:Conway's Game of Life in CoffeeScript by amirshim on Tuesday May 17, 2011 @11:18AM (#2556596)
great!
Re:Conway's Game of Life in CoffeeScript by d0m on Monday May 16, 2011 @11:59PM (#2555203)
Pretty clean code! A small suggestion:

  cell.live = false if count < 2 or count > 3
  cell.live = true  if count == 3
Could become:

  cell.live = count < 2 or count > 3
  cell.live = count == 3
since it's a bit redundant to write "true" since it's already a boolean expression. It's a little bit like saying:

  if a == true:
    return true
  else:
    return false
instead of:

  return a // Agreed that a could be a "truth" value without being the real "true". Still:

  return !!a // With a hack
Re:Conway's Game of Life in CoffeeScript by ggchappell on Tuesday May 17, 2011 @12:30AM (#2555284)
Nope. The point of those if's is that the value of cell.live remains unchanged if the condition is false. Your code does not do that.

What should happen is that, if count is 3, then cell.live becomes true. If count is 2, then cell.live retains its previous value. For all other values of count, cell.live becomes false.

Re:Conway's Game of Life in CoffeeScript by jashkenas on Tuesday May 17, 2011 @12:07AM (#2555219)
Or simply:

    cell.live = count is 3
no?
Re:Conway's Game of Life in CoffeeScript by ggchappell on Tuesday May 17, 2011 @12:26AM (#2555277)
No. You aren't handling the case when count is 2 correctly. In that case, cell.live should remain unchanged, while you are setting it to false. (Note, however, that the code in the comment you are replying to is also incorrect. And does the same thing as your code. So you posted a correct transformation of incorrect code, that keeps it incorrect.)
Re:Conway's Game of Life in CoffeeScript by abarrett on Tuesday May 17, 2011 @07:10AM (#2555999)
The original version still contains redundancy. It would be:

    cell.live = count == 3 if count != 2
Re:Conway's Game of Life in CoffeeScript by ggchappell on Tuesday May 17, 2011 @10:28AM (#2556452)
Yes, that would do it.
Re:Conway's Game of Life in CoffeeScript by eschulte on Tuesday May 17, 2011 @02:24AM (#2555519)
It's no APL... conway's game of life in a single line of code http://news.ycombinator.com/item?id=1041500
Re:Conway's Game of Life in CoffeeScript by wsbail29 on Tuesday May 17, 2011 @01:19AM (#2555371)
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.
Re:Conway's Game of Life in CoffeeScript by truthseeker on Tuesday May 17, 2011 @02:16AM (#2555503)
And here I was thinking it must be about Ron Conway. Too much startup news.
Re:Conway's Game of Life in CoffeeScript by xtat on Tuesday May 17, 2011 @01:16AM (#2555361)
heh, reminds me of my js version http://rapidpacket.com/