I have the following array assignment:
advances = if advance == 0 then [advance] else [advance, (0 - advance)]
I wonder if there is a more coffeescript way of doing this?
What about:
advances = [advance]
advances.push (0 - advance) if advance != 0
I think this is a bit more readable. But I guess that a matter of taste. It also uses coffee-script's nice if-at-the-end feature, which I makes some statements such as this "conditional array growing" more readable (for me at least).
PS: I also changed the == 0 to != 0 to avoid the unless statement. It just confuses me when reading code.
PPS: Don't put everything on one line just for the sake of it (even if is coffee-script is good at that). Always think of the most readable (and well performing) code.
unless is a problem, I actually like it. Also, the use of is helps the reading :) advances.push (0 - advance) unless advance is 0== and if just opens the door hell. ;)You're perhaps thinking about list comprehensions, but I can't imagine how to utilize them here. I believe you'd get better result utilizing underscore.js (or some other library providing collection utilities):
advances = _.uniq([advance, (0 - advance)])
isinstead of==if you really wanted to. They both mean===isand use plain old==. This way you are more compatible with the majority of developers. I also prefer to useif object then x else y(JavaScript's magic check for"", undefined, false, null, 0) which works fine in many places.