# newbie question: Can D do this?

Ali Çehreli acehreli at yahoo.com
Mon Dec 19 10:41:29 PST 2011

```On 12/19/2011 08:17 AM, clk wrote:

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

We need more newbie topics here! :)

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

No multiple assignment like that. But useful approarches exist for most
needs, like the swap that simendsjo has shown.

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

List comprehension is not part of the language.

import std.algorithm;

void f(int x)
{}

bool condition(int x)
{
return true;
}

void main()
{
auto list = [ 0, 1, 2 ];
map!f(filter!condition(list));
}

You can define f and condition within the body of main().

It is possible to use function literals as well:

import std.algorithm;

void main()
{
auto list = [ 0, 1, 2 ];
map!((x){
/* ... this is f(x) ...*/
})(filter!((x) {
return true; /* ... condition ... */
})(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.

std.range.stride does that:

import std.range;
// ...
stride([1, 2, 3, 4, 5], 2)

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

Ali

```