Function.prototype.partial
(note, it couldn't be a direct proxy to bind
because bind(null)
permanently sets the "this" value (ie, (function () { return this }).bind(null).call([])
will return null or the global object, not the
empty array. the polyfill would be a bit trickier, and couldn't use "bind"
under the hood without changes to bind itself)
(note, it couldn't be a direct proxy to `bind` because `bind(null)` permanently sets the "this" value (ie, `(function () { return this }).bind(null).call([])` will return null or the global object, not the empty array. the polyfill would be a bit trickier, and couldn't use "bind" under the hood without changes to bind itself) On Sun, Oct 4, 2015 at 8:06 AM, Tim Ruffles <oi at truffles.me.uk> wrote: > It'd be nice to add Function.prototype.partial, which is exactly like > `.bind` without the `this`-setting first argument. > > Adding it would have benefits: > > - `.bind(null, ...` is newbie confusing and ugly in functional code[1] > - `.bind(null, ...` is a very common idiom[2] > - `.partial` is intention revealing: it's clear you're writing functional > code that doesn't care about `this` > > Adding it seems low cost, and is backwards compatible: > > - implementors could literally make it a proxy to `.bind` if they want to > get it done fast > - shimmable > - not new functionality, just a subset of existing functionality > > Cheers, > > Tim > > @timruffles > > - [1] I've been teaching newbies Javascript/Node.js for years, and if you > take a 'functions first' approach it's complexity you have to tell them to > ignore > - [2] see github - > https://github.com/search?l=javascript&q=%22.bind%28null%22&ref=searchresults&type=Code&utf8=%E2%9C%93 > > _______________________________________________ > 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/20151004/7690c986/attachment-0001.html>
I think his point was that in the functional programming way he is thinking
about, you don't care about this
at all so you can shim partial
like:
Object.defineProperty(
Function.prototype,
'partial',
{
writable: true,
configurable: true,
value: function partial() {
for (var a = [null], i = 0; i < arguments.length; a[++i] =
arguments[i - 1]);
return this.bind.apply(this, a);
}
}
);
The dynamic this
alternative seems quite straight forward anyway
Object.defineProperty(
Function.prototype,
'partial',
{
writable: true,
configurable: true,
value: function partial() {
for (var f = this, a = [], i = 0; i < arguments.length; a[i] =
arguments[i++]);
return function () { a.push.apply(a, arguments); return f.apply(this,
a); };
}
}
);
I think his point was that in the functional programming way he is thinking about, you don't care about `this` at all so you can shim `partial` like: ```js Object.defineProperty( Function.prototype, 'partial', { writable: true, configurable: true, value: function partial() { for (var a = [null], i = 0; i < arguments.length; a[++i] = arguments[i - 1]); return this.bind.apply(this, a); } } ); ``` The dynamic `this` alternative seems quite straight forward anyway ```js Object.defineProperty( Function.prototype, 'partial', { writable: true, configurable: true, value: function partial() { for (var f = this, a = [], i = 0; i < arguments.length; a[i] = arguments[i++]); return function () { a.push.apply(a, arguments); return f.apply(this, a); }; } } ); ``` Regards On Sun, Oct 4, 2015 at 8:04 PM, Jordan Harband <ljharb at gmail.com> wrote: > (note, it couldn't be a direct proxy to `bind` because `bind(null)` > permanently sets the "this" value (ie, `(function () { return this > }).bind(null).call([])` will return null or the global object, not the > empty array. the polyfill would be a bit trickier, and couldn't use "bind" > under the hood without changes to bind itself) > > On Sun, Oct 4, 2015 at 8:06 AM, Tim Ruffles <oi at truffles.me.uk> wrote: > >> It'd be nice to add Function.prototype.partial, which is exactly like >> `.bind` without the `this`-setting first argument. >> >> Adding it would have benefits: >> >> - `.bind(null, ...` is newbie confusing and ugly in functional code[1] >> - `.bind(null, ...` is a very common idiom[2] >> - `.partial` is intention revealing: it's clear you're writing functional >> code that doesn't care about `this` >> >> Adding it seems low cost, and is backwards compatible: >> >> - implementors could literally make it a proxy to `.bind` if they want to >> get it done fast >> - shimmable >> - not new functionality, just a subset of existing functionality >> >> Cheers, >> >> Tim >> >> @timruffles >> >> - [1] I've been teaching newbies Javascript/Node.js for years, and if you >> take a 'functions first' approach it's complexity you have to tell them to >> ignore >> - [2] see github - >> https://github.com/search?l=javascript&q=%22.bind%28null%22&ref=searchresults&type=Code&utf8=%E2%9C%93 >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > _______________________________________________ > 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/20151004/a39da737/attachment.html>
for code sake, the dynamic should copy and push, not just push
Object.defineProperty(
Function.prototype,
'partial',
{
writable: true,
configurable: true,
value: function partial() {
for (var f = this, a = [], i = 0; i < arguments.length; a[i] =
arguments[i++]);
return function () {
var args = a.slice(0);
args.push.apply(a, arguments);
return f.apply(this, a);
};
}
}
);
for code sake, the dynamic should copy and push, not just push ```js Object.defineProperty( Function.prototype, 'partial', { writable: true, configurable: true, value: function partial() { for (var f = this, a = [], i = 0; i < arguments.length; a[i] = arguments[i++]); return function () { var args = a.slice(0); args.push.apply(a, arguments); return f.apply(this, a); }; } } ); ``` Regards On Sun, Oct 4, 2015 at 10:04 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > I think his point was that in the functional programming way he is > thinking about, you don't care about `this` at all so you can shim > `partial` like: > > ```js > Object.defineProperty( > Function.prototype, > 'partial', > { > writable: true, > configurable: true, > value: function partial() { > for (var a = [null], i = 0; i < arguments.length; a[++i] = > arguments[i - 1]); > return this.bind.apply(this, a); > } > } > ); > ``` > > The dynamic `this` alternative seems quite straight forward anyway > > ```js > Object.defineProperty( > Function.prototype, > 'partial', > { > writable: true, > configurable: true, > value: function partial() { > for (var f = this, a = [], i = 0; i < arguments.length; a[i] = > arguments[i++]); > return function () { a.push.apply(a, arguments); return > f.apply(this, a); }; > } > } > ); > ``` > > Regards > > > > > On Sun, Oct 4, 2015 at 8:04 PM, Jordan Harband <ljharb at gmail.com> wrote: > >> (note, it couldn't be a direct proxy to `bind` because `bind(null)` >> permanently sets the "this" value (ie, `(function () { return this >> }).bind(null).call([])` will return null or the global object, not the >> empty array. the polyfill would be a bit trickier, and couldn't use "bind" >> under the hood without changes to bind itself) >> >> On Sun, Oct 4, 2015 at 8:06 AM, Tim Ruffles <oi at truffles.me.uk> wrote: >> >>> It'd be nice to add Function.prototype.partial, which is exactly like >>> `.bind` without the `this`-setting first argument. >>> >>> Adding it would have benefits: >>> >>> - `.bind(null, ...` is newbie confusing and ugly in functional code[1] >>> - `.bind(null, ...` is a very common idiom[2] >>> - `.partial` is intention revealing: it's clear you're writing >>> functional code that doesn't care about `this` >>> >>> Adding it seems low cost, and is backwards compatible: >>> >>> - implementors could literally make it a proxy to `.bind` if they want >>> to get it done fast >>> - shimmable >>> - not new functionality, just a subset of existing functionality >>> >>> Cheers, >>> >>> Tim >>> >>> @timruffles >>> >>> - [1] I've been teaching newbies Javascript/Node.js for years, and if >>> you take a 'functions first' approach it's complexity you have to tell them >>> to ignore >>> - [2] see github - >>> https://github.com/search?l=javascript&q=%22.bind%28null%22&ref=searchresults&type=Code&utf8=%E2%9C%93 >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> >> _______________________________________________ >> 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/20151004/b393872e/attachment-0001.html>
I agree that it would be nice. I frequently come up with a helper function for that, although my lazy fingers don't feel like typing 7 letters each time to create a partially applied function...
// Maybe Function.prototype.part?
list.map(create.part('type'))
I agree that it would be nice. I frequently come up with a helper function for that, although my lazy fingers don't feel like typing 7 letters each time to create a partially applied function... ```js // Maybe Function.prototype.part? list.map(create.part('type')) ``` On Sun, Oct 4, 2015 at 4:07 PM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > for code sake, the dynamic should copy and push, not just push > > ```js > Object.defineProperty( > Function.prototype, > 'partial', > { > writable: true, > configurable: true, > value: function partial() { > for (var f = this, a = [], i = 0; i < arguments.length; a[i] = > arguments[i++]); > return function () { > var args = a.slice(0); > args.push.apply(a, arguments); > return f.apply(this, a); > }; > } > } > ); > ``` > > Regards > > > On Sun, Oct 4, 2015 at 10:04 PM, Andrea Giammarchi > <andrea.giammarchi at gmail.com> wrote: >> >> I think his point was that in the functional programming way he is >> thinking about, you don't care about `this` at all so you can shim `partial` >> like: >> >> ```js >> Object.defineProperty( >> Function.prototype, >> 'partial', >> { >> writable: true, >> configurable: true, >> value: function partial() { >> for (var a = [null], i = 0; i < arguments.length; a[++i] = >> arguments[i - 1]); >> return this.bind.apply(this, a); >> } >> } >> ); >> ``` >> >> The dynamic `this` alternative seems quite straight forward anyway >> >> ```js >> Object.defineProperty( >> Function.prototype, >> 'partial', >> { >> writable: true, >> configurable: true, >> value: function partial() { >> for (var f = this, a = [], i = 0; i < arguments.length; a[i] = >> arguments[i++]); >> return function () { a.push.apply(a, arguments); return >> f.apply(this, a); }; >> } >> } >> ); >> ``` >> >> Regards >> >> >> >> >> On Sun, Oct 4, 2015 at 8:04 PM, Jordan Harband <ljharb at gmail.com> wrote: >>> >>> (note, it couldn't be a direct proxy to `bind` because `bind(null)` >>> permanently sets the "this" value (ie, `(function () { return this >>> }).bind(null).call([])` will return null or the global object, not the empty >>> array. the polyfill would be a bit trickier, and couldn't use "bind" under >>> the hood without changes to bind itself) >>> >>> On Sun, Oct 4, 2015 at 8:06 AM, Tim Ruffles <oi at truffles.me.uk> wrote: >>>> >>>> It'd be nice to add Function.prototype.partial, which is exactly like >>>> `.bind` without the `this`-setting first argument. >>>> >>>> Adding it would have benefits: >>>> >>>> - `.bind(null, ...` is newbie confusing and ugly in functional code[1] >>>> - `.bind(null, ...` is a very common idiom[2] >>>> - `.partial` is intention revealing: it's clear you're writing >>>> functional code that doesn't care about `this` >>>> >>>> Adding it seems low cost, and is backwards compatible: >>>> >>>> - implementors could literally make it a proxy to `.bind` if they want >>>> to get it done fast >>>> - shimmable >>>> - not new functionality, just a subset of existing functionality >>>> >>>> Cheers, >>>> >>>> Tim >>>> >>>> @timruffles >>>> >>>> - [1] I've been teaching newbies Javascript/Node.js for years, and if >>>> you take a 'functions first' approach it's complexity you have to tell them >>>> to ignore >>>> - [2] see github - >>>> https://github.com/search?l=javascript&q=%22.bind%28null%22&ref=searchresults&type=Code&utf8=%E2%9C%93 >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -- Isiah Meadows
It'd be nice to add Function.prototype.partial, which is exactly like
.bind
without thethis
-setting first argument.Adding it would have benefits:
.bind(null, ...
is newbie confusing and ugly in functional code[1].bind(null, ...
is a very common idiom[2].partial
is intention revealing: it's clear you're writing functional code that doesn't care aboutthis
Adding it seems low cost, and is backwards compatible:
.bind
if they want to get it done fastCheers,
Tim
@timruffles