5 ms·
> it's hard to write code that traverses a data structure in configurable way and does some hardcoded thing on every element Wouldn’t this just be a function t
by idle_zealot 5y ago
> it's hard to write code that traverses a data structure in configurable way and does some hardcoded thing on every element
Wouldn’t this just be a function that operates on an iterator? I suppose generators make the creation of lazy iterators easier, but generally the solution for languages without generators is to have your traversal build a list. So you lose the laziness but the code remains simple. Then map your per-node processing to the list.
- ajuc 5y ago> generally the solution for languages without generators is to have your traversal build a list Yes, but then you move from O(1) to O(n) in memory usage. And if you want to optimize it you have to restructure your code, when in python lists and generators are drop-in replacements.
- justinpombrio 5y agoAlso, producing a list doesn't work very well as a substitute for infinite generators.
- greggyb 5y agoUntil you want to walk the generator twice -- stateful generators are a design flaw, in my opinion.