这是我的密码。
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))相反,我得到了一个编译器错误。
有人能解释一下空格分隔方法插入括号的规则吗?
发布于 2016-07-26 06:18:21
在infix表示法有优先权中为它们定义的操作,如规范中所提到的:
前缀操作符的优先级由该运算符的第一个字符决定。下面按优先级的增加顺序列出字符,同一行中的字符具有相同的优先级:
(all letters)
|
^
&
= !
< >
:
+ -
* / %
(all other special characters)运算符的优先级和相联性决定了表达式各部分的分组如下。
e0; op1; e1; op2… opn; en的连续infix操作op1,…,opnop1,…,opn,那么所有这些运算符都必须具有相同的结合性。如果所有运算符都是左关联的,则序列被解释为(…(e0;op1;e1);op2…);opn;en.否则,如果所有运算符都是正确关联的,则序列将被解释为e0;op1;(e1;op2;(…opn;en)…).。e1; op1; e2; op2总是等同于(e1;op1;e2);op2。根据规范,您的第二个表达式应该是:
strings.|>>((toSelf.&>>toInt).&>>(sum)))因为|的优先级低于&,所以它是最后调用的,然后&>>是左关联的,因此它们从左到右调用。
https://stackoverflow.com/questions/38581360
复制相似问题