Is there a brief syntax for executing a block n times in Scala?

前端 未结 6 1557
误落风尘
误落风尘 2020-12-13 05:54

I find myself writing code like this when I want to repeat some execution n times:

for (i <- 1 to n) { doSomething() }

I\'m looking for

6条回答
  •  既然无缘
    2020-12-13 06:20

    The Range class has a foreach method on it that I think is just what you need. For example, this:

     0.to(5).foreach(println(_))
    

    produced

    0
    1
    2
    3
    4
    5

提交回复
热议问题