Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created October 5, 2011 07:04
Show Gist options
  • Save ishiduca/1263830 to your computer and use it in GitHub Desktop.
Save ishiduca/1263830 to your computer and use it in GitHub Desktop.
Function#papply と Function#lessCurry
var sum = function () {
for (var v = 0,i = 0,l = arguments.length; i < l; v += arguments[i++]); return v;
};
console.log(sum(2,3,4)); // 9
// これは部分適用というらしい
if (! Function.prototype.papply) {
Function.prototype.papply = function () {
var slice = Array.prototype.slice,
func = this,
args = slice.apply(arguments);
return function () {
args = args.concat(slice.apply(arguments));
return func.apply(null, args);
};
};
}
console.log(f.papply(2)(3, 4)); // 9
console.log(f.papply(2).papply(3)(4)); // 9
// これだと最後に () を付けないと駄目だ
if (! Function.prototype.lessCurry) {
Function.prototype.lessCurry = function (firstArg) {
var func = this;
var args = [ firstArg ];
var callback;
callback = function (arg) {
if (typeof arg === 'undefined') {
return func.apply(null, args);
} else {
args.push(arg);
return callback;
}
};
return callback;
};
}
console.log(f.lessCurry(2)(3)(4)(5)()); // 14
@ishiduca
Copy link
Author

ishiduca commented Oct 5, 2011

カリー化するメソッドが作りたいんだよー!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment