title: How reduceLeft works on sequence of Functions returning Future
body: <p>This might be a naive question and I am sorry for that. I am studying Scala Futures and stumbled on below code:</p>
  
  <pre><code>object Main extends App {
      def await[T](f: Future[T]) = Await.result(f, 10.seconds)
  
      def f(n: Int): Future[Int] = Future {n + 1}
      def g(n: Int): Future[Int] = Future {n * 2}
      def h(n: Int): Future[Int] = Future {n - 1}
  
      def doAllInOrder[T](f: (T =&gt; Future[T])*): T =&gt; Future[T] = {
        f.reduceLeft((a,b) =&gt; x =&gt; a(x).flatMap(y =&gt; b(y)))
      }
  
      println(await(doAllInOrder(f, g, h)(10))) // 21
    }
  </code></pre>
  
  <p>I know how <code>reduceLeft</code> works when it is applied on <code>Collections</code>. But in the above example, as I understand, in the first pass of <code>reduceLeft</code> value <code>x</code> i.e. <code>10</code> is applied to Function <code>a</code> and the result is applied to Function <code>b</code> using <code>flatMap</code> which eventually return <code>Future[Int]</code> (say, I call it <code>result</code>). In the next pass of <code>reduceLeft</code> the <code>result</code> and Function <code>h</code> has to be used, but here is I am troubled.</p>
  
  <p>The <code>result</code> is actually an already executed Future, but the <code>reduceLeft</code> next pass expects a Function which returns a Future[Int].Then how it is working?</p>
  
  <p>Another thing I am not able to understand how each pass sending its result to next pass of <code>reduceLeft</code>, i.e. how <code>x</code> is getting it's value in subsequent passes.</p>
  
  <p>Though both of my confusions are interrelated and a good explanation may help clear my doubt.</p>
  
  <p>Thanks in advance.</p>