On Mon, Oct 10, 2011 at 1:33 PM, Dean Landolt <[email protected]> wrote:

>
>
> On Wed, Oct 5, 2011 at 1:40 PM, Bob Nystrom <[email protected]> wrote:
>
>>
>>
>> On Wed, Oct 5, 2011 at 7:18 AM, John J Barton <
>> [email protected]> wrote:
>>
>>>
>>>
>>> On Tue, Oct 4, 2011 at 2:33 PM, Bob Nystrom <[email protected]> wrote:
>>>
>>>> That's correct. That's often the cost of concision. By analogy: you can
>>>> do a lot of stuff using either an explicit stack data structure or
>>>> recursion. Using recursion is often more concise but then the state is
>>>> hidden from you in the callstack. Sometimes that's a good trade-off,
>>>> sometimes it's not.
>>>
>>>
>>> Thanks, that analogy helps confirm my opinion of generators. In my
>>> experience, the good trade-off is recursion on data structures and the
>>> not-good-trade-off is recursion for iteration.
>>>
>>
>> Yes, I wasn't making any claim about using recursion *for iteration*.
>> Maybe I need to be more concrete here:
>>
>> If you need to walk a tree, you can do so using recursion or you can use
>> an explicit stack data structure. The former is usually more terse (because
>> it implicitly relies on the callstack itself storing the stack) but the
>> other can be more clear.
>>
>> *By analogy*, if you need to produce a sequence of values, you can do so
>> using generators, or you can use an explicit iterator object. Again, the
>> former is usually more terse (because the language automatically creates the
>> iterator object for you given a function containing yield) but the latter
>> can be more clear (because the state is laid bare).
>>
>> Note that those two paragraphs are separate. I'm not talking about using
>> recursion for iteration. Is this clearer now?
>>
>> So far the only examples I've seen for generators involve iteration.
>>
>>
>> Yup, that's what generators do. And recursion involves stacks.
>>
>> I've heard no clamor at all.
>>>
>>
>> That may be true, but it may say as much about your surrounding auditory
>> environment as it does the feature in question. You may be right, but simply
>> saying "Bigfoot isn't in my living room" doesn't disprove the existence of
>> Bigfoot.
>>
>> There *are* languages in wide use that have this feature. None of those
>> languages (Lua, C#, Python) has a reputation for catering to the esoteric
>> programming language fanbase.
>>
>>  Really this is a niche feature, an aid for the rare case where
>>> conventional iterators are a poor match.
>>>
>>
>> They said the same thing about closures. I agree that generators aren't
>> the most important language feature in the world, but I think if you had
>> them you'd find that you used them.
>>
>> Here's an example:
>>
>> let tree = [['a', 'b', 'c'], [['d', 'e'], 'f'], ['g']];
>>
>> let inOrder = walk(tree);
>> for (node of walk(tree)) alert(node); // a, b, c, d, ...
>>
>> walk(tree) {
>>   if (typeof tree == 'string') {
>>     yield tree; // leaf
>>   } else {
>>     yield* walk(tree); // branch
>>   }
>> }
>>
>>
>> What would the above look like without generators?
>>
>
>
> Woah there -- where does this syntax come from? Was `walk(tree) {...}`
> supposed to be `function* walk(tree) {...}` instead? Or is this intended to
> represent some sort of method generator? If the latter, doesn't this need
> some mechanism to distinguish it from a non-generator method?
>

Whoops, my mistake. Too much time working on the class proposal where
"function" isn't needed for methods. Yes, that should be function*.

- bob
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to