如何使 Scala 控制重复直到抽象?
我是彼得·皮尔格林。我看到 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.repeatLoop({
scala.this.Predef.println("找到 y=".+(y));
y = y.+(1)
Control.this.Until 类型的 }) 不带参数
重复循环 {
函数体可以被认为是返回一个表达式(y+1的值),但是repeatUntil的声明体参数清楚地表明这是否可以被忽略?
错误是什么意思?
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 ) }
}
}
The error message reads:
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
C:\Users\Peter\IdeaProjects\HelloWord\src\demo\Control.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 {
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?
What does the error mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个没有
StackOverflowError
的解决方案。根据 Jesper 和 Rex Kerr 的说法,这是一个没有异常的解决方案。
Here is a solution without the
StackOverflowError
.According to Jesper and Rex Kerr here is a solution without the Exception.
你不需要第二对大括号,用法应该是:
而不是:
错误意味着 Scala 认为你正在尝试调用带有类似签名的方法:
并且找不到这样的签名方法。这是说“repeatLoop(body)”不带参数。该解决方案的完整代码清单可能看起来更像是:
这里有两个有用的观察结果:
until
对我来说似乎是错误的方式(当条件成立时停止会更自然,而不是在条件成立时继续真的)。You don't need the 2nd pair of braces, the usage should be:
And not:
The error means that Scala thinks you are trying to call a method with a signature something like:
And can find no such method. It is saying "repeatLoop(body)" does not take parameters. A full code listing for the solution probably looks something a bit more like:
There are two useful observations to make here:
StackOverflowError
for long iterations (trywhile (y < 10000)
)until
seems the wrong way round to me (it would be more natural to stop when the condition becomes true, not carry on while it is true).一个单行重复直到怎么样?
例如,给出:-
How about a one liner for repeat until.
Which, for example, gives:-
如上所述,但递归:)
它也经过 @tailrec 优化。
爱斯卡拉:)
As above yet recursive :)
It's @tailrec optimized too.
Llove scala :)