scala Scala向下或减少循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2617513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:03:39  来源:igfitidea点击:

Scala downwards or decreasing for loop?

scalaiteratorloopsfor-loop

提问by Felix

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

在 Scala 中,您经常使用迭代器按for递增顺序执行循环,例如:

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

How would you do it so it goes from 10 to 1? I guess 10 to 1gives an empty iterator (like usual range mathematics)?

你会怎么做,让它从 10 变成 1?我猜10 to 1给出了一个空的迭代器(就像通常的范围数学)?

I made a Scala script which solves it by calling reverse on the iterator, but it's not nice in my opinion, is the following the way to go?

我制作了一个 Scala 脚本,它通过在迭代器上调用 reverse 来解决它,但在我看来这并不好,以下是要走的路吗?

def nBeers(n:Int) = n match {

    case 0 => ("No more bottles of beer on the wall, no more bottles of beer." +
               "\nGo to the store and buy some more, " +
               "99 bottles of beer on the wall.\n")

    case _ => (n + " bottles of beer on the wall, " + n +
               " bottles of beer.\n" +
               "Take one down and pass it around, " +
              (if((n-1)==0)
                   "no more"
               else
                   (n-1)) +
                   " bottles of beer on the wall.\n")
}

for(b <- (0 to 99).reverse)
    println(nBeers(b))

回答by Randall Schulz

scala> 10 to 1 by -1
res1: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

回答by Chirlo

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

来自@Randall 的答案就像黄金一样好,但为了完成起见,我想添加几个变体:

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.

回答by Dipak Shaw

Scala provides many ways to work on downwards in loop.

Scala 提供了许多在循环中向下工作的方法。

1st Solution: with "to" and "by"

第一种解决方案:使用“to”和“by”

//It will print 10 to 0. Here by -1 means it will decremented by -1.     
for(i <- 10 to 0 by -1){
    println(i)
}

2nd Solution: With "to" and "reverse"

第二种解决方案:使用“to”和“reverse”

for(i <- (0 to 10).reverse){
    println(i)
}

3rd Solution: with "to" only

第三种解决方案:仅使用“to”

//Here (0,-1) means the loop will execute till value 0 and decremented by -1.
for(i <- 10 to (0,-1)){
    println(i)
}

回答by LP_

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

在 Pascal 中编程后,我发现这个定义很好用:

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)

回答by KaaPex

You can use Range class:

您可以使用 Range 类:

val r1 = new Range(10, 0, -1)
for {
  i <- r1
} println(i)

回答by Jonny

You can use : for (i <- 0 to 10 reverse) println(i)

您可以使用 : for (i <- 0 to 10 reverse) println(i)

回答by Sandu Nicula

for (i <- 10 to (0,-1))

The loop will execute till the value == 0, decremented each time by -1.

循环将执行直到值 == 0,每次递减 -1。