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

时间:2010-06-14 09:11:25

标签: scala controls abstraction 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 ) }
  }

}

错误消息显示为:

  

信息:编译完成,出现1个错误和0个警告   信息:1错误
  信息:0警告
  C:\ Users \用户彼得\ IdeaProjects \ HELLOWORD的\ src \演示\ 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参数清楚地说明了这个可以被忽略吗?

错误是什么意思?

4 个答案:

答案 0 :(得分: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)
  }   
}

答案 1 :(得分:8)

您不需要第二对括号,用法应为:

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

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

  1. 解决方案不是尾递归的,并且会导致StackOverflowError进行长迭代(尝试while (y < 10000)
  2. until对我来说似乎是错误的方式(当条件变为真时停止更自然,而不是在条件成立时继续进行)。

答案 2 :(得分: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

答案 3 :(得分: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)

它也是@tailrec优化。

Llove scala:)