我正在从一只笼子上穿透以下线。 我想确定自首以来的第一行。 这样做的最佳方式是,不要让反var手。
var counter = 0
for (line <- lines) {
println(CsvParser.parse(line, counter))
counter++
}
我知道,必须找到更好的办法,才能做到这一点,这是新情况。
我正在从一只笼子上穿透以下线。 我想确定自首以来的第一行。 这样做的最佳方式是,不要让反var手。
var counter = 0
for (line <- lines) {
println(CsvParser.parse(line, counter))
counter++
}
我知道,必须找到更好的办法,才能做到这一点,这是新情况。
Try zipWithIndex
:
for (line <- lines.zipWithIndex) {
println(CsvParser.parse(line._1, line._2))
}
https://stackoverflow.com/users/576766]>@tenshi 建议与模式匹配作以下改进:
for ((line, count) <- lines.zipWithIndex) {
println(CsvParser.parse(line, count))
}
我完全同意上述答复,但我还是想指出一些重要内容,而且我最初打算作简单评论。
但是,时间很长,这样,我就把它当作一个变式的答案。
<代码>zip* 方法有助于编制附有清单的表格,但它们也有对应人员填写清单,以便编制清单。
因此,一项共同建议是,按顺序排列在<条码>查询<>代码>上要求采取的行动,以便你把所有行动合并起来,只产生结果。 产生结果时,考虑的是可回归代码tforeach
。
现在,如果你有<条码><>>>>条码/代码”作为大卷宗(或甚至名单上的被点人)的线路清单,<条码>齐pWithIndex就将穿过所有物体,并编制一个表格(可移植胶卷)。 然后,便会再次通过同样数额的项目来抓。
Finally, you ve impacted the running lenght by n
, where n
is the length of lines
and added a memory footprint of m + n*16
(roughtly) where m
is the lines
footprint.
lines.view.zipWithIndex map Function.tupled(CsvParser.parse) foreach println
下面几句话(我的承诺),lines.view
将产生如下内容:scala. Collection.Seq
zipWithIndex
和>>>。
Moreover, I think the expression is more elegant because it follows the reader and logical. "For lines, create a view that will zip each item with its index, the result as to be mapped on the result of the parser which must be printed".
HTH.
I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...
I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...
I m going to work on comparing around 300 binary files using Scala, bytes-by-bytes, 4MB each. However, judging from what I ve already done, processing 15 files at the same time using java....
The scala type Nothing represents (as I understand it) the bottom of the type hierarchy, also denoted by the symbol ⊥. That is, Nothing is a sub-type of any given type. The requirement for a Nothing ...
When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...
Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...
I am trying to use Scala s capabilty of running a class with either JUnit or ScalaTest. I took the example given for FeatureSpec and combined it with the example using JUnitSuite. I am having a ...
There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...