In Scala, you often use an iterator to do a for
loop 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 1
gives an empty iterator (like usual range mathematics)?
怎么做呢,从10到1?我猜10比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脚本,通过在迭代器上调用反向来解决它,但是在我看来,它不是很好,是下面的方法吗?
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))
5 个解决方案
#1
186
scala> 10 to 1 by -1
res1: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#2
33
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.
#3
6
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)
#4
5
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)
}
#5
0
You can use Range class:
你可以使用Range类:
val r1 = new Range(10, 0, -1)
for {
i <- r1
} println(i)
#1
186
scala> 10 to 1 by -1
res1: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#2
33
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.
#3
6
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)
#4
5
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)
}
#5
0
You can use Range class:
你可以使用Range类:
val r1 = new Range(10, 0, -1)
for {
i <- r1
} println(i)