newbie question: Can D do this?

simendsjo simendsjo at gmail.com
Mon Dec 19 10:17:58 PST 2011


On 19.12.2011 17:17, clk wrote:
> 1) Does D support something like the javascript 1.8 destructuring
> assigment (multiple assigment in python):
>
> [a, b] = [b, a];

I don't think so, but you can do something like this with templates:
void swap(alias a, alias b)() {
     auto t = a;
     a = b;
     b = t;
}

     int a = 1, b = 2;
     swap!(a, b);
     assert(a == 2);
     assert(b == 1);


> 2) D doesn't seem to support the list comprehension syntax available in
> python and javascript. Is this correct?
>
> [f(x) for x in list if condition]

Don't think so. You can use std.algorithm, but it's a bit harder to read:
     auto arr = [1,2,3,4,5,6];
     auto res = array(pipe!(filter!"a>3", map!"a*2")(arr));
     assert(res == [8,10,12]);
     // or
     auto res2 = array(map!"a*2"(filter!"a>3"(arr)));
     assert(res2 == [8,10,12]);


But I'm a newbie myself.


More information about the Digitalmars-d-learn mailing list