gpt4 book ai didi

隐含点和括号的 Scala 优先级

转载 作者:行者123 更新时间:2023-12-04 09:40:09 29 4
gpt4 key购买 nike

下面的“括号”是怎么写的?

val words = List("foo", "bar", "baz")
val phrase = "These are upper case: " + words map { _.toUpperCase } mkString ", "

是不是一样

val words = List("foo", "bar", "baz")
val phrase = "These are upper case: " + words.map(_.toUpperCase).mkString(", ")

换句话说,隐含的点(“.”)和括号的优先级是否与真实的相同?

第一个版本是否相同

val words = List("foo", "bar", "baz")
val phrase =
"These are upper case: " + (words map { _.toUpperCase } mkString ", ")

最佳答案

以字母开头的运算符具有最低的优先级。 + 的优先级较低,但高于 mapmkString。所以

"These are upper case: " + words map { _.toUpperCase } mkString ", "

应该解析为:

(("These are upper case: " + words).map{ _.toUpperCase }).mkString(", ")

把它想象成:

v1 + v2 map v3 mkString v4
((v1 + v2) map v3) mkString v4

有关更多信息,请参阅我的其他答案:When to use parenthesis in Scala infix notation

关于隐含点和括号的 Scala 优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12623331/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com