On Fri, 27 Jul 2012 03:56:32 +0200 "Stuart" <stu...@gmx.com> wrote:
> On Friday, 27 July 2012 at 00:10:31 UTC, Brad Anderson wrote: > > D uses ranges instead of iterators. You can read more about > > them here: http://ddili.org/ders/d.en/ranges.html > > > > I find ranges to be a vast improvement over iterators > > personally (I use iterators extensively in C++ for my job and > > lament not having ranges regularly). > > > > > On Friday, 27 July 2012 at 00:17:21 UTC, H. S. Teoh wrote: > > D has something far superior: ranges. > > > > http://www.informit.com/articles/printerfriendly.aspx?p=1407357&rll=1 > > > > Even better, they are completely implemented in the library. No > > unnecessary language bloat just to support them. > > I'm not very well up on ranges. I understand the general [1 ... > 6] type of ranges, but I really don't see how custom range > functions could be as useful as the Yield support in VB.NET. I > mean, here's an example of an iterator in VB.NET: > > Public Function InfiniteSequence(StartValue As Int32, Step As > Int32) As IEnumerable(Of Int32) > Do > Yield StartValue > StartValue += Step > Loop > End Function > > Usage: > > For Each N in InfiniteSequence(2, 2) > ... do something with this sequence of even numbers ... > Next > > Notice how this function is written like a synchronous loop, yet > yields a lazy-initialised infinite sequence of numbers. Granted, > it's not a particularly useful example, but trust me: Iterators > and Yield in .NET is *really* damn useful. I would go so far as > to say it was one of the language's best features. > > I may be wrong, but it looks like I'd have to code a new class - > not to mention several specific functions and some kind of state > variable - just to simulate this functionality in D. Can anyone > clarify this for me? D can do that with either fibers or opApply. I've explored all the different approaches in this admittedly not-so-well-written article: https://semitwist.com/articles/article/view/combine-coroutines-and-input-ranges-for-dead-simple-d-iteration Note, however, that fibers (while far, *far* faster than threads) are still too heavyweight to be recommended for most generator functions (as I learned after writing that article). So if you want a generator, you should use opApply instead of fibers or my "InputVisitor" trick. I'd recommend using Adam's trick mentioned in the second "Update" at the bottom of the article.