newbie question: Can D do this?

Kai Meyer kai at unixlords.com
Mon Dec 19 10:50:33 PST 2011


On 12/19/2011 09:17 AM, clk wrote:
> Hello,
> I'm new to this mailing list. I'm trying to learn D to eventually use it
> in production code.
> I'm a little bit intimidated by the fact that the topics in the d-learn
> list look rather advanced to a newbie like me.
> I have 3 fairly simple questions:
>
> 1) Does D support something like the javascript 1.8 destructuring
> assigment (multiple assigment in python):
>
> [a, b] = [b, a];

I would love multiple assignment like this, but it's tricky. But your 
usage isn't really multiple assignment as much as it is a swap. What I'd 
love is something like this:

[a, b, c] = [get_a(), get_b(), get_c()];

Or

[a, b, c] = [to!(int)(argv[1]), some_other_value, argv[4]);



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

No, D's syntax is very C-ish. I don't expect syntax like this to ever 
show up (though what you are doing is possible with things like 
std.algorithm)

>
> 3) D's slice operator apparently doesn't allow the use of a stride other
> than unity as is allowed with fortran and matlab. Is there a way to
> implement this feature so that
>
> [1, 2, 3, 4, 5][0..$:2] would refer to [1, 3, 5], etc..., where 2 is the
> non unit stride. Or is the find function from std.algorithm the only
> option to achieve the same behavior.

Ya, std.range, like Ali said.

>
> I find the 3 features above extremely convenient in every day coding.
> Thanks,
> -clk
>



More information about the Digitalmars-d-learn mailing list