Scala downwards or decreasing for loop?

前端 未结 7 985
死守一世寂寞
死守一世寂寞 2020-12-13 01:56

In Scala, you often use an iterator to do a for loop in an increasing order like:

for(i <- 1 to 10){ code }

How would you d

7条回答
  •  半阙折子戏
    2020-12-13 02:25

    The answer from @Randall is good as gold, but for sake of completion I wanted to add a couple of variations:

    scala> for (i <- (1 to 10).reverse) {code} //Will count in reverse.
    
    scala> for (i <- 10 to(1,-1)) {code} //Same as with "by", just uglier.
    

提交回复
热议问题