Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 19, 2011 newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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]; 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] 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. I find the 3 features above extremely convenient in every day coding. Thanks, -clk |
December 19, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | 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. |
December 19, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | 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 |
December 19, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | 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 > |
December 19, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | On Mon, 19 Dec 2011 17:17:43 +0100, clk <clk@clksoft.com> 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];
This, or something quite like it, was covered on Saturday in the thread
"Alias/Ref Tuples ?". This works (but is hardly as elegant as Python's
syntax:
import std.typetuple : TypeTuple;
import std.typecons : tuple;
TypeTuple!(a, b) = tuple(b,a);
|
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On Monday, 19 December 2011 at 19:01:10 UTC, Simen Kjærås wrote: > import std.typetuple : TypeTuple; > import std.typecons : tuple; > > TypeTuple!(a, b) = tuple(b,a); There is a pull request implementing multiple variable declarations: https://github.com/D-Programming-Language/dmd/pull/341 However, the right hand side must still be a tuple. |
Copyright © 1999-2021 by the D Language Foundation