To begin in D coming from Python

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Jul 21 09:32:37 PDT 2008


"Jason House" <jason.james.house at gmail.com> wrote in message 
news:g62d9m$crt$1 at digitalmars.com...
> 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;

foreach(ref elem; li)
    elem *= 2; 





More information about the Digitalmars-d mailing list