title: How Javascript `reduce` performed on function array achieves function composition?
body: <p>I came across this pattern in <code>redux</code> <code>compose</code> function. I still don't understand how in the example below the functions are evaluated starting from the last and not from the first:</p>
  
  <pre><code>function f2(a) {
    return a + a;
  }
  function f3(a) {
    return a + a + a;
  }
  function f4(a) {
    return a + a + a + a;
  }
  function f5(a) {
    return a + a + a + a + a;
  }
  
  function compose(...funcs) {
    return funcs.reduce(function x(a, b) {
      return function y(...args) {
        const temp = a(b(...args));
        return temp;
      };
    });
  }
  
  const composedFunction = compose(f2, f3, f4, f5);
  const result = composedFunction(2);
  </code></pre>
  
  <p>In the first <code>reduce</code> iteration the accumulator is <code>f2</code> so we'll get <code>f2(f3(2))=12</code>. In the next iteration we'll call <code>f4(12)=48</code>. In the last iteration we'll call <code>f5(48)=240</code>. So the evaluation order is <code>f5(f4(f2(f3(2))))</code>. But using <code>console.log</code> I see that the evaluation order is <code>f2(f3(f4(f5(2))))</code> which is also 240 by coincidence.</p>
  
  <p>As far as I understand the function <code>y</code> is called for all array elements so why only the last function gets <code>2</code> as the parameter?</p>