How to group a variable-length, repeating sequence in Scala

后端 未结 4 1593
生来不讨喜
生来不讨喜 2020-12-31 22:14

I have a collection of ints that repeat themselves in a pattern:

val repeatingSequence = List(1,2,3,1,2,3,4,1,2,1,2,3,4,5)

I\'d like to sec

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 23:02

    Given an iterator itr, this will do the trick:

    val head = iter.next()
    val out = (
      Iterator continually {iter takeWhile (_ != head)}
      takeWhile {!_.isEmpty}
      map {head :: _.toList}
    ).toList
    

提交回复
热议问题