To begin in D coming from Python

Jason House jason.james.house at gmail.com
Mon Jul 21 09:25:26 PDT 2008


Luis P. Mendes Wrote:
> >> And regarding list comprehensions like li = [elem*2 for elem in li]? 
> >> is there something close in D?
> > 
> > Depends on how you define close. There is foreach and with some template
> > trickery you can get similar syntax:
> > 
> > // Please note that this works only if you define the template "each"
> > yourself!
> > [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline;
> > });
> > 
> > But I would recommend you concentrate on the basic stuff first.
> By something close I meant some kind of (high-level) construct that D 
> would have to offer.  As in this example, as in other I wrote I didn't 
> want to mimic Python. 

I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like:

foreach(index, elem; li)
  li[index] = elem*2;

Note that I may have reversed the order of index and elem.  I never remember which one should come first...



More information about the Digitalmars-d mailing list