Scala for-loop. Getting index in consice way

后端 未结 4 1249
广开言路
广开言路 2020-12-31 16:17

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

var index=0

for(str <- splitToStrings(text) ) yield           


        
4条回答
  •  天命终不由人
    2020-12-31 16:51

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

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

提交回复
热议问题