it doesn't even make sense to me why a for loop would return a collection. It seems very bizarre that it would have those semantics (it's 'for', not 'map')
Ruby works the same, every statement is also an expression. I never saw the use with for and each() either though, it just causes problems and confusion.
I'm guessing because the cofeescript author(s) decided everything was going to be an expression even in situations where it's totally redundant or can bite you. Kind of like the example Plokryeran provided.
Now if you use coffeescript you always be aware of this behaviour. If you don't want your code to be doing needless work you have to end it with an empty return instead.
The short answer is that it's trying to be both map and for, and only partially succeeds at that.
Personally I'd prefer something a bit closer to Python's approach with list comprehensions. If you had to wrap the for block with parentheses to explicitly turn it into a map this problem would go away (and as it happens you already have to do so in most cases).
View all comments
Show parent comment
6
u/hderms Jul 25 '13
it doesn't even make sense to me why a for loop would return a collection. It seems very bizarre that it would have those semantics (it's 'for', not 'map')