Group elements in a list by range Scala
<p>Considering the following List in Scala :</p>

<p><code>List(4, 5, 6, 7, 8, 12, 13, 14, 17, 23, 24, 25)</code></p>

<p>I want to get the output </p>

<pre><code>List(List(4,8), List(12,14), List(17), List(23,25))
</code></pre>

<p>I have this answer <a href="https://stackoverflow.com/questions/4761386/scala-list-function-for-grouping-consecutive-identical-elements">Scala List function for grouping consecutive identical elements</a></p>

<p>But it is working for grouping identical elements in the same List. </p>

<p>How to extend this solution to resolve my current problem? </p>

<p>I have tried this code </p>

<pre><code>def sliceByRange[A &lt;% Int](s: List[A]): List[List[A]] = s match {
      case Nil =&gt; Nil
     case x :: xs1 =
    val (first, rest) = s.span(y =&gt; y - x == 1)
    first :: sliceByRange(rest)
    }
</code></pre>

<p>But it is not working.</p>