在每个谓词 scala 中将字符串列表拆分为多个列表

Splitting a list of strings into several lists at every predicate scala

有没有办法拆分字符串列表,如下所示:

List("lorem", "ipsum" ,"X", "sit", "amet", "consectetur")

在每个谓词如 x => x.equals("X") 中放入几个列表,结果将是:

List(List("lorem", "ipsum"), List("sit", "amet", "consectetur"))

这一切都以一种简单的功能方式?

您可以使用尾递归函数轻松跟踪每个块,如下所示:

def splitEvery[A](data: List[A])(p: A => Boolean): List[List[A]] = {
  @annotation.tailrec
  def loop(remaining: List[A], currentChunk: List[A], acc: List[List[A]]): List[List[A]] =
    remaining match {
      case a :: tail =>
        if (p(a))
          loop(
            remaining = tail,
            currentChunk = List.empty,
            currentChunk.reverse :: acc
          )
        else
          loop(
            remaining = tail,
            a :: currentChunk,
            acc
          )
      
      case Nil =>
        (currentChunk.reverse :: acc).reverse
    }
  
  loop(
    remaining = data,
    currentChunk = List.empty,
    acc = List.empty
  )
}

可以这样使用:

val data = List("lorem", "ipsum" ,"X", "sit", "amet", "consectetur")
val result = splitEvery(data)(_ == "X")
// result: List[List[String]] = List(List(lorem, ipsum), List(sit, amet, consectetur)
)

可以看到代码运行 here.

unfold() (Scala 2.13.x) 方式。

val lst =
  List("X","lorem","ipsum","X","sit","amet","X","consectetur","X")

List.unfold(lst){st =>
  Option.when(st.nonEmpty){
    val (nxt, rst) = st.span(_ != "X")
    (nxt, rst.drop(1))
  }
}
//res0: List[List[String]] = List(List()
//                              , List(lorem, ipsum)
//                              , List(sit, amet)
//                              , List(consectetur))