title: Looking for a nice way to split an array
body: <p>I've been looking for a method similar to <em>String.split</em> in Scala <em>Array</em>, but I've not been able to find it.</p>
  
  <p>What I want to do is to split an array by a separator.</p>
  
  <p>For example, separating the following array: </p>
  
  <pre><code>val array = Array('a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n')
  </code></pre>
  
  <p>using the <code>'\n'</code> separator, should result in:</p>
  
  <pre><code>List(Array(a, b), Array(c, d, e), Array(g))
  </code></pre>
  
  <p>I know that I can convert the Array to String, and apply split there:</p>
  
  <pre><code>array.mkString.split('\n').map(_.toArray)
  </code></pre>
  
  <p>but I would prefer to skip the conversion.</p>
  
  <p>The solution I have so far involves using <strong>span</strong> recursively and is a bit too boilerplate:</p>
  
  <pre><code>  def splitArray[T](array: Array[T], separator: T): List[Array[T]] = {
      def spanRec(array: Array[T], aggResult: List[Array[T]]): List[Array[T]] = {
        val (firstElement, restOfArray) = array.span(_ != separator)
        if (firstElement.isEmpty) aggResult
        else spanRec(restOfArray.dropWhile(_ == separator), firstElement :: aggResult)
      }
      spanRec(array, List()).reverse
    }
  </code></pre>
  
  <p>I'm sure there must be something in Scala I'm missing. Any idea?</p>
  
  <p>thanks,
  Ruben</p>