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 <michalwa...@gmail.com> wrote:

>
> 2015-10-15 22:20 GMT+02:00 Yongxu Ren <renyon...@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@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to