我正在阅读 Beginning Scala 并尝试使用 REPL 中的示例。
我创建了一个 xml 和一个 toInt 函数,如下所示
val x = <x>{(1 to 3).map(i => <i>{i}</i>)}</x>
def toInt(in : String) : Option[Int] = {
try {
Some(Integer.parseInt(in))
}
catch {
case e : Exception => None
}
}
我试过以下
(x \\ "i").map(i => i.text.toInt) // returns Seq[Int]
(x \\ "i").map(i => toInt(i.text)) // returns Sep[Option[Int]]
为什么第一个版本返回 Seq[Int] 而第二个版本返回 Seq[Option[Int]] ?
谢谢
最佳答案
关于scala - 映射转换 i.text.toInt 与 toInt(i.text),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372297/