Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Attachments:
| Thank you for your quick replies. I'm impressed by the helpfulness and dedication of the D community! Here's another one. Is there a way to pass arguments to functions by keyword as in the calls to f and g below? void f(int a = 0, int b = 1) {} void g(int a) {} void main() { f(b = 1, a = 0); // compile error g(a = 0); // also compile error } On 12/19/2011 03:00 PM, digitalmars-d-learn-request@puremagic.com wrote: > Send Digitalmars-d-learn mailing list submissions to > digitalmars-d-learn@puremagic.com > > To subscribe or unsubscribe via the World Wide Web, visit > http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn > > or, via email, send a message with subject or body 'help' to > digitalmars-d-learn-request@puremagic.com > > You can reach the person managing the list at > digitalmars-d-learn-owner@puremagic.com > > When replying, please edit your Subject line so it is more specific than "Re: Contents of Digitalmars-d-learn digest..." > > > Today's Topics: > > 1. Re: newbie question: Can D do this? (Ali ?ehreli) > 2. Re: newbie question: Can D do this? (Kai Meyer) > 3. Re: newbie question: Can D do this? (Simen Kj?r?s) > 4. Re: newbie question: Can D do this? (Ali ?ehreli) > 5. Re: newbie question: Can D do this? (Jonathan M Davis) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 19 Dec 2011 10:41:29 -0800 > From: Ali ?ehreli<acehreli@yahoo.com> > To: digitalmars-d-learn@puremagic.com > Subject: Re: newbie question: Can D do this? > Message-ID:<jco0gq$1ilt$1@digitalmars.com> > Content-Type: text/plain; charset=UTF-8; format=flowed > > 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 > > > > ------------------------------ > > Message: 2 > Date: Mon, 19 Dec 2011 11:50:33 -0700 > From: Kai Meyer<kai@unixlords.com> > To: digitalmars-d-learn@puremagic.com > Subject: Re: newbie question: Can D do this? > Message-ID:<jco11q$1lle$1@digitalmars.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > 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 >> > > > ------------------------------ > > Message: 3 > Date: Mon, 19 Dec 2011 20:01:06 +0100 > From: Simen Kj?r?s<simen.kjaras@gmail.com> > To: digitalmars-d-learn@puremagic.com > Subject: Re: newbie question: Can D do this? > Message-ID:<op.v6q234mt0gpyof@biotronic.lan> > Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes > > 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); > > > ------------------------------ > > Message: 4 > Date: Mon, 19 Dec 2011 11:07:49 -0800 > From: Ali ?ehreli<acehreli@yahoo.com> > To: digitalmars-d-learn@puremagic.com > Subject: Re: newbie question: Can D do this? > Message-ID:<jco225$1r5m$1@digitalmars.com> > Content-Type: text/plain; charset=UTF-8; format=flowed > > On 12/19/2011 10:39 AM, Jonathan M Davis wrote: > > it's a range (see > > http://www.informit.com/articles/printerfriendly.aspx?p=1407357 for a > general > > explanation of the concept of ranges) > > That's a great article.[1] I hope that this chapter is more beginner-friendly: > > http://ddili.org/ders/d.en/ranges.html > > Ali > > [1] Andrei's article has a Turkish translation as well: > > http://ddili.org/makale/eleman_erisimi_uzerine.html > > > > ------------------------------ > > Message: 5 > Date: Mon, 19 Dec 2011 14:30:39 -0500 > From: "Jonathan M Davis"<jmdavisProg@gmx.com> > To: "digitalmars.D.learn"<digitalmars-d-learn@puremagic.com> > Subject: Re: newbie question: Can D do this? > Message-ID:<20111219193039.5420@gmx.com> > Content-Type: text/plain; charset="utf-8" > > On Monday, December 19, 2011 11:07:49 Ali ?ehreli wrote: >> That's a great article.[1] I hope that this chapter is more beginner-friendly: >> >> http://ddili.org/ders/d.en/ranges.html > Cool. One of the things that we're missing on the website is a solid article on ranges (I started such an article a while back and really should go back and finish it), but having something like this to link to should be quite useful. I'll have to give it a read. Thanks! > > - Jonathan M Davis > > > ------------------------------ > > _______________________________________________ > Digitalmars-d-learn mailing list > Digitalmars-d-learn@puremagic.com > http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn > > > End of Digitalmars-d-learn Digest, Vol 71, Issue 29 *************************************************** > |
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | On 12/20/2011 03:18 PM, clk wrote: > Thank you for your quick replies. I'm impressed by the helpfulness and > dedication of the D community! > Here's another one. Is there a way to pass arguments to functions by > keyword as in the calls to f and g below? > > void f(int a = 0, int b = 1) {} > void g(int a) {} > > void main() { > f(b = 1, a = 0); // compile error > g(a = 0); // also compile error > } > > No, there are no named arguments in D. Having them would sometimes be useful, but the drawback is that the parameter names become part of the public interface. > > > > On 12/19/2011 03:00 PM, digitalmars-d-learn-request@puremagic.com wrote: >> Send Digitalmars-d-learn mailing list submissions to >> digitalmars-d-learn@puremagic.com >> >> To subscribe or unsubscribe via the World Wide Web, visit >> http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn >> >> or, via email, send a message with subject or body 'help' to >> digitalmars-d-learn-request@puremagic.com >> >> You can reach the person managing the list at >> digitalmars-d-learn-owner@puremagic.com >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Digitalmars-d-learn digest..." >> >> >> Today's Topics: >> >> 1. Re: newbie question: Can D do this? (Ali ?ehreli) >> 2. Re: newbie question: Can D do this? (Kai Meyer) >> 3. Re: newbie question: Can D do this? (Simen Kj?r?s) >> 4. Re: newbie question: Can D do this? (Ali ?ehreli) >> 5. Re: newbie question: Can D do this? (Jonathan M Davis) >> >> >> ---------------------------------------------------------------------- >> >> Message: 1 >> Date: Mon, 19 Dec 2011 10:41:29 -0800 >> From: Ali ?ehreli<acehreli@yahoo.com> >> To:digitalmars-d-learn@puremagic.com >> Subject: Re: newbie question: Can D do this? >> Message-ID:<jco0gq$1ilt$1@digitalmars.com> >> Content-Type: text/plain; charset=UTF-8; format=flowed >> >> 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 >> >> >> >> ------------------------------ >> >> Message: 2 >> Date: Mon, 19 Dec 2011 11:50:33 -0700 >> From: Kai Meyer<kai@unixlords.com> >> To:digitalmars-d-learn@puremagic.com >> Subject: Re: newbie question: Can D do this? >> Message-ID:<jco11q$1lle$1@digitalmars.com> >> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >> 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 >>> >> >> >> ------------------------------ >> >> Message: 3 >> Date: Mon, 19 Dec 2011 20:01:06 +0100 >> From: Simen Kj?r?s<simen.kjaras@gmail.com> >> To:digitalmars-d-learn@puremagic.com >> Subject: Re: newbie question: Can D do this? >> Message-ID:<op.v6q234mt0gpyof@biotronic.lan> >> Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes >> >> 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); >> >> >> ------------------------------ >> >> Message: 4 >> Date: Mon, 19 Dec 2011 11:07:49 -0800 >> From: Ali ?ehreli<acehreli@yahoo.com> >> To:digitalmars-d-learn@puremagic.com >> Subject: Re: newbie question: Can D do this? >> Message-ID:<jco225$1r5m$1@digitalmars.com> >> Content-Type: text/plain; charset=UTF-8; format=flowed >> >> On 12/19/2011 10:39 AM, Jonathan M Davis wrote: >> > it's a range (see >> > http://www.informit.com/articles/printerfriendly.aspx?p=1407357 for a >> general >> > explanation of the concept of ranges) >> >> That's a great article.[1] I hope that this chapter is more >> beginner-friendly: >> >> http://ddili.org/ders/d.en/ranges.html >> >> Ali >> >> [1] Andrei's article has a Turkish translation as well: >> >> http://ddili.org/makale/eleman_erisimi_uzerine.html >> >> >> >> ------------------------------ >> >> Message: 5 >> Date: Mon, 19 Dec 2011 14:30:39 -0500 >> From: "Jonathan M Davis"<jmdavisProg@gmx.com> >> To: "digitalmars.D.learn"<digitalmars-d-learn@puremagic.com> >> Subject: Re: newbie question: Can D do this? >> Message-ID:<20111219193039.5420@gmx.com> >> Content-Type: text/plain; charset="utf-8" >> >> On Monday, December 19, 2011 11:07:49 Ali ?ehreli wrote: >>> That's a great article.[1] I hope that this chapter is more >>> beginner-friendly: >>> >>> http://ddili.org/ders/d.en/ranges.html >> Cool. One of the things that we're missing on the website is a solid article >> on ranges (I started such an article a while back and really should go back >> and finish it), but having something like this to link to should be quite >> useful. I'll have to give it a read. Thanks! >> >> - Jonathan M Davis >> >> >> ------------------------------ >> >> _______________________________________________ >> Digitalmars-d-learn mailing list >> Digitalmars-d-learn@puremagic.com >> http://lists.puremagic.com/cgi-bin/mailman/listinfo/digitalmars-d-learn >> >> >> End of Digitalmars-d-learn Digest, Vol 71, Issue 29 >> *************************************************** >> > |
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | On Tue, 20 Dec 2011 09:18:16 -0500, clk <clk@clksoft.com> wrote:
> Thank you for your quick replies. I'm impressed by the helpfulness and
> dedication of the D community!
> Here's another one. Is there a way to pass arguments to functions by
> keyword as in the calls to f and g below?
>
> void f(int a = 0, int b = 1) {}
> void g(int a) {}
>
> void main() {
> f(b = 1, a = 0); // compile error
> g(a = 0); // also compile error
> }
No. There are workarounds, but they are quite ugly.
-Steve
|
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clk | On 20/12/2011 14:18, clk wrote: > Thank you for your quick replies. I'm impressed by the helpfulness and dedication of the D > community! > Here's another one. Is there a way to pass arguments to functions by keyword as in the > calls to f and g below? No. Named arguments have been proposed on at least one occasion: http://tinyurl.com/cvy99td <snip> > void main() { > f(b = 1, a = 0); // compile error > g(a = 0); // also compile error > } <snip> Moreover, that notation's no good, since the arguments are already assignment expressions. If the feature is one day implemented it would likely use colons, in line with array/struct intialisers. Stewart. |
December 20, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | > On 20/12/2011 14:18, clk wrote:
>> Here's another one. Is there a way to pass arguments to functions by
>> keyword as in the
>> calls to f and g below?
I remember a discussion about year ago or so.
It seems doable to have some kind of function transformer (adaptor?) for this.
from:
int foo(int a = 0, int b = 1, double c = 0.0, bool d = false) { return 1;}
alias namedParams!foo nfoo; // transform it into a called-by-name function.
nfoo(["d": true]); // a = 0, b = 1, c = 0.0, d = true
nfoo(["d" : true], ["b" : 100]); // a=0, b=100, c=0.0, d=true
nfoo(1, 2, ["d" : true]); // a=1, b=2, c=0.0, d=true
That is, it expects some values, then string/values couples as associative arrays.
Would that be palatable? Because I think it's doable.
To obtain the arguments names:
int foo(int a, int b, double c = 0.0, bool d = true) { return 1;}
template Name(alias foo) if (isCallable!foo)
{
enum string Name = S!(foo.stringof);
}
template S(string s) // this template is just a trick because
foo.stringof directly displeases DMD
{
enum string S = s;
}
writeln(Name!foo); // "int(int a, int b, double c = 0, bool d = true)"
So this gives us:
- the arguments names
- which ones have default values
- what is that default value
The difficulty here is correctly parsing the ( ,,,) part, without
getting desoriented by argument types that themselves use (,), like
templated types.
I think that would make for an small & interesting community challenge.
Philippe
|
December 21, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On 20/12/2011 20:36, Philippe Sigaud wrote:
<snip>
> That is, it expects some values, then string/values couples as
> associative arrays.
<snip>
I've a recollection of seeing something like this in the PHP library, but I forget where. I believe it's used in some functions that have a lot of options to set, such that you'll typically just want to set a few of them in a given call.
Stewart.
|
December 21, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | In PHP frameworks often use $option arrays which are some kind of key value pairs. they are merged with the default valuzes inside the function. pro: - you can pass an argument by name - you only need to pass those who needed. cons: - hash arrays it should be possible to create a similar method in d. but I really don't like this solution in d. it feels bad and looks ugly. <?php $model->query(array( 'firstname' => 'John', 'country' => 'France', 'order' => 'asc' )); class Model { function query($options = array()) { // merge with defaults $options = array_merge(array( 'deep' => true, 'order' => 'desc', 'type' => 'sql', 'backend' => 'mysql' ... ), $options); } } ?> |
December 21, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Timon Gehr , dans le message (digitalmars.D.learn:31142), a écrit : > On 12/20/2011 03:18 PM, clk wrote: >> Thank you for your quick replies. I'm impressed by the helpfulness and >> dedication of the D community! >> Here's another one. Is there a way to pass arguments to functions by >> keyword as in the calls to f and g below? >> >> void f(int a = 0, int b = 1) {} >> void g(int a) {} >> >> void main() { >> f(b = 1, a = 0); // compile error >> g(a = 0); // also compile error >> } >> >> > > No, there are no named arguments in D. Having them would sometimes be useful, > but the drawback is that the parameter names become part of the public interface. Well, that's precisely the point. And it is a drawback if parameters are systematically names, but not if it is triggered only on demand. Example : void foo(int a, int b:, int c:); void main() { foo(1, 2, 3); foo(1, c: 3, b: 2; foo(a: 1, b: 2, c: 3); // error : a is not a named parameter. } In the example, ":" is used to make a named parameter to recall the use when you call the function. |
Copyright © 1999-2021 by the D Language Foundation