Function Currying
see also:
http://en.wikipedia.org/wiki/Currying
http://rest-term.com/archives/2857/
/**
* Copyright wellflat ( http://wonderfl.net/user/wellflat )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dh0z
*/
package {
import flash.display.Sprite;
import flash.text.TextField;
public class FunctionCurrying extends Sprite {
public function FunctionCurrying() {
var tf:TextField = new TextField();
addChild(tf);
var curriedSum:Function = FunctionUtils.curry(sum);
var result1:int = curriedSum(1)(2,3)(4,5,6)(7,8,9,10);
var result2:int = curriedSum(1)()()()()()()()()()
()(2)()()()()()()()()
()()(3)()()()()()()()
()()()(4)()()()()()()
()()()()(5)()()()()()
()()()()()(6)()()()()
()()()()()()(7)()()()
()()()()()()()(8)()()
()()()()()()()()(9)()
()()()()()()()()()(10);
tf.text = result1.toString() + ", " + result2.toString();
}
public function sum(a:int, b:int, c:int, d:int, e:int,
f:int, g:int, h:int, i:int, j:int):int {
return a + b + c + d + e + f + g + h + i + j;
}
}
}
class FunctionUtils {
// bind (== partial)
public static function bind(f:Function, ...args):Function {
return function(...rest):* {
return f.apply(null, args.concat(rest));
};
}
// currying
public static function curry(f:Function, ...rest):Function {
function currying(args:Array):* {
if(args.length >= f.length) {
return f.apply(null, args);
}
return function(...more):* {
return currying(args.concat(more));
};
}
return currying(rest);
}
}