Impressed

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Jul 26 20:24:08 PDT 2012


On Thu, 26 Jul 2012 23:21:54 -0400
Nick Sabalausky <SeeWebsiteToContactMe at semitwist.com> wrote:

> On Thu, 26 Jul 2012 21:00:12 -0600
> Brad Anderson <eco at gnuk.net> wrote:
> 
> > On Thu, Jul 26, 2012 at 7:56 PM, Stuart <stugol at 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<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<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 equivalent: iota(0, int.max, 2).map!(a => /* do something with
> > even numbers */)();
> > 
> 
> Or:
> 
> foreach(i; iota(0, int.max, 2))
> {
> 	// stuff
> }
> 
> Or (not as fast, but more flexible):
> 
> foreach(i; iota(0, int.max).filter!(a => a%2==0)())
> {
> 	// stuff
> }
> 
> Or:
> 
> foreach(i; recurrence!(a => a[n-1] + 2)(0))

Erm, should be:
foreach(i; recurrence!"a[n-1] + 2"(0))

> {
> 	// stuff
> }
> 
> Etc...
> 
> 




More information about the Digitalmars-d mailing list