【发布时间】:2016-11-29 14:22:24
【问题描述】:
这是我的代码...
val strings: Enumerator[String] = Enumerator("1","2","3","4")
//create am Enumeratee using the map method on Enumeratee
val toInt: Enumeratee[String,Int] = Enumeratee.map[String]{
(s: String) => s.toInt
}
val toSelf: Enumeratee[String,String] = Enumeratee.map[String]{
(s: String) => s
}
List("Mary", "Paul") map (_.toUpperCase) filter (_.length > 5)
val r1 = strings |>> (toSelf &>> (toInt &>> sum))
val r2 = strings |>> toSelf &>> (toInt &>> sum)
val r3 = strings |>> toSelf &>> toInt &>> sum // does not compile
val foo1 = strings &> toInt |>> sum
val foo2 = strings &> (toInt |>> sum) // does not compile
val foo3 = (strings &> toInt) |>> sum
符号|>>、&>>。 &> 是方法。我对编译器在它们周围加上括号的方式感到困惑。行内:
List("Mary", "Paul") map (_.toUpperCase) filter (_.length > 5)
编译器像这样插入括号:
((List("Mary", "Paul") map (_.toUpperCase))) filter (_.length > 5)
实际上它编译为:
List("Mary", "Paul").map(((x$3: String) => x$3.toUpperCase()))(List.canBuildFrom[String]).filter(((x$4: String) => x$4.length().>(5)))
在后面的例子中:
strings |>> toSelf &>> (toInt &>> sum)
编译器像这样插入括号:
strings |>> (toSelf &>> (toInt &>> sum))
实际上它编译为:
strings.|>> (toSelf.&>> (toInt.&>>(sum)))
有时编译器似乎是从右到左插入括号(第二个示例),而有时编译器似乎是从左到右插入括号(第一个示例)。有时,就像在
val r3 = strings |>> toSelf &>> toInt &>> sum
我希望它像这样插入括号
val r3 = strings |>> (toSelf &>> (toInt &>> sum))
我得到一个编译器错误。
有人可以解释一下空格分隔方法的括号插入规则吗?
【问题讨论】:
标签: scala infix-notation