Q:

Splitting List into List of List

How can I convert:
List(1,1,1,1,4,2,2,2,2)

into:
List(List(1,1,1,1), List(2,2,2,2))

Thought this would be the easiest way to show what I'm looking for.  I am having a hard time trying to find the most functional way to do this with a large list that needs to be separated at a specific element.  This element does not show up in the new list of lists.  Any help would be appreciated! 

A:

If you want to support multiple instances of that separator, you can use foldRight with some list "gymnastics":
// more complex example: separator (4) appears multiple times
val l = List(1,1,1,1,4,2,2,2,2,4,5,6,4)

val separator = 4

val result = l.foldRight(List[List[Int]]()) {
  case (`separator`, res) => List(Nil) ++ res
  case (v, head :: tail) => List(v :: head) ++ tail
  case (v, Nil) => List(List(v))
}

// result: List(List(1, 1, 1, 1), List(2, 2, 2, 2), List(5, 6))

