Hmm, I have a solution, but I suspect one can do better:
(test.head :: test).sliding(2).toList.map( (pair: List[Int]) => (pair(1), pair(1) - pair(0)) )
.foldLeft(List(List.empty[Int])){ (acc, pair) =>
if (pair._2 < 3) (pair._1 :: acc.head) :: acc.tail else List(pair._1) :: acc }
Note that this gives results in "doubly-reversed" order:
res3: List[List[Int]] = List(List(14, 12, 10), List(6), List(3, 1))
Which can be corrected by adding .map(_.reverse).reverse
to the end of the function.
EDIT - alternate attempt:
def func(is: List[Int], diff: Int): List[List[Int]] = {
def loop(is: List[Int], prev: Int, acc: List[List[Int]]): List[List[Int]] = is match {
case Nil => acc
case h :: t if (h - prev < diff) => loop(t, h, (h :: acc.head) :: acc.tail)
case h :: t => loop(t, h, List(h) :: acc)
}
loop(is, is.head, List(List.empty[Int]))
}
Again, gives solution in doubly-reversed form.