Most elegant repeat loop in Scala

前端 未结 4 888
遇见更好的自我
遇见更好的自我 2021-02-01 01:56

I\'m looking for an equivalent of:

for(_ <- 1 to n)
  some.code()

that would be shortest and most elegant. Isn\'t there in Scala anything si

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 02:19

    I would suggest something like this:

    List.fill(10)(println("hi"))
    

    There are other ways, e.g.:

    (1 to 10).foreach(_ => println("hi"))
    

    Thanks to Daniel S. for the correction.

提交回复
热议问题