Skip to content

Instantly share code, notes, and snippets.

@thurt
Created January 11, 2016 17:22
Show Gist options
  • Save thurt/6a8a55db4be4b6b53e03 to your computer and use it in GitHub Desktop.
Save thurt/6a8a55db4be4b6b53e03 to your computer and use it in GitHub Desktop.
uncurrying() in JavaScript.
Function.prototype.uncurrying = function() {
var _this = this;
return function() {
return Function.prototype.call.apply(_this, arguments);
};
};
/*var push = Array.prototype.push.uncurrying();
var a = [];
push(a, 1, 2, 'zensh');
console.log(a);
var concat = String.prototype.concat.uncurrying();
var b = concat('Hello,', 'zensh!');
console.log(b);*/
@thurt
Copy link
Author

thurt commented Apr 26, 2016

Here is an ES6 version to consider:

Function.prototype.uncurrying = function() {
  return (...x) => this.call(...x)
}

The first parameter to #call is the intended this value.
In the case of push(a, 1, 2, 'zensh'), the a array will be the this value.
In the case of concat('Hello,', 'zensh!'), the 'Hello' will be the this value. However, this must be an object so 'Hello' is implicitly converted to an object using the String object wrapper.

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