newbie question: Can D do this?

Jonathan M Davis jmdavisProg at gmx.com
Mon Dec 19 10:39:58 PST 2011


On Monday, December 19, 2011 11:17:43 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];

No. You'd have to use std.algorithm.swap.

> 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, but you can do similar stuff with std.algorithm.

auto transformed = map!func(filter!cond(list));

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

Use std.range.stride. If you want a new array from it, then use 
std.array.array on the result. Otherwise, it's a range (see 
http://www.informit.com/articles/printerfriendly.aspx?p=1407357 for a general 
explanation of the concept of ranges) but not an array. e.g.

auto arr = array(stride([1, 2, 3, 4, 5], 2));

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list