newbie question: Can D do this?

2011-12-19 Thread clk

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



Re: newbie question: Can D do this?

2011-12-19 Thread simendsjo

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.


Re: newbie question: Can D do this?

2011-12-19 Thread Jonathan M Davis
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


Re: newbie question: Can D do this?

2011-12-19 Thread Ali Çehreli

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



Re: newbie question: Can D do this?

2011-12-19 Thread Kai Meyer

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





Re: newbie question: Can D do this?

2011-12-19 Thread Simen Kjærås

On Mon, 19 Dec 2011 17:17:43 +0100, 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];



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);


Re: newbie question: Can D do this?

2011-12-19 Thread Ali Çehreli

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



Re: newbie question: Can D do this?

2011-12-19 Thread Jonathan M Davis
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


Re: newbie question: Can D do this?

2011-12-19 Thread Jakob Ovrum

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.


Re: newbie question: Can D do this?

2011-12-20 Thread clk
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-requ...@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-requ...@puremagic.com

You can reach the person managing the list at
digitalmars-d-learn-ow...@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
To: digitalmars-d-learn@puremagic.com
Subject: Re: newbie question: Can D do this?
Message-ID:
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
To: digitalmars-d-learn@puremagic.com
Subject: Re: newbie question: Can D do this?
Message-ID:
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

Re: newbie question: Can D do this?

2011-12-20 Thread Timon Gehr

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-requ...@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-requ...@puremagic.com

You can reach the person managing the list at
digitalmars-d-learn-ow...@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
To:digitalmars-d-learn@puremagic.com
Subject: Re: newbie question: Can D do this?
Message-ID:
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
To:digitalmars-d-learn@puremagic.com
Subject: Re: newbie question: Can D do this?
Message-ID:
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.algorit

Re: newbie question: Can D do this?

2011-12-20 Thread Steven Schveighoffer

On Tue, 20 Dec 2011 09:18:16 -0500, 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 workarounds, but they are quite ugly.

-Steve


Re: newbie question: Can D do this?

2011-12-20 Thread Philippe Sigaud
> 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


Re: newbie question: Can D do this?

2011-12-20 Thread Philippe Sigaud
On Mon, Dec 19, 2011 at 17:17, clk  wrote:

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

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


Re: newbie question: Can D do this?

2011-12-20 Thread Stewart Gordon

On 20/12/2011 20:36, Philippe Sigaud wrote:


That is, it expects some values, then string/values couples as
associative arrays.



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.


Re: newbie question: Can D do this?

2011-12-21 Thread mta`chrono
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.


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);
}

}

?>


Re: newbie question: Can D do this?

2011-12-21 Thread Christophe
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.



Re: newbie question: Can D do this?

2011-12-22 Thread clk

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





Re: newbie question: can D do this?

2011-12-22 Thread clk

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

Message: 2
Date: Tue, 20 Dec 2011 21:45:26 +0100
From: Philippe Sigaud
To: "digitalmars.D.learn"
Subject: Re: newbie question: Can D do this?
Message-ID:

Content-Type: text/plain; charset=UTF-8

On Mon, Dec 19, 2011 at 17:17, clk  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&&  amapper, 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




Re: newbie question: Can D do this?

2011-12-22 Thread Philippe Sigaud
On Thu, Dec 22, 2011 at 16:37, clk  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.


Re: newbie question: can D do this?

2011-12-22 Thread Philippe Sigaud
On Thu, Dec 22, 2011 at 16:45, clk  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)