Scala向下或减少循环?

时间:2010-04-11 15:22:08

标签: scala iterator loops for-loop

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

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

你会怎么做,所以它从10变为1?我想10 to 1给出一个空的迭代器(就像通常的范围数学一样)?

我制作了一个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))

7 个答案:

答案 0 :(得分:211)

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

答案 1 :(得分:35)

来自@ 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.

答案 2 :(得分:9)

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/scale_20dp">

    <LinearLayout
        android:id="@+id/button_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/currentYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/paymentscreengrey"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingLeft="35dp"
                android:paddingTop="@dimen/scale_50dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/scale_20dp"
                    android:text="****   ****   ****   5432"
                    android:textColor="@color/white"
                    android:textSize="@dimen/scale_20dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="2345"
                    android:textColor="@color/white"
                    android:textSize="@dimen/scale_16dp" />

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

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

第3个解决方案:仅使用“to”

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

答案 3 :(得分:6)

在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
}

以这种方式使用:

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

答案 4 :(得分:1)

您可以使用Range类:

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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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

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