title: Idiomatic scala way to implement this simple split-like function
body: <p>I'd like to implement a function that has the following characteristics:</p>
  
  <ul>
  <li>take an <code>array</code> (<code>Array[A]</code>) and a number of <code>pieces</code> to return (<code>Int</code>) as parameters</li>
  <li>returns the <code>array</code> array split in <code>pieces</code> pieces.</li>
  <li>all pieces should be of equal length if possible, else the first ones should be one element longer to the last ones (if the length of <code>array</code> isn't a multiple of <code>pieces</code>)</li>
  </ul>
  
  <p>in Haskell I would have coded something along those lines:
  </p>
  
  <pre><code>split :: [a] -&gt; Int -&gt; [[a]]
  split list pieces = go list (length list `div` pieces)
   where
    go xs n | l &gt; n &amp;&amp; m == 0 = take n xs : go (drop n xs) n
            | l &gt; n           = take (n + 1) xs : go (drop (n + 1) xs) n
            | otherwise       = [xs]
     where
      l = length xs
      m = l `mod` n
  </code></pre>
  
  <p>Though in Scala I encounter many difficulties to code this (basic) function. For the recursion first, <code>Array</code> doesn't seem that adapted. Then, the <code>if</code> structure that'd allow me to implement the kind of guards I'm using in haskell aren't allowed in place of an expression, which seems weird to me. Another problem I have is that I wouldn't know how to make my scala code polymorphic (as my Haskell code is). Last but not least, I don't get how I'd go about making not only the <code>a</code> equivalent polymorphic, but also the <code>Array</code> instance, because there are many basic collections in Scala.</p>
  
  <p>I'd be fine with <strike>either a complete explained solution or</strike> simple hints to answer my misunderstandings.</p>