Scala 中缀表示法

标签 scala infix-notation

这是我的代码...

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))

相反,我收到了编译器错误。

有人可以解释一下空格分隔方法的括号插入规则吗?

最佳答案

操作infix notation have a precedence定义给他们,如规范中所述:

The precedence of an infix operator is determined by the operator's first character. Characters are listed below in increasing order of precedence, with characters on the same line having the same precedence:

(all letters)
|
^
&
= !
< >
:
+ -
* / %
(all other special characters)

Precedence and associativity of operators determine the grouping of parts of an expression as follows.

  • If there are several infix operations in an expression, then operators with higher precedence bind more closely than operators with lower precedence.

  • If there are consecutive infix operations e0; op1; e1; op2… opn; en with operators op1,…,opnop1,…,opn of the same precedence, then all these operators must have the same associativity. If all operators are left-associative, the sequence is interpreted as (…(e0;op1;e1);op2…);opn;en. Otherwise, if all operators are right-associative, the sequence is interpreted as e0;op1;(e1;op2;(…opn;en)…).

  • Postfix operators always have lower precedence than infix operators. E.g. e1; op1; e2; op2 is always equivalent to (e1;op1;e2);op2

根据规范,您的第二个表达式应该是:

strings.|>>((toSelf.&>>toInt).&>>(sum)))

由于 | 的优先级低于 &,因此最后调用它,然后 &>> 保持关联,因此它们是从左到右调用。

关于Scala 中缀表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581360/

相关文章:

scala - 在 Scala Cats Validated 中,如何组合有序验证

scala - 用于处理接收中的异步操作的 Akka 模式

java - 评估后缀表达式,例如

c++ - 函数中 return 语句的段错误

java - 反向抛光到 java 中的 Infix Notation 优化

c++ - 使用 std::map 评估表达式树

scala - 如何定义一个参数类型不能为 Any 的 scala 方法

Play Framework 2.1 RC1 中的 AJAX 文件上传传送一个空文件

scala - 玩! 2 WS 库 : Detect and Handle Closed Connection in Streaming HTTP response

postfix-notation - 如何检查给定表达式是中缀表达式、后缀表达式还是前缀表达式?