如何在重复之前进行Scala控制抽象?

pet*_*rim 10 controls abstraction scala repeat

我是Peter Pilgrim.我看过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 ) }
  }

}
Run Code Online (Sandbox Code Playgroud)

错误消息显示:

信息:编译完成时出现1错误和0警告
信息:1错误
信息:0警告
C:\ Users\Peter\IdeaProjects\HelloWord\src\demo\Control.scala
错误:错误:第(57)行错误:Control.this. repeatLoop({
scala.this.Predef.println("found y =".+(y));
y = y.+(1)
})类型Control.this.Until不接受参数
repeatLoop {

在curried函数中,可以认为body返回一个表达式(y + 1的值)但是repeatUntil的声明body参数清楚地表明这可以被忽略或不被忽略?

错误是什么意思?

mic*_*ebe 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> 
Run Code Online (Sandbox Code Playgroud)

根据Jesper和Rex Kerr的说法,这是一个没有Exception的解决方案.

def repeat(body: => Unit) = new {
  def until(condition: => Boolean) = { 
    do {
      body
    } while (!condition)
  }   
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么尝试/捕捉什么时候你可以`{body} while(!condition)`? (4认同)
  • 滥用正常控制流程的异常,丑陋! (3认同)
  • 它被称为结构类型 (2认同)

oxb*_*kes 8

你不需要第二对牙套,用法应该是:

repeatLoop (x) until (cond) //or...
repeatLoop {x} until {cond}
Run Code Online (Sandbox Code Playgroud)

并不是:

repeatLoop {x} { until(cond) } //EXTRA PAIR OF BRACES
Run Code Online (Sandbox Code Playgroud)

该错误意味着 Scala认为您正在尝试使用以下签名调用方法:

def repeatLoop(x: => Unit)(something: X) //2 parameter lists
Run Code Online (Sandbox Code Playgroud)

并且找不到这样的方法.它说"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)
  }
}
Run Code Online (Sandbox Code Playgroud)

这里有两个有用的观察结果:

  1. 该解决方案不是尾递归的,并且会导致StackOverflowError长迭代(尝试while (y < 10000))
  2. until似乎是南辕北辙,我(当条件为真会更自然停止,不进行,而这是真的).


Don*_*zie 6

如何重复一个班轮直到.

def repeat(b: => Unit) = new AnyRef {def until(c: => Boolean) {b; while (! c) b}}
Run Code Online (Sandbox Code Playgroud)

例如,它给出了: -

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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 5

如上所述递归:)

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

var i = 0
repeat {
  println(i)
  i+=1
} until (i < 10)
Run Code Online (Sandbox Code Playgroud)

这也是@tailrec的优化.

Llove scala :)