Slide 30
Slide 30 text
@tailrec methods
groupRuns(Vector(1, 1, 3, 3, 4, 2, 2, 5, 6))
List(Vector(1, 1), Vector(3, 3), Vector(4), Vector(2, 2), Vector(5),Vector(6))
@tailrec
def groupRuns[A](c: Seq[A], acc: Seq[Seq[A]] = Seq.empty): Seq[Seq[A]] = {
c match {
case Seq() => acc
case xs =>
val (same, rest) = xs.span { _ == xs.head }
groupRuns(rest, acc :+ same)
}
}
Tuesday, June 11, 13