Curried functions
Michał Wadas
michalwadas at gmail.com
Fri Oct 16 18:48:11 UTC 2015
We can reverse this argument - why we needed Function.prototype.bind? It's
quite easy to polyfill (especially if you have Reflect.construct for edge
case of constructor or use eval for ES3/5 compatible implementation). There
were hundreds libraries providing .bind.
Returning threads about partially applied function without writing
boilerplate code or using external library are good argument to consider
implementing it into language.
And in my opinion method is more readable than function.
On Oct 16, 2015 8:36 PM, "Isiah Meadows" <isiahmeadows at gmail.com> wrote:
> An npm package (not mine): https://npm.im/curry
>
> Another simple implementation, ES3-compatible:
>
> ```js
> function curry(f) {
> function curried() {
> if (arguments.length >= f.length) {
> return f.apply(this, arguments);
> } else {
> var args = []
> for (var i = 0; i < arguments.length; i++) {
> args.push(arguments[i]);
> }
> return function () {
> var rest = args.slice();
> for (var i = 0; i < arguments.length; i++) {
> rest.push(arguments[i]);
> }
> return curried.apply(this, rest);
> }
> }
> }
> Object.defineProperty(curried, "length",
> Object.getOwnPropertyDescriptor(f, "length"));
> return curried;
> }
> ```
>
> I don't need it often enough to want a standard library feature, but it's
> an easy function to make. And it does exactly what you want it to: curry a
> function without binding `this`. If you want to make it a method decorator
> as well, you can use this (also ES3-compatible):
>
> ```js
> function helper(f) {
> function curried() {
> if (arguments.length >= f.length) {
> return f.apply(this, arguments);
> } else {
> var args = []
> for (var i = 0; i < arguments.length; i++) {
> args.push(arguments[i]);
> }
> return function () {
> var rest = args.slice();
> for (var i = 0; i < arguments.length; i++) {
> rest.push(arguments[i]);
> }
> return curried.apply(this, rest);
> }
> }
> }
> Object.defineProperty(curried, "length",
> Object.getOwnPropertyDescriptor(f, "length"));
> return curried;
> }
>
> function curry(target, prop, desc) {
> if (typeof target === "function") {
> // called on function
> return helper(target);
> } else {
> // called as decorator
> desc.value = helper(desc.value);
> }
> }
> ```
>
> On Fri, Oct 16, 2015, 05:39 Michał Wadas <michalwadas at gmail.com> wrote:
>
>>
>> 2015-10-15 22:20 GMT+02:00 Yongxu Ren <renyongxu at gmail.com>:
>>
>>>
>>> First, I do not think anyone will ever intended to write code like this,
>>>
>>
>>
>> It's a wrong assumption - tons of library code depends on some argument
>> being null or undefined.
>>
>> function ajax(url, options) {
>> options = options || defaultOptions;
>> ...
>> }
>>
>> And in user code it can be called:
>>
>> ajax('menu.html');
>> ajax('menu.html', null);
>> ajax('menu.html', undefined);
>>
>>
>> BTW - I think ES2016 needs some way to curry function without binding
>> `this`. Function.prototype.curry or something like that.
>>
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151016/a65ee54a/attachment.html>
More information about the es-discuss
mailing list