Named Arrow Functions
Salvador de la Puente González
salva at unoyunodiez.com
Wed Aug 12 06:12:09 UTC 2015
Ups, I answered only to Jacob but I think this is a real simple solution:
var factorial;
x.map(factorial = x => x < 1 ? 1 : x * factorial(x-1));
Or simply:
var factorial = x => x < 1 ? 1 : x * factorial(x-1);
x.map(factorial);
As you said, this is ocassionally needed. And for event handlers:
var once = evt => { button.onclick = null; onClick(evt) };
button.onclick = once;
And yes, this solution was already provided:
https://esdiscuss.org/topic/self-recursion-and-arrow-functions#content-10
On Wed, Aug 12, 2015 at 4:00 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:
> And as Kevin said, it has been mentioned before (with event handlers and
> timeouts as the initial driving force).
>
> https://esdiscuss.org/topic/self-recursion-and-arrow-functions
>
> On Tue, Aug 11, 2015, 21:57 Isiah Meadows <isiahmeadows at gmail.com> wrote:
>
>> Sent this too early... Corrected inline.
>>
>> On Tue, Aug 11, 2015, 21:56 Isiah Meadows <isiahmeadows at gmail.com> wrote:
>>
>> The real reason people need named arrow functions, the biggest use case
>> is for event handlers.
>>
>> ```js
>> let p = new Promise((resolve, reject) =>
>> setTimeout((x => () => x(x))(handler => {
>> onNotNeeded(() => clearTimeout(handler));
>>
>> // `return` is to take advantage of TCO
>> return doSomethingAsync(err => {
>> if (err != null) return reject(err)
>> else return resolve();
>> });
>> }));
>> ```
>>
>> By the way, the way I created a self reference is a complete lambda
>> calculus hack.
>>
>> <deviation>
>> If you'd like your eyes to bleed, here's one that is purely out if
>> lambdas. I couldn't help myself.
>>
>> ```js
>> let p = new Promise((resolve, reject) =>
>> setTimeout((x => () => x(x))(h =>
>> x => y => y(x()))(
>> onNotNeeded(() => clearTimeout(h)))(
>> doSomethingAsync(e =>
>>
>>
>> e
>>
>> != null
>>
>> ?
>>
>> reject(err)
>>
>> :
>>
>> resolve()
>>
>> )))
>>
>> );
>> ```
>>
>> On Tue, Aug 11, 2015, 20:45 Leonardo Wolter <leocwolter at gmail.com> wrote:
>>
>> Well, I found out arguments is actually a reserved word too haha
>>
>> About that:
>>
>> If they're not from the tiny set of remaining reserved words
>> (enum, anyone?), they can be users' identifiers, and have to be based
>> contextually on some enclosing syntax, like yield is.
>>
>> That could be it, right? Since it would be only available at arrow
>> functions(anon functions too?)
>>
>> 2015-08-11 21:42 GMT-03:00 Leonardo Wolter <leocwolter at gmail.com>:
>>
>> Yeah., that's what I meant.
>>
>> My proposal is not a keyword, but an hidden variable included at
>> functions (e.g. arguments).
>>
>> Does arrow functions have any limitations about that?
>>
>> 2015-08-11 21:35 GMT-03:00 Daniel Ehrenberg <dehrenberg at chromium.org>:
>>
>> I assume you mean more like this (without factorial):
>>
>> x.map((x) => do {
>> if (x <= 1) {
>> 1;
>> } else {
>> x * recur(x - 1)
>> }
>> });
>>
>> One issue is that it's hard to add keywords to JavaScript at this
>> point. If they're not from the tiny set of remaining reserved words
>> (enum, anyone?), they can be users' identifiers, and have to be based
>> contextually on some enclosing syntax, like yield is.
>>
>> Another downside is that then, arrow functions have a distinct and
>> less powerful method of recursing (e.g., nested functions won't be
>> able to see the binding to the outer one).
>>
>> Dan
>>
>> On Tue, Aug 11, 2015 at 5:30 PM, Leonardo Wolter <leocwolter at gmail.com>
>> wrote:
>> > What about a clojure like recur hidden variable binded to the
>> bottom-level
>> > function?
>> >
>> > x.map(factorial(x) => do {
>> > if (x <= 1) {
>> > 1;
>> > } else {
>> > x * recur(x - 1)
>> > }
>> > });
>> >
>> > 2015-08-11 21:26 GMT-03:00 Daniel Ehrenberg <dehrenberg at chromium.org>:
>> >>
>> >> In addition to being hard to parse in general, I don't think this
>> >> would play very well with the async/await proposal
>> >> https://tc39.github.io/ecmascript-asyncawait/ , which wants to have
>> >> arrow functions like
>> >>
>> >> async (x) => ...
>> >>
>> >> Because we can't count on async as a keyword, your proposal would
>> >> create an ambiguity.
>> >>
>> >> On Tue, Aug 11, 2015 at 1:49 PM, Jacob Parker <
>> jacobparker1992 at gmail.com>
>> >> wrote:
>> >> > I did look, but couldn’t find anything on named arrow functions were
>> not
>> >> > included. I do sometimes find cases where I want recursion inside a
>> class
>> >> > function definition, and still need access to `this`. Was it just
>> seen as
>> >> > syntax bloat, or were there any complications to implementing it?
>> >> >
>> >> > Obviously a contrived example, but something like this (using do
>> syntax
>> >> > too)
>> >> >
>> >> > x.map(factorial(x) => do {
>> >> > if (x <= 1) {
>> >> > 1;
>> >> > } else {
>> >> > x * factorial(x - 1)
>> >> > }
>> >> > });
>> >> >
>> >> > _______________________________________________
>> >> > 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
>>
>>
>>
> _______________________________________________
> 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/20150812/d4ca00c1/attachment-0001.html>
More information about the es-discuss
mailing list