8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scala: リストを条件によって分割する(String.splitのList版)

8
Posted at

たとえば、List(1, 0, 1, 1, 0, 1)というリストを 0 で区切ったリスト List(List(1), List(1, 1), List(1)) に変換したいときに使う。

  def split[A](xs: List[A], f: A => Boolean): List[List[A]] = {
    val (a, b) = xs.span((a: A) => !f(a))
    b.isEmpty match {
      case true  => List[List[A]](a)
      case false => List[List[A]](a) ++ split(b.tail, f)
    }
  }

  assert(
    split[Int](List(1, 0, 1, 1, 0, 1), _ == 0) == List(List(1), List(1, 1), List(1))
  )

implicit class にしておくと使いまわしやすいかも。

  implicit class SplittableList[A](list: List[A]) {
    def splitBy(f: A => Boolean): List[List[A]] = splitList(list, f)

    private def splitList(xs: List[A], p: A => Boolean): List[List[A]] = {
      val (a, b) = xs.span((a: A) => !p(a))
      b.isEmpty match {
        case true  => List[List[A]](a)
        case false => List[List[A]](a) ++ splitList(b.tail, p)
      }
    }
  }

  assert(
    List(1, 0, 1, 1, 0, 1).splitBy(_ == 0) == List(List(1), List(1, 1), List(1))
  )
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?