Collection producing for-loop in Scala

2009-03-27 by mira

An unusual way to create a collection with the numbers 0 .. 99 in Scala.

The more I actually write code in Scala, the more I am impressed with it. I love OO and I already write my Java code in a somewhat functional way. I value type safety very much and don’t want to give it away, so Scala is a natural fit for me.

Here is my highlight of today; the (nested) for-loop that produces a collection value

val numbers =
    for {
      n1 <- 0 to 9
      n0 <- 0 to 9
    } yield 10*n1 + n0

numbers.foreach(println)

Archive

architecture