r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

http://donatstudios.com/CoffeeScript-Madness
207 Upvotes

315 comments sorted by

View all comments

Show parent comment

1

u/nachsicht Jul 27 '13

It doesn't have to be either/or. In scala, for is an expression too, but we can control its return type:

val x = Array(1,2,3)
for(i <- 0 to 2) {
   x(i) = i * 2
} //returns (), which is Unit. This behaviour is analogous to C++ or java's for loops.

for(i <- 0 to 2) yield {
  x(i) = i * 2
} //returns Vector((),(),())