10

私はピーター・ピルグリムです。Martin Odersky が Scala でコントロールの抽象化を作成するのを見ました。ただし、IntelliJ IDEA 9 内でそれを繰り返すことはまだできないようです。それは IDE ですか?

package demo

class Control {

  def repeatLoop ( body: => Unit ) = new Until( body )

  class Until( body: => Unit ) {
    def until( cond: => Boolean ) {
      body;
      val value: Boolean = cond;
      println("value="+value)
      if ( value ) repeatLoop(body).until(cond)
      // if  (cond) until(cond)
    }
  }

  def doTest2(): Unit = {
    var y: Int = 1
    println("testing ... repeatUntil() control structure")
    repeatLoop {
      println("found y="+y)
      y = y + 1
    }
    { until ( y < 10 ) }
  }

}

エラーメッセージは次のとおりです。

情報: コンパイルは 1 つのエラーと 0 の警告で完了しました
情報: 1 つのエラー
情報: 0 の警告
C:\Users\Peter\IdeaProjects\HelloWord\src\demo\Control.scala
エラー: エラー: 行 (57) エラー: Control.this.型 Control.this.Until のrepeatLoop({
scala.this.Predef.println("found y=".+(y));
y = y.+(1)
}) はパラメータを取らない
repeatLoop {

カリー化された関数では、本体は式 (y+1 の値) を返すと考えられますが、repeatUntil の宣言本体パラメーターは、これを無視できるかどうかを明確に示していますか?

エラーの意味は何ですか?

4

4 に答える 4

10

これは、なしのソリューションStackOverflowErrorです。

scala>   class ConditionIsTrueException extends RuntimeException
defined class ConditionIsTrueException

scala>   def repeat(body: => Unit) = new {
 |     def until(condition: => Boolean) = { 
 |       try {
 |         while(true) {
 |           body
 |           if (condition) throw new ConditionIsTrueException
 |         }   
 |       } catch {
 |         case e: ConditionIsTrueException =>
 |       }   
 |     
 |     }   
 |   }
repeat: (body: => Unit)java.lang.Object{def until(condition: => Boolean): Unit}

scala> var i = 0              
i: Int = 0

scala> repeat { println(i); i += 1 } until(i == 3)
0
1
2

scala> repeat { i += 1 } until(i == 100000)       

scala> repeat { i += 1 } until(i == 1000000)

scala> repeat { i += 1 } until(i == 10000000)

scala> repeat { i += 1 } until(i == 100000000)

scala> 

Jesper と Rex Kerr によると、ここに例外のない解決策があります。

def repeat(body: => Unit) = new {
  def until(condition: => Boolean) = { 
    do {
      body
    } while (!condition)
  }   
}
于 2010-06-14T11:26:14.733 に答える
8

中かっこの 2 番目のペアは必要ありません。使用方法は次のとおりです。

repeatLoop (x) until (cond) //or...
repeatLoop {x} until {cond}

そしてそうではありません:

repeatLoop {x} { until(cond) } //EXTRA PAIR OF BRACES

このエラーは、次のようなシグネチャでメソッドを呼び出そうとしていると Scala が認識していることを意味します。

def repeatLoop(x: => Unit)(something: X) //2 parameter lists

そして、そのような方法を見つけることができません。「repeatLoop(body)」はパラメーターを取らないと言っています。ソリューションの完全なコード リストは、おそらく次のようになります。

object Control0 {
  def repeatLoop(body: => Unit) = new Until(body)

  class Until(body: => Unit) {
    def until(cond: => Boolean) {
      body;
      val value: Boolean = cond;

      if (value) repeatLoop(body).until(cond)
    }
  }


  def main(args: Array[String]) {
    var y: Int = 1
    println("testing ... repeatUntil() control structure")
    repeatLoop {
      println("found y=" + y)
      y += 1
    }.until(y < 10)
  }
}

ここで、次の 2 つの有益な観察事項があります。

  1. 解決策は末尾再帰的ではなく、StackOverflowErrorfor 長い反復になります (試してくださいwhile (y < 10000))
  2. 私には間違った方法のuntilようです(条件が真になったら停止する方が自然であり、真である間は続行しません)。
于 2010-06-14T09:18:48.907 に答える
6

までの繰り返しのワンライナーはどうですか。

def repeat(b: => Unit) = new AnyRef {def until(c: => Boolean) {b; while (! c) b}}

たとえば、次のようになります。

scala> repeat {
     |   println("i = "+i)
     |   i+=1
     | } until (i >= 10)
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
于 2010-06-14T16:21:26.073 に答える