Is there an equivalent to python reduce() function in scala?
<p>I've just started learning Scala and functional programming and I'm trying to convert the following from Python to Scala:</p>

<pre><code>def immutable_iterative_fibonacci(position):

    if (position ==1):
        return [1]

    if (position == 2):
        return [1,1]

    next_series = lambda series, _: series + [series [-1] + series [-2]]
    return reduce(next_series, range(position - 2), [1, 1])
</code></pre>

<p>I can't figure out what the equivalent of reduce in Scala is. This is what I currently have. Everything works fine except the last line.</p>

<pre><code>def immutable_fibonacci(position: Int) : ArrayBuffer[Int] = {

    if (position == 1){
          return ArrayBuffer(1)
     }

     if (position == 2){
         return ArrayBuffer(1,1)
     }

     var next_series = (series: ArrayBuffer[Int]) =&gt; series :+ ( series( series.size - 1) + series( series.size -2))

     return reduce(next_series, 2 to position, ArrayBuffer(1,1))
}
</code></pre>

<p>Summary of Python <a href="https://docs.python.org/2/library/functions.html#reduce" rel="nofollow"><code>reduce</code></a>, for reference:</p>

<pre><code>reduce(function, iterable[, initializer])
</code></pre>

<h2>Traversable</h2>

<p>A good type to look at is <a href="http://www.scala-lang.org/api/current/index.html#scala.collection.Traversable" rel="nofollow"><code>Traversable</code></a>, a supertype of <code>ArrayBuffer</code>. You may want to just peruse that API for a while, because there's a lot of useful stuff in there.</p>

<h2>Reduce</h2>

<p>The equivalent of Python's <code>reduce</code>, when the <code>initializer</code> arg is omitted, is Scala's <a href="http://www.scala-lang.org/api/current/index.html#scala.collection.Traversable@reduceLeft[B&gt;:A](op:(B,A)=&gt;B):B" rel="nofollow"><code>Traversable[A]#reduceLeft</code></a>:</p>

<pre><code>reduceLeft[B &gt;: A](op: (B, A) =&gt; B): B
</code></pre>

<p>The <code>iterable</code> arg from the Python function corresponds to the <code>Traversable</code> instance, and the <code>function</code> arg from the Python function corresponds to <code>op</code>.</p>

<p>Note that there are also methods named <code>reduce</code>, <code>reduceRight</code>, <code>reduceLeftOption</code>, and <code>reduceRightOption</code>, which are similar but slightly different.</p>

<h2>Fold</h2>

<p>Your example, which does provide an <code>initializer</code> arg, corresponds to Scala's <a href="http://www.scala-lang.org/api/current/index.html#scala.collection.Traversable@foldLeft[B](z:B)(op:(B,A)=&gt;B):B" rel="nofollow"><code>Traversable[A]#foldLeft</code></a>:</p>

<pre><code>foldLeft[B](z: B)(op: (B, A) =&gt; B): B
</code></pre>

<p>The <code>initializer</code> arg from the Python function corresponds to the <code>z</code> arg in <code>foldLeft</code>.</p>

<p>Again, note that there are some related methods named <code>fold</code> and <code>foldRight</code>.</p>

<h2>Fibonacci</h2>

<p>Without changing the algorithm, here's a cleaned-up version of your code:</p>

<pre><code>def fibonacci(position: Int): Seq[Int] =
  position match {
    case 1 =&gt; Vector(1)
    case 2 =&gt; Vector(1, 1)
    case _ =&gt;
      (2 to position).foldLeft(Vector(1, 1)) { (series, _) =&gt;
        series :+ (series(series.size - 1) + series(series.size - 2))
      }
  }
</code></pre>

<p>A few miscellaneous notes:</p>

<ul>
<li>We generally never use the <code>return</code> keyword</li>
<li>Pattern matching (what we're doing with the <code>match</code> keyword) is often considered cleaner than an <code>if</code>-<code>else</code> chain</li>
<li>Replace <code>var</code> (which allows multiple assignment) with <code>val</code> (which doesn't) wherever possible</li>
<li>Don't use a mutable collection (<code>ArrayBuffer</code>) if you don't need to. <code>Vector</code> is a good general-purpose immutable sequence.</li>
</ul>

<p>And while we're on the topic of collections and the Fibonacci series, for fun you may want to check out the first example in the <a href="http://www.scala-lang.org/api/current/#scala.collection.immutable.Stream" rel="nofollow"><code>Stream</code></a> documentation:</p>

<pre><code>val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #::
  fibs.zip(fibs.tail).map { n =&gt; n._1 + n._2 }

fibs.drop(1).take(6).mkString(" ")
// "1 1 2 3 5 8"
</code></pre>

python scala lambda functional-programming reduce