Impressed

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jul 28 01:53:27 PDT 2012


On 28-Jul-12 11:49, Stuart wrote:
> On Friday, 27 July 2012 at 22:38:46 UTC, Dmitry Olshansky wrote:
>> On 27-Jul-12 22:58, Stuart wrote:
>>>
>>> Well, off the top of my head, you could use something like:
>>>
>>>    Public Iterator Function AllFiles(RootDirectory As String) As
>>> IEnumerable(Of String)
>>>       Dim Roots As New Queue(Of String) From {RootDirectory}
>>>       While Roots.Any
>>>          Dim Root = Roots.Pop
>>>          Roots.AddRange(IO.Directory.GetDirectories(Root))
>>>          For Each Filename in IO.Directory.GetFiles(Root)
>>>             Yield Filename
>>>          Next
>>>       End While
>>>    End Function
>>>
>>
>> Then it's not in anyway better then ranges.
>
> I assume you mean "any way" - "anyway" has a different meaning. And I
> don't know much about ranges, because there's very little documentation.
> But as I understand it, for ranges I'd need to write a whole new class.

struct. Yeah, you need it.

> Here, I'm writing a SINGLE FUNCTION in standard imperative style.

That implicitly constructs Iterator.
It's just a sugar. In fact you can do template mixin to generate most of 
range boilerplate.

>> You again maintain stack (or queue, whatever).
>
> There is a difference between recursion (stack) and state variables. To
> claim that my function is recursive is sheer nonsense.

I haven't claimed that in this post. Like I said, you indeed create a 
state automation. That is a function + state struct (bunch of state 
vars). It's not a SINGLE FUNCTION in the end. It's a sugar for special 
object with member function  "next" or whatever.
Also this object should implement all members of Iterator, meaning it's 
polymorphic and uses virtual calls.

What I tried to point out is that the end result is the same, with yield 
you just generate state object automatically (and wrap it in polymorphic 
Iterator?).

 > Recursion is
 > costlier than a local queue.

Not necessarily.  Again, hardware stack is faster, but saves more stuff 
(unlike your queue that saves only filenames).
So recursion is not extremely costly on its own, I'd say even faster. In 
fact, incredibly popular PCRE uses recursion to process regular 
expressions. At least there it was faster then explicit stack, the only 
problematic thing is requirement to have big thread stack.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list