Scala downwards or decreasing for loop?

前端 未结 7 971
死守一世寂寞
死守一世寂寞 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:36

    Having programmed in Pascal, I find this definition nice to use:

    implicit class RichInt(val value: Int) extends AnyVal {
      def downto (n: Int) = value to n by -1
      def downtil (n: Int) = value until n by -1
    }
    

    Used this way:

    for (i <- 10 downto 0) println(i)
    

提交回复
热议问题