title: Currying with functions that take unlimited arguments
body: <p>Lets say i have the following add function that takes an unlimited number of arguments.</p>
  
  <pre><code>function add () {
      var total = 0;
      var args = Array.prototype.slice.call(arguments, 0);
      for (var i=0; i&lt;args.length; i++) {
          total += arguments[i];
      }
      return total;
  }
  </code></pre>
  
  <p>and the following curry function.</p>
  
  <pre><code>function curryFunction(orig_func) {
      var ap = Array.prototype;
      var args = arguments;
      function fn() {
          if (arguments.length != 0) {
              ap.push.apply(fn.args, arguments);
              return fn;
          } else {
              return orig_func.apply(this, fn.args);
          }
      };
      return function() {
          fn.args = ap.slice.call( args, 1 );
          return fn.apply( this, arguments );
      };
  }
  </code></pre>
  
  <p>I then want to do something like:</p>
  
  <pre><code>var f = curryFunction(add);
  var a = f(3)(4)(3);
  var b = f(10)(3);
  var result1 = a(); // returns 10
  var result2 = b(); // returns 13 
  </code></pre>
  
  <p>However i always get 13 for both a() and b() i assume is because in line</p>
  
  <pre><code>fn.args = ap.slice.call(args, 1);
  </code></pre>
  
  <p>the existing array [3,4,3] is overwriting with []. Can someone please provide me with a hint on how to make this work? Thanks</p>