English 中文(简体)
• 如何正确从一台拖拉机中抽取目前的航程
原标题:How to correctly get current loop count from a Iterator in scala
  • 时间:2012-01-13 18:43:05
  •  标签:
  • scala

我正在从一只笼子上穿透以下线。 我想确定自首以来的第一行。 这样做的最佳方式是,不要让反var手。

var counter = 0
for (line <- lines) {
  println(CsvParser.parse(line, counter))
  counter++
}

我知道,必须找到更好的办法,才能做到这一点,这是新情况。

最佳回答
问题回答

我完全同意上述答复,但我还是想指出一些重要内容,而且我最初打算作简单评论。

但是,时间很长,这样,我就把它当作一个变式的答案。

<代码>zip* 方法有助于编制附有清单的表格,但它们也有对应人员填写清单,以便编制清单。

因此,一项共同建议是,按顺序排列在<条码>查询<>代码>上要求采取的行动,以便你把所有行动合并起来,只产生结果。 产生结果时,考虑的是可回归代码t。 例如,foreach

现在,如果你有<条码><>>>>条码/代码”作为大卷宗(或甚至名单上的被点人)的线路清单,<条码>齐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.

Proposition

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.





相关问题
How to flatten a List of different types in Scala?

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

To use or not to use Scala for new Java projects? [closed]

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, ...

Why does Scala create a ~/tmp directory when I run a script?

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

Include jar file in Scala interpreter

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

Scala and tail recursion

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