This fell off the list. Re-adding it as requested. :) - bob
---------- Forwarded message ---------- From: Bob Nystrom <[email protected]> Date: Wed, Oct 5, 2011 at 1:39 PM Subject: Re: On "I got 99 problems and JavaScript syntax ain't one" To: John J Barton <[email protected]> On Wed, Oct 5, 2011 at 11:06 AM, John J Barton <[email protected]>wrote: > > I think I was the one that was not clear. Recursion on a data structure is > a bit tricky but at least the call stack gives you a cursor into a data > structure as a guide to the state of the processing. I've had good > experience with it. My brief investigation of generators confused me because > I couldn't find the state of the iterator in the debugger. > Yeah, that's a bit annoying. The callframe for the generator has been reified and moved aside. If you put a breakpoint in the generator function, you can get to it in your debugger the next time the iterator is iterated. > Sorry, I just can't imagine that generators will ever be used any > significant fraction of the current use of closures in JS. > The new built-in methods for iterating over objects (keys(), values() and items()) are defined using them. They won't be used everywhere, but they will be used. They're like a cheese grater: not something you use for every meal, but unbelievably convenient for the meals that require them. > > var tree = [['a', 'b', 'c'], [['d', 'e'], 'f'], ['g']]; > > function walkLeaf(leaf) { > console.log(leaf); > } > > function walkTree(tree) { > tree.forEach(function(treeOrLeaf) { > if (typeof treeOrLeaf == 'string') { > walkLeaf(treeOrLeaf); > } else { > walkTree(treeOrLeaf); > } > }); > } > > Your walkTree() isn't a generic function for traversing a tree like walk(), it's a specific function for logging a tree. You could change it to take a callback, of course, but then the callers lose the ability to do flow control while iterating. How would you handle this? function findLeaf(tree, leaf) { for (node of walk(tree)) { if (node == leaf) return node; } } Or: function partition(tree, cut) { let nodes = walk(tree); let before = []; for (node of nodes) { if (node == cut) break; before.push(node); } let after = []; for (node of nodes) after.push(node); return [before, after]; } - bob
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

