【发布时间】:2014-02-04 02:27:56
【问题描述】:
我想查看在 map/flatmap 中传递的 f 函数,但没有运气。我抛出了一个异常,看到f 的任何迹象,没有工作。那是什么功能?它是如何在幕后生成的?
Exception in thread "main" java.lang.RuntimeException
at x.x.Main$A.getInt(Empty.scala:8)
at x.x.Main$A.flatMap(Empty.scala:10)
object Main extends App {
class A {
def getInt: Int = throw new RuntimeException
def map(f: Int => Boolean): Boolean = f(getInt)
def flatMap(f: Int => Boolean): Boolean = f(getInt)
}
for {
x <- new A
y <- new A
} yield x == y
}
【问题讨论】:
-
看起来 senia 实际上有一个比我更好的解决方案来去除 repl 中的东西。也许你应该接受他们的回答而不是我的回答。