17

Given e.g.:

List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)

I'd like to get to:

List(List(5), List(2), List(3, 3, 3), List(5, 5), List(3, 3), List(2, 2, 2))

I would assume there is a simple List function that does this, but am unable to find it.

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59

8 Answers8

16

This is the trick that I normally use:

def split[T](list: List[T]) : List[List[T]] = list match {
  case Nil => Nil
  case h::t => val segment = list takeWhile {h ==}
    segment :: split(list drop segment.length)
}

Actually... It's not, I usually abstract over the collection type and optimize with tail recursion as well, but wanted to keep the answer simple.

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
8
val xs = List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)

Here's another way.

(List(xs.take(1)) /: xs.tail)((l,r) =>
  if (l.head.head==r) (r :: l.head) :: l.tail else List(r) :: l
).reverseMap(_.reverse)
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
8

Damn Rex Kerr, for writing the answer I'd go for. Since there are minor stylistic differences, here's my take:

list.tail.foldLeft(List(list take 1)) { 
    case (acc @ (lst @ hd :: _) :: tl, el) => 
        if (el == hd) (el :: lst) :: tl 
        else (el :: Nil) :: acc 
}

Since the elements are identical, I didn't bother reversing the sublists.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • Can you please explain (or point me to some link) case (acc @ (lst @ hd :: _). I've never seen such syntax before. Thanks in advance. – gashu Mar 12 '15 at 13:21
  • 1
    @gashu In a pattern matching, `x @ y` means `x` will be assigned whatever is _matched_ by `y`. So, for example, `x @ _ :: _` will assign to x a non-empty list (that is, a list with a head and a tail, as matched by `_ :: _`). So `acc`, above, is the whole list, and `lst` is the head of the list. – Daniel C. Sobral Mar 12 '15 at 14:17
  • @daniel-c-sobral so such expressions are only used in pattern matching, or in another context, they do mean something different? – gashu Mar 12 '15 at 14:34
  • @gashu @ is also used for annotations, much in the same way as Java. It doesn't look at all like the above, but it's also a use of the at sign. – Daniel C. Sobral Mar 12 '15 at 14:53
6
list.foldRight(List[List[Int]]()){
  (e, l) => l match {
    case (`e` :: xs) :: fs => (e :: e :: xs) :: fs
    case _ => List(e) :: l
  }
}

Or

list.zip(false :: list.sliding(2).collect{case List(a,b) => a == b}.toList)
 .foldLeft(List[List[Int]]())((l,e) => if(e._2) (e._1 :: l.head) :: l.tail 
                                       else List(e._1) :: l ).reverse

[Edit]

//find the hidden way 
//the beauty must be somewhere
//when we talk scala

def split(l: List[Int]): List[List[Int]] = 
  l.headOption.map{x => val (h,t)=l.span{x==}; h::split(t)}.getOrElse(Nil)
Landei
  • 54,104
  • 13
  • 100
  • 195
  • Would any of the approaches work w/ NaN values? I'm trying to figure out whether I see n consecutive Double.NaN values in a Vector. Looking at the solutions posted here, it seems this is nothing I will come up myself. – TomTom101 May 19 '16 at 17:27
  • NaN values would require special handling, indeed. Maybe you could wrap the `Int`s in `Option[Int]`, and convert the `NaN`s to `None`s. Then the usual equality based solutions would work. Or you could write your own equality function. – Landei May 21 '16 at 21:09
5

I have these implementations lying around from working on collections methods. In the end I checked in simpler implementations of inits and tails and left out cluster. Every new method no matter how simple ends up collecting a big tax which is hard to see from the outside. But here's the implementation I didn't use.

import generic._
import scala.reflect.ClassManifest
import mutable.ListBuffer
import annotation.tailrec
import annotation.unchecked.{ uncheckedVariance => uV }

def inits: List[Repr] = repSequence(x => (x, x.init), Nil)
def tails: List[Repr] = repSequence(x => (x, x.tail), Nil)
def cluster[A1 >: A : Equiv]: List[Repr] =
  repSequence(x => x.span(y => implicitly[Equiv[A1]].equiv(y, x.head)))

private def repSequence(
  f: Traversable[A @uV] => (Traversable[A @uV], Traversable[A @uV]),
  extras: Traversable[A @uV]*): List[Repr] = {

  def mkRepr(xs: Traversable[A @uV]): Repr = newBuilder ++= xs result
  val bb = new ListBuffer[Repr]

  @tailrec def loop(xs: Repr): List[Repr] = {
    val seq = toCollection(xs)
    if (seq.isEmpty)
      return (bb ++= (extras map mkRepr)).result

    val (hd, tl) = f(seq)
    bb += mkRepr(hd)
    loop(mkRepr(tl))
  }

  loop(self.repr)
}

[Edit: I forget other people won't know the internals. This code is written from inside of TraversableLike, so it wouldn't run out of the box.]

psp
  • 12,138
  • 1
  • 41
  • 51
3

Here's a slightly cleaner one:

def groupConsequtive[A](list: List[A]): List[List[A]] = list match {
  case head :: tail =>
    val (t1, t2) = tail.span(_ == head)
    (head :: t1) :: groupConsequtive(t2)
  case _ => Nil  
}

tail-recursive version

@tailrec
def groupConsequtive[A](list: List[A], acc: List[List[A]] = Nil): List[List[A]] = list match {
  case head :: tail =>
    val (t1, t2) = tail.span(_ == head)
    groupConsequtive(t2, acc :+ (head :: t1))
  case _ => acc
}
pathikrit
  • 32,469
  • 37
  • 142
  • 221
  • For my understanding - the downside of the tail-recursive solution here is that the prepend operation is less time effective, isn't it? – miko Nov 30 '20 at 09:43
2

Here's a tail-recursive solution inspired by @Kevin Wright and @Landei:

@tailrec
def sliceEqual[A](s: Seq[A], acc: Seq[Seq[A]] = Seq()): Seq[Seq[A]] = {
  s match {
    case fst :: rest =>
      val (l, r) = s.span(fst==)
      sliceEqual(r, acc :+ l)
    case Nil => acc
  }
}
dskrvk
  • 1,318
  • 15
  • 24
-1

this could be simpler:

val input = List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)
input groupBy identity values