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.
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.
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]
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.
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.
[+] [-] wsbail29|15 years ago|reply
[+] [-] spatten|15 years ago|reply
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 :)
[+] [-] starnix17|15 years ago|reply
[+] [-] pan69|15 years ago|reply
[+] [-] amirshim|15 years ago|reply
I personally like code like this:
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.
[+] [-] jacobolus|15 years ago|reply
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:
[+] [-] jashkenas|15 years ago|reply
Yes, range comprehensions were recently optimized to remove direction-checking when possible. On CoffeeScript master ...
... compiles into: 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.[+] [-] d0m|15 years ago|reply
[+] [-] ggchappell|15 years ago|reply
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.
[+] [-] jashkenas|15 years ago|reply
[+] [-] eschulte|15 years ago|reply
[+] [-] wsbail29|15 years ago|reply
[+] [-] truthseeker|15 years ago|reply
[+] [-] xtat|15 years ago|reply