Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 22, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
Attachments:
| Philippe, Thank you very much for your response. It looks similar to what I've done in javascript by wrapping all function arguments into a single object literal but the D alternative you propose is a little to convoluted for a beginner like me. Perhaps I'll understand it better after I'm done reading the D book. To bad D doesn't support passing arguments by name. It makes code so much easier to read, especially in large projects. Even Fortran allows it. -clk (Christian Keppenne) > On 20/12/2011 14:18, clk wrote: 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 > > End of Digitalmars-d-learn Digest, Vol 71, Issue 34 *************************************************** > |
December 22, 2011 Re: newbie question: can D do this? | ||||
---|---|---|---|---|
| ||||
Attachments:
| Philippe,
I don't understand the example below to simulate the list comprehension
syntax. Are input1, input2 and input3 ranges? Where is the comp
function defined?
Thank you,
-clk
(Christian Keppenne)
auto lc = comp!("tuple(a,b,c)", "a*a+b*b == c*c&& a<b")(input1,input2, input3);
> ------------------------------
>
> Message: 2
> Date: Tue, 20 Dec 2011 21:45:26 +0100
> From: Philippe Sigaud<philippe.sigaud@gmail.com>
> To: "digitalmars.D.learn"<digitalmars-d-learn@puremagic.com>
> Subject: Re: newbie question: Can D do this?
> Message-ID:
> <CAOA6Bi6BaYm1T3PhvWY9mBRPsedK_dDjWYr-7yDO5cOV2ddiTw@mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Mon, Dec 19, 2011 at 17:17, clk<clk@clksoft.com> wrote:
>
> Correct. As other have said, it's doable by combining std functions. As fas as I know, we do not have a cartesian product range, to iterate on all combinations of two or more ranges.
>
> [f(x,y) for x in list1 for y in list2 if condition]
>
> I gave it a try a few years ago and could get something like this:
>
> auto lc = comp!("tuple(a,b,c)", "a*a+b*b == c*c&& a<b")(input1,
> input2, input3);
>
> -> mapper, condition,
> input ranges, as many as you wish
>
> But at the time I couldn't find a way to do bindings, that is:
>
> [f(x,y) for x in [0..10] for y in [0..x]]
> -> the range iterated by y depends on x.
>
> If anyone has an idea, I'm game.
>
> Philippe
|
December 22, 2011 Re: newbie question: Can D do this? | ||||
---|---|---|---|---|
| ||||
On Thu, Dec 22, 2011 at 16:37, clk <clk@clksoft.com> wrote:
> Philippe,
> Thank you very much for your response. It looks similar to what I've done
> in javascript by wrapping all function arguments into a single object
> literal but the D alternative you propose is a little to convoluted for a
> beginner like me. Perhaps I'll understand it better after I'm done reading
> the D book.
Oh yes, it *is* convoluted :)
What I was proposing is to try and code it myself, if others find the
resulting syntax acceptable. I wouldn't know, since I do not use named
arguments myself.
|
December 22, 2011 Re: newbie question: can D do this? | ||||
---|---|---|---|---|
| ||||
On Thu, Dec 22, 2011 at 16:45, clk <clk@clksoft.com> wrote: > Philippe, > I don't understand the example below to simulate the list comprehension > syntax. Are input1, input2 and input3 ranges? Yes, inputs are ranges. Sorry, I was perhaps a bit hasty in answering. The code is on github: https://github.com/PhilippeSigaud/dranges comp() is defined into the algorithm.d module. Github does not host the docs, but I generated them on dsource a long time ago: http://svn.dsource.org/projects/dranges/trunk/dranges/docs/algorithm.html (search for comp, there is no anchor. I think there wasn't a way to make them easily on DDoc a year ago) |
Copyright © 1999-2021 by the D Language Foundation