HTML and CSS Reference
In-Depth Information
Listing 6.8 Implementing a method in terms of another one and curry
(function () {
String.prototype.trim =
String.prototype.replace.curry(/^\s+ | \s+ $ /g, "");
TestCase("CurryTest", {
"test should trim spaces": function () {
var str = "
some spaced string
";
assertEquals("some spaced string", str.trim());
}
});
}());
The implementation of curry in Listing 6.9 resembles the bind implementa-
tion from before.
Listing 6.9 Implementing curry
if (!Function.prototype.curry) {
(function () {
var slice = Array.prototype.slice;
Function.prototype.curry = function () {
var target = this;
var args = slice.call(arguments);
return function () {
var allArgs = args;
if (arguments.length > 0) {
allArgs = args.concat(slice.call(arguments));
}
return target.apply(this, allArgs);
};
};
}());
}
There's no optimization for the case in which curry does not receive argu-
ments, because calling it without arguments is senseless and should be avoided.
 
Search WWH ::




Custom Search