Scala for-loop。以consice方式获取索引

时间:2021-08-06 04:35:36

In this code I want to increment index to put it to each yielding result.

在这段代码中,我想增加索引以将它放到每个让步的结果中。

var index=0

for(str <- splitToStrings(text) ) yield  {

  if (index != 0) index += 1               // but index is equal to `0` all the time

  new Word(str, UNKNOWN_FORM, index )
}

Why I can not change index ? And what the best way to implement this logic then, trying to be concise?

为什么我不能改变指数?那么实现这种逻辑的最佳方法是什么,试图简洁?

4 个解决方案

#1


22  

The zipWithIndex method on most sequence-like collections will give you a zero-based index, incrementing with each element:

大多数类似序列的集合上的zipWithIndex方法将为您提供从零开始的索引,并随每个元素递增:

for ((str, index) <- splitToStrings(text).zipWithIndex)
  yield new Word(str, UNKNOWN_FORM, index)

#2


6  

Because initially index is set to 0, thus your condition index != 0 is never executes to true and index is never got incremented. Maybe you don't need this condition? Maybe you can count results afterwards? Now I see that index is used within loop. Then you have to either use @BenJames answer or go recursive.

因为最初索引设置为0,因此您的条件索引!= 0永远不会执行为true,索引永远不会递增。也许你不需要这个条件?也许你可以计算结果呢?现在我看到索引在循环中使用。然后你必须使用@BenJames答案或递归。

#3


3  

zipWithIndex will copy and create a new collection, so better make it lazy when the collection is potentially large

zipWithIndex将复制并创建一个新集合,因此当集合可能很大时,最好使其变得懒惰

for ((str, index) <- splitToStrings(text).view.zipWithIndex)
  yield new Word(str, UNKNOWN_FORM, index)

In fact, if you are working with an indexed sequence, then a more efficient way is to use indices, which produces the range of all indices of this sequence.

事实上,如果您正在使用索引序列,那么更有效的方法是使用索引,它生成此序列的所有索引的范围。

val strs = splitToStrings(text)

for(i <- strs.indices) yield  {
  new Word(strs(i), UNKNOWN_FORM, i )
}

#4


1  

splitToStrings(text).foldLeft(0,List[Word]){(a,b) => {
   if(a._1!=0) (a._1+1,new Word(str, UNKNOWN_FORM, index) :: b)
   else (a._1,new Word(str, UNKNOWN_FORM, index) :: b)
}}

I am using foldLeft here with a tuple as: starting base with index = 0 and an empty List. I then iterate over each element.

我在这里使用foldLeft和一个元组:起始基数索引= 0和一个空列表。然后我迭代每个元素。

Above a is this tuple. I check the index value and increment it. Else I dont add the index. And I add the new Word to the list.

高于a就是这个元组。我检查索引值并递增它。否则我不添加索引。然后我将新Word添加到列表中。

Ultimately in the end you get a tuple containing the index value and the total List containing all Words.

最终,你得到一个包含索引值的元组和包含所有单词的总List。

#1


22  

The zipWithIndex method on most sequence-like collections will give you a zero-based index, incrementing with each element:

大多数类似序列的集合上的zipWithIndex方法将为您提供从零开始的索引,并随每个元素递增:

for ((str, index) <- splitToStrings(text).zipWithIndex)
  yield new Word(str, UNKNOWN_FORM, index)

#2


6  

Because initially index is set to 0, thus your condition index != 0 is never executes to true and index is never got incremented. Maybe you don't need this condition? Maybe you can count results afterwards? Now I see that index is used within loop. Then you have to either use @BenJames answer or go recursive.

因为最初索引设置为0,因此您的条件索引!= 0永远不会执行为true,索引永远不会递增。也许你不需要这个条件?也许你可以计算结果呢?现在我看到索引在循环中使用。然后你必须使用@BenJames答案或递归。

#3


3  

zipWithIndex will copy and create a new collection, so better make it lazy when the collection is potentially large

zipWithIndex将复制并创建一个新集合,因此当集合可能很大时,最好使其变得懒惰

for ((str, index) <- splitToStrings(text).view.zipWithIndex)
  yield new Word(str, UNKNOWN_FORM, index)

In fact, if you are working with an indexed sequence, then a more efficient way is to use indices, which produces the range of all indices of this sequence.

事实上,如果您正在使用索引序列,那么更有效的方法是使用索引,它生成此序列的所有索引的范围。

val strs = splitToStrings(text)

for(i <- strs.indices) yield  {
  new Word(strs(i), UNKNOWN_FORM, i )
}

#4


1  

splitToStrings(text).foldLeft(0,List[Word]){(a,b) => {
   if(a._1!=0) (a._1+1,new Word(str, UNKNOWN_FORM, index) :: b)
   else (a._1,new Word(str, UNKNOWN_FORM, index) :: b)
}}

I am using foldLeft here with a tuple as: starting base with index = 0 and an empty List. I then iterate over each element.

我在这里使用foldLeft和一个元组:起始基数索引= 0和一个空列表。然后我迭代每个元素。

Above a is this tuple. I check the index value and increment it. Else I dont add the index. And I add the new Word to the list.

高于a就是这个元组。我检查索引值并递增它。否则我不添加索引。然后我将新Word添加到列表中。

Ultimately in the end you get a tuple containing the index value and the total List containing all Words.

最终,你得到一个包含索引值的元组和包含所有单词的总List。