To begin in D coming from Python

Jesse Phillips jessekphillips at gmail.com
Wed Jul 23 21:09:58 PDT 2008


On Mon, 21 Jul 2008 12:25:26 -0400, Jason House wrote:

> 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...

You are correct that index occurs first.



More information about the Digitalmars-d mailing list