"" - , , , .
- , .
curry, , , .
function curry() {
var args = Array.prototype.slice.call(arguments);
return function(fn) {
return function() {
var args2 = Array.prototype.slice.call(arguments);
return fn.apply(this,args.concat(args2));
};
};
}
,
, , ...
var workWithName = curry("Bubba");
... , , .
, curried
talkToName, ...
var talkToName = workWithName(function(curried_str, before, after) {
return before + curried_str + after;
});
, talkToName, 2 , .
talkToName("Hello there ", ". How are you?");
talkToName("", " is really super awesome.");
, talkToName, , workWithName, 3 .
, workWithName(), , talkToName, .
, workWithName, "Bubba" , ...
var incrementName = workWithName(function(curried_str, n) {
var ret = '';
for(var i = 0; i < curried_str.length; i++) {
ret += String.fromCharCode(curried_str[i].charCodeAt() + n);
}
return ret;
});
, incrementName ...
incrementName(3); // "Exeed"
incrementName(8); // "J}jji"
incrementName(0); // "Bubba"
, , curry() , , , .
, incrementName, , workWithName, 2 . curried.
, , 3 5.
var workWith3And5 = curry(3, 5);
,
, workWith3And5, , ...
var addNTo3And5 = workWith3And5(function(x, y, n) {
return [3 + n, 5 + n];
});
addNTo3And5( 8 );
addNTo3And5( -4 );
, workWith3And5, 3 5, 3 x 5, ...
var create3By5GridWithData = workWith3And5(function(x, y, data) {
var ret = []
for(var i = 0; i < x; i++) {
ret[i] = [];
for(var j = 0; j < y; j++) {
ret[i][j] = data;
}
}
return ret;
});
create3By5GridWithData( 'content' );