Finding lengths between characters in a string
<p>I have a function from a character and a string to an array of integers. The array has an index which is the a number within of times the character has shown up in the text, and the entry for this index is the distance from the preceding character that showed up in the text. If that character is newline, this function basically computes an array of the line lengths given the string. </p>

<pre><code>val func: Char =&gt; (String =&gt; Array[Int]) = (ch: Char) =&gt; (str: String) =&gt; {
    var lens: Array[Int] = new Array[Int](20)
    var noCh: Int = 0
    var c: Int = 0 // accumlates till the next character is spotted
    for (i &lt;- 0 until str.length) {
        c += 1
        if (str.charAt(i) == ch) {
            if (noCh&gt;= lens.length) {
                val newlen: Array[Int] = new Array[Int](2*noCh)
                Array.copy(lens,0,newlen,0,noCh)
                lens = newlen
            }
            lens(noCh) = c; c = 0; noCh+= 1
        }
    }
    lens
    }                                         //&gt; func  : Char =&gt; (String =&gt; Array[Int]) = &lt;function1&gt;
    func('\n')("hello world \n hello wstsadfasdf \n sdlfkjasdf\n")
                                                  //&gt; res2: Array[Int] = Array(13, 20, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                  //| 0, 0, 0, 0)
</code></pre>

<p>Is there a faster method of solving this problem? Iterating through every character seems very slow, especially if you going through a very large string. </p>