Looking at your code more closely I am left wondering if the
"need to reset" more properly belongs to a previous version
of the function. Rewritten to take out the outer variables
which are hidden by parameter names in nested function
"accummulator", I got:
function curry(func,args,space)
}
----
Curried functions appears to copy existing, accumulated arguments
into a formal parameter array passed to "accumulator" executing
and creating a closure one level down from the call. Effectively
this parameter array is the "new accumulation" array I saw as
logically required.
What I now can't see, or find examples for, is why there would be
a need to restore the parameter array when a nested call to
"accumulator" executes "func" - should not the copy of accumulated
arguments in its 2nd formal parameter go out of existence anyway?
This is an intriguing problem and I thought that I would have a go at
it. I didn't like the idea of accumulating the parameters in an array so
I traded that off against an increased number of function objects, but
along the way I gained the ability to pass some, or all, of the
arguments to the original call to the curry function with the function
that is to be executed.
This is my version:-
<html>
<head>
<title></title>
<script type="text/javascript">
function curry(f){
var obj = this; //global, unless this function
//is assigned as a method.
arguments.callee.depth = 0;
function getArgFunc(args, af){
function argFunc(x){
var retVal;
var self = arguments.callee;
function getArgs(){
var ar = self.getArgs();
ar[ar.length] = x;
return ar;
}
if(self.depth == f.length){
retVal = f.apply(obj, getArgs());
}else{
retVal = getArgFunc(arguments, getArgs);
}
return retVal;
}
argFunc.depth = args.callee.depth+1;
argFunc.getArgs = af;
for(var c = 1;c < args.length;c++){
argFunc = argFunc(args[c]);
if((typeof argFunc != 'function')||(!argFunc.getArgs)){
break;
}
}
args = null;
return argFunc;
}
return getArgFunc(arguments, function(){return [];});
}
</script>
</head>
<body>
<script type="text/javascript">
function add(x,y,z,a) {return x+','+y+','+z+','+a;}
var cadd = curry(add);
var cadd1 = cadd(1);
var cadd2 = cadd(2);
var cadd13 = cadd1(3);
var cadd15 = cadd1(5);
var cadd27 = cadd2(7);
var cadd13A = cadd13(10);
var cadd159 = cadd15(9);
var cadd27D = cadd27(13)
document.write('cadd13A(6) = '+cadd13A(6)+' :<br>');
document.write('cadd159(8) = '+cadd159(8)+' :<br>');
document.write('cadd27D(5) = '+cadd27D(5)+' :<br>');
document.write('cadd13A(1) = '+cadd13A(1)+' :<br>');
var cadd134 = cadd13(4);
document.write('cadd134(5) = '+cadd134(5)+' :<br>');
var cadd12 = cadd(1,2);
var cadd123 = cadd12(3);
document.write('cadd123(5) = '+cadd123(5)+' :<br>');
document.write('cadd12(6,7) = '+cadd12(6,7)+' :<br>');
document.write('curry(add,4,5,6,7) = '+curry(add,4,5,6,7)+' :<br>');
document.write('cadd2(4,6,8) = '+cadd2(4,6,8)+' :<br>');
document.write('cadd27(100,120) = '+cadd27(100,120)+' :<br>');
var cadd98 = curry(add,9,8)
document.write('cadd98(7,6) = '+cadd98(7,6)+' :<br>');
</script>
</body>
</html>
Richard.