title: Chaining and Function Composition
body: <p>Here is a piece of code I am trying to understand:</p>
  
  <pre><code>const seq2 = (f1, f2) =&gt; {
   return (...args) =&gt; {
  
    return f2( f1( ...args) );
   }
  }
  
  
  
  const seq = ( f1, ...fRest) =&gt;
   fRest.reduce( seq2, f1 );
  
  const elevator = {
   floor: 5
  };
  
  const up = elevator =&gt; {
   return {
    floor: elevator.floor + 1
   }
  };
  
  const down = elevator =&gt; {
   return {
    floor: elevator.floor - 1
   }
  };
  
  
  const move = seq( up, up, down, up);
  const newElevator = move( elevator );
  
  
  
  console.log( newElevator.floor ) // shows 7
  </code></pre>
  
  <p>this is an example from js functional programming course. And I am trying to figure out if I could simplify seq function so it would look like this </p>
  
  <p><code>const seq = ( ...fRest) =&gt;
   fRest.reduce( seq2 );</code></p>
  
  <p>????</p>
  
  <p>Is there any specific reason why I have to pass <code>f1</code> as a first argument and then pass it further to the reduce method as an <code>initialValue</code> ?? When I won't specify the <code>initialValue</code> in the reduce method wouldn't it treat the first array element - <code>accumulator</code> - as an <code>initialValue</code> by default? I will much appreciate if someone could explain to me what is going on in that code :)</p>