Live data from Hacker News

Conway's Game of Life in CoffeeScript

willbailey.name

21–23 of 23 posts

Re: Conway's Game of Life in CoffeeScript

#21

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 car…

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

#22

Earlier quoted context omitted.

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

The original version still contains redundancy. It would be: cell.live = count == 3 if count != 2

Yes, that would do it.

Re: Conway's Game of Life in CoffeeScript

#23

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 car…

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

great!
Post reply on HN