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

