简体   繁体   English

Scala映射,zipWithIndex和中缀形式

[英]Scala map, zipWithIndex and infix form

I have next code: 我有下一个代码:

List.tabulate(n, n)(_ * _).zipWithIndex.map{case (list, index) => index :: list}

I want to write it as suggested by http://docs.scala-lang.org/style/method-invocation.html , so in infix form: 我想按照http://docs.scala-lang.org/style/method-invocation.html的建议编写它,所以以infix形式:

val n = 10
val list = List.tabulate(n, n)(_ * _) zipWithIndex map{case (list, index) => index :: list}

but this stops from compiling. 但这将停止编译。 Why? 为什么? And are there any better explanation when I can use infix and when can I not, rather than a link I gave? 当我可以使用infix以及何时不能使用infix而不是我给出的链接时,还有什么更好的解释吗? It doesn't look like it explains the chained calls of different Arity, like I'm doing 看起来不像我在解释不同Arity的链式调用

Both zipWithIndex does not take parameters so you cannot write it in infix position. 这两个zipWithIndex都没有参数,因此您不能在中缀位置写入它。 Try: 尝试:

val n = 10
val list = List.tabulate(n, n)(_ * _).zipWithIndex map { case (list, index) => index :: list }

However, if you meant suffix, that is as Peter mentioned deprecated and discouraged. 但是,如果您指的是后缀,那就是彼得所提及弃用的。 If you insist though, you would have to import scala.language.postfixOps . 但是,如果您坚持要求,则必须import scala.language.postfixOps

And even then you would not be able to combine suffix with infix notation. 即使那样,您也无法将后缀与中缀表示法结合使用。

import scala.language.postfixOps
val n = 10
val list = List.tabulate(n, n)(_ * _) zipWithIndex 
val result = list map { case (list, index) => index :: list }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM