Friday, February 15, 2008

curry - functional javascript


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>curry - functional javascript</title>
<script type="text/javascript">
Function.prototype.curry = function() {
var fn = this, args = Array.prototype.slice.call(arguments);
//alert(fn); // function slice() { native code }
//alert(args); // 2
return function() {
//alert(this); // ['t', 'e', 's', 't']
//alert(args.concat(Array.prototype.slice.call(arguments)));
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};
var arr = 'test'.split('');
Array.prototype.yaslice = Array.prototype.slice.curry(2);
alert(arr.yaslice(3)); // arr.slice(2,3) => 's'
</script>
</head>
<body>

</body>
</html>

references:
1. http://ejohn.org/blog/partial-functions-in-javascript/
2. http://osteele.com/sources/javascript/functional/

No comments :