我想在scala中编写一个for循环,但在某些特殊情况下,计数器应该增加多个(数量是可变的)。
答案 0 :(得分:0)
您可以使用过滤器和外部变量的组合来完成此操作。这是一个例子:
var nextValidVal = 0
for (i <- 0 to 99; if i >= nextValidVal) {
var amountToSkip = 0
// Whatever this loop is for
nextValidVal = if (amountToSkip > 0) i + amountToSkip + 1 else nextValidVal
}
因此,在循环的主体中,您可以根据条件将amountToSkip
设置为 n 。将跳过i序列的下一个 n 值。
如果您的序列是从其他类型的序列中提取的,您可以这样做
var skip = 0
for (o <- someCollection if { val res = skip == 0; skip = if (!res) skip - 1 else 0; res } ) {
// Do stuff
}
如果在循环体中将skip
设置为正值,则将跳过序列的下一个 n 元素。
当然,这是非常必要和副作用。我会通过映射或过滤或折叠原始序列来寻找其他可能的方法。
答案 1 :(得分:0)
您可以实现自己的流来反映步骤,例如:
import scala.collection.immutable.Stream
import ForStream._
object Test {
def main(args: Array[String]): Unit = {
val range = 0 to 20 by 1 withVariableStep; // in case you like definition through range
//val range = ForStream(0,20,1) // direct definition
for (i<- range) {
println(s"i=$i")
range.step = range.step + 1
}
}
}
object ForStream{
implicit def toForStream(range: Range): ForStream = new ForStreamMaster(range.start, range.end,range.step)
def apply(head:Int, end:Int, step:Int) = new ForStreamMaster(head, end,step)
}
abstract class ForStream(override val head: Int, val end: Int, var step: Int) extends Stream[Int] {
override val tailDefined = false
override val isEmpty = head > end
def withVariableStep = this
}
class ForStreamMaster(_head: Int, _end: Int, _Step: Int) extends ForStream(_head, _end,_Step){
override def tail = if (isEmpty) Stream.Empty else new ForStreamSlave(head + step, end, step, this)
}
class ForStreamSlave(_head: Int, _end: Int, _step: Int, val master: ForStream) extends ForStream(_head, _end,_step){
override def tail = if (isEmpty) Stream.Empty else new ForStreamSlave(head + master.step, end, master.step, master)
}
打印:
I = 0
I = 2
I = 5
I = 9
I = 14
I = 20
您可以使用含义从Range定义ForStream,或直接定义它。但要小心:
同样注意到@ om-nom-nom,使用递归
可能会更好答案 2 :(得分:0)
为什么不使用do-while循环?
var x = 0;
do{
...something
if(condition){change x to something else}
else{something else}
x+=1
}while(some condition for x)