如何在重复直到中使 Scala 控制抽象? [英] How Does One Make Scala Control Abstraction in Repeat Until?

查看:12
本文介绍了如何在重复直到中使 Scala 控制抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是彼得·朝圣者.我看到 Martin Odersky 在 Scala 中创建了一个控制抽象.但是我似乎还不能在 IntelliJ IDEA 9 中重复它.它是 IDE 吗?

I am Peter Pilgrim. I watched Martin Odersky create a control abstraction in Scala. However I can not yet seem to repeat it inside IntelliJ IDEA 9. Is it the 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:UsersPeterIdeaProjectsHelloWordsrcdemoControl.scala
错误:错误:第(57)行错误:Control.this.repeatLoop({
scala.this.Predef.println("找到y=".+(y));
y = y.+(1)
}) 类型 Control.this.Until 不带参数
重复循环{

Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
C:UsersPeterIdeaProjectsHelloWordsrcdemoControl.scala
Error:Error:line (57)error: Control.this.repeatLoop({
scala.this.Predef.println("found y=".+(y));
y = y.+(1)
}) of type Control.this.Until does not take parameters
repeatLoop {

在柯里化函数中,主体可以被认为返回一个表达式(y+1 的值),但是repeatUntil 的声明主体参数清楚地表明这是否可以忽略?

In the curried function the body can be thought to return an expression (the value of y+1) however the declaration body parameter of repeatUntil clearly says this can be ignored or not?

错误是什么意思?

推荐答案

这是一个没有 StackOverflowError 的解决方案.

Here is a solution without the 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 的说法,这是一个没有例外的解决方案.

According to Jesper and Rex Kerr here is a solution without the Exception.

def repeat(body: => Unit) = new {
  def until(condition: => Boolean) = { 
    do {
      body
    } while (!condition)
  }   
}

这篇关于如何在重复直到中使 Scala 控制抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆