A few more ways to repeat:
scala> xs.foreach (println) first second third
foreach and a similar map that will return something (the results of a function that is for println, Unit, so a list of units)
scala> val lens = for (x <- xs) yield (x.length) lens: Array[Int] = Array(5, 6, 5)
work with elements, not index
scala> ("" /: xs) (_ + _) res21: java.lang.String = firstsecondthird
folding
for(int i=0, j=0; i+j<100; i+=j*2, j+=i+2) {...}
can be done with recursion:
def ijIter (i: Int = 0, j: Int = 0, carry: Int = 0) : Int = if (i + j >= 100) carry else ijIter (i+2*j, j+i+2, carry / 3 + 2 * i - 4 * j + 10)
The carrier part is just an example to do something with me and j. It should not be Int.
for simpler things, closer to the usual for loops:
scala> (1 until 4) res43: scala.collection.immutable.Range with scala.collection.immutable.Range.ByOne = Range(1, 2, 3) scala> (0 to 8 by 2) res44: scala.collection.immutable.Range = Range(0, 2, 4, 6, 8) scala> (26 to 13 by -3) res45: scala.collection.immutable.Range = Range(26, 23, 20, 17, 14)
or without warrant:
List (1, 3, 2, 5, 9, 7).foreach (print)