Re: How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Jacob Carlborg

On 2011-08-24 19:40, Andrej Mitrovic wrote:

Here's what I can do with a variadic function:

void main()
{
 int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

 process(a[a.countUntil(7) .. $]);
 process(1);
}

void process(int[] vals...)
{
 foreach (val; vals)
 {
 }
}

Very simple, pass one or multiple arguments. But then I thought about
using the `until` template instead of countUntil. However `until`
returns a range. So my next guess was to write:

void main()
{
 int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

 process(a.until(7));  // ok
 process(4);  // error since 4 is not a range
}

void process(Range)(Range vals) if (isInputRange!Range&&
is(ElementType!Range == int))
{
 foreach (val; vals)
 {
 }
}

Is it somehow possible to automatically convert a literal to a range?
I really miss the convenience of variadic functions. I thought about
making an overload that only takes an int and constructing a simple
input range around it so it can be passed to process(), e.g.:

void process(Range)(Range vals) if (isInputRange!Range&&
is(ElementType!Range == int))
{
 foreach (val; vals)
 {
 }
}

void process(int arg)
{
 process(makeInputRange(arg));  // make an input range, pass to
above process()
}

But I can't overload templated and non-templated functions, I think
this is one of those old-standing bugs.


Use a variadic template function and check if the first argument is a 
range or not.


--
/Jacob Carlborg


Re: Default libraries by dmd

2011-08-24 Thread Jesse Phillips
On Thu, 25 Aug 2011 04:46:11 +, Cleem wrote:

> What libraries are linked by command dmd as linker? I want to use g++ as
> linker and link phobos2 and druntime libraries. But there are some
> undefined references, for example:
> _D3std4conv13ConvException7__ClassZ
> _D3std4conv16__T5parseTlTAyaZ5parseFKAyaZl
> _D3std5ascii7isDigitFNaNbNfwZb
> _d_arraysetcapacity
> and many others.
> However when I use dmd for linking, I get success. Linux. DMD v2.054.

dmd -v main.d
...
gcc main.o -o main -m64 -Xlinker --export-dynamic -lrt -Xlinker --no-warn-
search-mismatch -lphobos2 -lpthread -lm 


Re: Default libraries by dmd

2011-08-24 Thread Jonathan M Davis
On Thursday, August 25, 2011 04:46:11 Cleem wrote:
> What libraries are linked by command dmd as linker? I want to use g++
> as linker and link phobos2 and druntime libraries. But there are some
> undefined references, for example:
> _D3std4conv13ConvException7__ClassZ
> _D3std4conv16__T5parseTlTAyaZ5parseFKAyaZl
> _D3std5ascii7isDigitFNaNbNfwZb
> _d_arraysetcapacity
> and many others.
> However when I use dmd for linking, I get success.
> Linux. DMD v2.054.

Looke at dmd.conf. It should be in the same directory as dmd.

- Jonathan M Davis


Re: Split by length?

2011-08-24 Thread Vladimir Panteleev
On Mon, 15 Aug 2011 05:49:59 +0300, Andrej Mitrovic  
 wrote:



Is there something in Phobos with which I could do:

auto arr = [1, 2, 3, 4, 5, 6];

int[][] newarr = arr.splitLength(2);
assert(newarr.length == 3);


Almost but not quite: auto newarr = cast(int[2][])arr;

--
Best regards,
 Vladimirmailto:vladi...@thecybershadow.net


Default libraries by dmd

2011-08-24 Thread Cleem
What libraries are linked by command dmd as linker? I want to use g++
as linker and link phobos2 and druntime libraries. But there are some
undefined references, for example:
_D3std4conv13ConvException7__ClassZ
_D3std4conv16__T5parseTlTAyaZ5parseFKAyaZl
_D3std5ascii7isDigitFNaNbNfwZb
_d_arraysetcapacity
and many others.
However when I use dmd for linking, I get success.
Linux. DMD v2.054.


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Timon Gehr

On 08/25/2011 12:47 AM, Mafi wrote:

Am 24.08.2011 21:04, schrieb Timon Gehr:

On 08/24/2011 08:04 PM, Andrej Mitrovic wrote:

Here's some code that iterates through "parents" of some class object
until it finds an object with no parent (where parent is null):

import std.stdio;

class Foo
{
Foo parent;
int state;

this (int state) { this.state = state; }
}

void main()
{
auto foo = new Foo(0);
foo.parent = new Foo(1);
foo.parent.parent = new Foo(2);

while (true)
{
if (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}
else
{
break;
}
}
}

(syntax-highlighted: http://codepad.org/8yHRmICh)

But I was hoping I could simplify this by doing:

while (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}

However that doesn't work, I get back:
expression expected, not 'auto'

Is there a limitation on why this couldn't work or can this be added
to the language?


Afaics, this could be added just like it could be added for if. In fact
the compiler should be able to simply rewrite it to your first example.
I think it is worth an enhancement request, because there are situations
where this would be useful, and implementation should be trivial, if
somebody has the time. (I also think it adds to the consistency of the
language, but others may disagree.)


(btw, i always use for(;;) instead of while(true), it is usually faster
in debug mode and faster to type :))


I just have to say that it already works for 'if'. It's a great feature
because in the body of the 'if' you know the value is non-null and
outside, where there could be an segfault, the variable is not visible.
I'm not really sure if it's good for 'while'.
I'm unsure because there are two somewhat natural semantics for such a
construct.

//example
//more to the nature of while
while(auto obj = init) { work(obj); }

//1 (your interpretation)
typeof(init) obj;
while(obj = init) { work(obj); }

//2
/*
seems more natural for me because init runs only once (like any other
init and like in 'for' or 'if')
*/


for(;;){int x=init;} // init runs infinitely many times


if(auto a=init){} is really more trying to solve

if(???){auto a=init;} // correct scoping but cannot refer to it in cond

than

{auto a=init; if(a){}} // correct scoping as long as there is no else

That is why there is the language construct. That it has not been 
adopted for while is because it is so much more useful for if, but I 
personally think while could/should get the possibility as well.





auto obj = init;
while(obj) { work(obj); }

This could confuse many people, I think. What do you think?



I think that by the same argument, you could say that the fact that the 
body of a looping statement is executed multiple times could confuse 
many people.


1 is more natural imho. (and 2 would be rather useless)




Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Mafi

Am 24.08.2011 21:04, schrieb Timon Gehr:

On 08/24/2011 08:04 PM, Andrej Mitrovic wrote:

Here's some code that iterates through "parents" of some class object
until it finds an object with no parent (where parent is null):

import std.stdio;

class Foo
{
Foo parent;
int state;

this (int state) { this.state = state; }
}

void main()
{
auto foo = new Foo(0);
foo.parent = new Foo(1);
foo.parent.parent = new Foo(2);

while (true)
{
if (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}
else
{
break;
}
}
}

(syntax-highlighted: http://codepad.org/8yHRmICh)

But I was hoping I could simplify this by doing:

while (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}

However that doesn't work, I get back:
expression expected, not 'auto'

Is there a limitation on why this couldn't work or can this be added
to the language?


Afaics, this could be added just like it could be added for if. In fact
the compiler should be able to simply rewrite it to your first example.
I think it is worth an enhancement request, because there are situations
where this would be useful, and implementation should be trivial, if
somebody has the time. (I also think it adds to the consistency of the
language, but others may disagree.)


(btw, i always use for(;;) instead of while(true), it is usually faster
in debug mode and faster to type :))


I just have to say that it already works for 'if'. It's a great feature 
because in the body of the 'if' you know the value is non-null and 
outside,  where there could be an segfault, the variable is not visible.

I'm not really sure if it's good for 'while'.
I'm unsure because there are two somewhat natural semantics for such a 
construct.


//example
//more to the nature of while
while(auto obj = init) { work(obj); }

//1 (your interpretation)
typeof(init) obj;
while(obj = init) { work(obj); }

//2
/*
seems more natural for me because init runs only once (like any other 
init and like in 'for' or 'if')

*/
auto obj = init;
while(obj) { work(obj); }

This could confuse many people, I think. What do you think?

Mafi



Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Andrej Mitrovic
Req'd: http://d.puremagic.com/issues/show_bug.cgi?id=6550


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Timon Gehr

On 08/24/2011 09:36 PM, Jonathan M Davis wrote:

On Wednesday, August 24, 2011 21:29:23 Timon Gehr wrote:

On 08/24/2011 09:21 PM, Andrej Mitrovic wrote:

On 8/24/11, Timon Gehr   wrote:

it is usually faster
in debug mode


Huh.. How come?


Well, not notably faster, but many compilers will emit something in the
lines of

mov eax, 1
test eax
jnz beginning_of_loop

if no optimizer is run,

whereas most get

for(;;){}

as

jmp beginning_of_loop

even in debug mode.


Optimizations aside, I would always argue that while(true) should be used
rather than for(;;). I find for(;;) to be ugly personally and would argue that
while(true) better captures what you're actually doing. A loop with no
condition at all is horrible IMHO. It seems completely inconsistent to me that
the language would allow you to have a for loop without a condition.

Regardless of that, however, I would fully expect that if there is a difference
in code between the two in debug mode (which there shouldn't be IMHO, but
compilers don't always do what makes the most sense - especially in debug
mode) that the difference would be drowned by the contents of the loop and
wouldn't matter at all. But even if it did, I'd generally argue that coding in
a certain way just because it was slightly faster in debug mode but had zero
impact in release mode is not a good idea unless all of the considerations are
equal. And I'd definitely argue that while(true) is better than for(;;) from an
aesthetic point of view at minimum, so they definitely aren't equal.

In any case, that's my take on it.

- Jonathan M Davis


Well, it is consistent in that you can leave out the other parts in for 
too. (but those are statements, and you can almost everywhere write an 
empty statement, while there exist no empty expressions)


As to while(true) vs for(;;), in my point of view, it is the other way 
round, because how it is compiled in debug mode is really how the 
semantics of the language are: look at the condition, and if it is true, 
loop another time, if it is false, break. If you want an infinite loop, 
why bother having to write a condition to evaluate? Imho for(;;){} 
captures the meaning better than while(true){}. for(;;){} means, loop 
unconditionally (which proves insight in what it is actually doing), and 
while(true){} means 'loop until true is false', which is imo ugly for 
obvious reasons.


That is my way of thinking about aesthetics in programming.



Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Jonathan M Davis
On Wednesday, August 24, 2011 21:29:23 Timon Gehr wrote:
> On 08/24/2011 09:21 PM, Andrej Mitrovic wrote:
> > On 8/24/11, Timon Gehr  wrote:
> >> it is usually faster
> >> in debug mode
> > 
> > Huh.. How come?
> 
> Well, not notably faster, but many compilers will emit something in the
> lines of
> 
> mov eax, 1
> test eax
> jnz beginning_of_loop
> 
> if no optimizer is run,
> 
> whereas most get
> 
> for(;;){}
> 
> as
> 
> jmp beginning_of_loop
> 
> even in debug mode.

Optimizations aside, I would always argue that while(true) should be used 
rather than for(;;). I find for(;;) to be ugly personally and would argue that 
while(true) better captures what you're actually doing. A loop with no 
condition at all is horrible IMHO. It seems completely inconsistent to me that 
the language would allow you to have a for loop without a condition.

Regardless of that, however, I would fully expect that if there is a difference 
in code between the two in debug mode (which there shouldn't be IMHO, but 
compilers don't always do what makes the most sense - especially in debug 
mode) that the difference would be drowned by the contents of the loop and 
wouldn't matter at all. But even if it did, I'd generally argue that coding in 
a certain way just because it was slightly faster in debug mode but had zero 
impact in release mode is not a good idea unless all of the considerations are 
equal. And I'd definitely argue that while(true) is better than for(;;) from an 
aesthetic point of view at minimum, so they definitely aren't equal.

In any case, that's my take on it.

- Jonathan M Davis


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Timon Gehr

On 08/24/2011 09:21 PM, Andrej Mitrovic wrote:

On 8/24/11, Timon Gehr  wrote:

it is usually faster
in debug mode


Huh.. How come?


Well, not notably faster, but many compilers will emit something in the 
lines of


mov eax, 1
test eax
jnz beginning_of_loop

if no optimizer is run,

whereas most get

for(;;){}

as

jmp beginning_of_loop

even in debug mode.


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Andrej Mitrovic
On 8/24/11, Timon Gehr  wrote:
> it is usually faster
> in debug mode

Huh.. How come?


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Timon Gehr

On 08/24/2011 08:04 PM, Andrej Mitrovic wrote:

Here's some code that iterates through "parents" of some class object
until it finds an object with no parent (where parent is null):

import std.stdio;

class Foo
{
 Foo parent;
 int state;

 this (int state) { this.state = state; }
}

void main()
{
 auto foo  = new Foo(0);
 foo.parent= new Foo(1);
 foo.parent.parent = new Foo(2);

 while (true)
 {
 if (auto par = foo.parent)
 {
 writeln(par.state);
 foo = par;
 }
 else
 {
 break;
 }
 }
}

(syntax-highlighted: http://codepad.org/8yHRmICh)

But I was hoping I could simplify this by doing:

 while (auto par = foo.parent)
 {
 writeln(par.state);
 foo = par;
 }

However that doesn't work, I get back:
expression expected, not 'auto'

Is there a limitation on why this couldn't work or can this be added
to the language?


Afaics, this could be added just like it could be added for if. In fact 
the compiler should be able to simply rewrite it to your first example.
I think it is worth an enhancement request, because there are situations 
where this would be useful, and implementation should be trivial, if 
somebody has the time. (I also think it adds to the consistency of the 
language, but others may disagree.)



(btw, i always use for(;;) instead of while(true), it is usually faster 
in debug mode and faster to type :))


Re: How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Jonathan M Davis
On Wednesday, August 24, 2011 11:00 Timon Gehr wrote:
> On 08/24/2011 07:54 PM, Steven Schveighoffer wrote:
> > On Wed, 24 Aug 2011 13:40:38 -0400, Andrej Mitrovic
> > 
> >  wrote:
> >> Here's what I can do with a variadic function:
> >> 
> >> void main()
> >> {
> >> int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];
> >> 
> >> process(a[a.countUntil(7) .. $]);
> >> process(1);
> >> }
> >> 
> >> void process(int[] vals...)
> >> {
> >> foreach (val; vals)
> >> {
> >> }
> >> }
> >> 
> >> Very simple, pass one or multiple arguments. But then I thought about
> >> using the `until` template instead of countUntil. However `until`
> >> returns a range. So my next guess was to write:
> >> 
> >> void main()
> >> {
> >> int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];
> >> 
> >> process(a.until(7)); // ok
> >> process(4); // error since 4 is not a range
> >> }
> >> 
> >> void process(Range)(Range vals) if (isInputRange!Range &&
> >> is(ElementType!Range == int))
> >> {
> >> foreach (val; vals)
> >> {
> >> }
> >> }
> >> 
> >> Is it somehow possible to automatically convert a literal to a range?
> >> I really miss the convenience of variadic functions. I thought about
> >> making an overload that only takes an int and constructing a simple
> >> input range around it so it can be passed to process(), e.g.:
> >> 
> >> void process(Range)(Range vals) if (isInputRange!Range &&
> >> is(ElementType!Range == int))
> >> {
> >> foreach (val; vals)
> >> {
> >> }
> >> }
> >> 
> >> void process(int arg)
> >> {
> >> process(makeInputRange(arg)); // make an input range, pass to
> >> above process()
> >> }
> >> 
> >> But I can't overload templated and non-templated functions, I think
> >> this is one of those old-standing bugs.
> > 
> > maybe:
> > 
> > void process(Range)(Range vals) if (isInputRange!Range &&
> > is(ElementType!Range == int))
> > {
> > ...
> > }
> > 
> > void process(Vals...)(Vals vals) if (allValsElementsAreInt)
> > {
> > ...
> > }
> > 
> > Note that I'm not sure what to put for allValsElementsAreInt...
> > 
> > -Steve
> 
> This should do (although it would probably be even better to have a
> forAll template and a predicate template).
> 
> template allElementsAreInt(T...){
> static if(T.length==0) enum allElementsAreInt=true;
> else enum allElementsAreInt=is(typeof(T[0])==int)
> && allElementsAreInt!(T[1..$]);
> }

std.typetuple has anySatisfy and allSatisfy (though that's a rather bizarre 
place to put them IMHO).

- Jonathan M Davis


Re: links to nested structure

2011-08-24 Thread Adam Ruppe
Yeah, it should work with any level of nesting.

If toPrettyChars() doesn't work out, my parent
plan is actually more like:

auto item = this.parent;
while(item) {
 str ~= "." ~ item.toChars();
 item = this.parent;
}

so it'd go any level needed.


Re: reading in text files

2011-08-24 Thread Jesse Phillips
Brian Brady Wrote:

> but it just hangs there, not doing anything(for a considerable time) so I am
> assuming I am doing something wrong. There isn't any actual mention in the
> book of *how* reading in the text file should be accomplished, so what is the
> best way to do this?

Now that you know how to use the program, here is the answer to your question.

auto content = std.file.readText("filename");

There are other functions depending on use case but this is most common.


Re: Why no (auto foo = bar) in while loops?

2011-08-24 Thread Andrej Mitrovic
P.S. I'm aware I'm loosing the reference to the first foo object but
this is just demonstration code.


Re: links to nested structure

2011-08-24 Thread jdrewsen

Den 23-08-2011 22:38, Adam D. Ruppe skrev:

Jonathan M Davis wrote:

I'm fairly certain that the anchors are generated by ddoc, not std.ddoc


Well, it's a bit of both.

DDOC_PSYMBOL =$(U $0)

Looking at doc.c, DDOC_PSYMBOL is used in the toDocBuffer() methods.
The paramater to it is always this.toChars().

I'm not sure how to do it best. It could probably just do a
parent.toChars() ~ "." ~ toChars() (conceptually) but then it
would output stuff like:

structOuter.Inner

which still isn't ideal.

But, I think this might be ideal:

("$(DDOC_PSYMBOL %s %s)", toChars(), parent.toChars() ~ [...]);


Then, the macro in std.ddoc can be changed to be like this:

DDOC_PSYMBOL =$(U $1)


So you keep the simple info and the more detailed name.


I've gotta look at the dmd source to see what's there for the
parent member, but I'm sure there's something.


Maybe I'll do up a pull request this weekend if nothing else
comes up on my schedule.


Would this also handle double nested classes etc.?

/Jonas


Re: How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Timon Gehr

On 08/24/2011 07:54 PM, Steven Schveighoffer wrote:

On Wed, 24 Aug 2011 13:40:38 -0400, Andrej Mitrovic
 wrote:


Here's what I can do with a variadic function:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a[a.countUntil(7) .. $]);
process(1);
}

void process(int[] vals...)
{
foreach (val; vals)
{
}
}

Very simple, pass one or multiple arguments. But then I thought about
using the `until` template instead of countUntil. However `until`
returns a range. So my next guess was to write:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a.until(7)); // ok
process(4); // error since 4 is not a range
}

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

Is it somehow possible to automatically convert a literal to a range?
I really miss the convenience of variadic functions. I thought about
making an overload that only takes an int and constructing a simple
input range around it so it can be passed to process(), e.g.:

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

void process(int arg)
{
process(makeInputRange(arg)); // make an input range, pass to
above process()
}

But I can't overload templated and non-templated functions, I think
this is one of those old-standing bugs.


maybe:

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
...
}

void process(Vals...)(Vals vals) if (allValsElementsAreInt)
{
...
}

Note that I'm not sure what to put for allValsElementsAreInt...

-Steve


This should do (although it would probably be even better to have a 
forAll template and a predicate template).


template allElementsAreInt(T...){
static if(T.length==0) enum allElementsAreInt=true;
else enum allElementsAreInt=is(typeof(T[0])==int)
  && allElementsAreInt!(T[1..$]);
}




Why no (auto foo = bar) in while loops?

2011-08-24 Thread Andrej Mitrovic
Here's some code that iterates through "parents" of some class object
until it finds an object with no parent (where parent is null):

import std.stdio;

class Foo
{
Foo parent;
int state;

this (int state) { this.state = state; }
}

void main()
{
auto foo  = new Foo(0);
foo.parent= new Foo(1);
foo.parent.parent = new Foo(2);

while (true)
{
if (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}
else
{
break;
}
}
}

(syntax-highlighted: http://codepad.org/8yHRmICh)

But I was hoping I could simplify this by doing:

while (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}

However that doesn't work, I get back:
expression expected, not 'auto'

Is there a limitation on why this couldn't work or can this be added
to the language?


Re: How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Steven Schveighoffer
On Wed, 24 Aug 2011 13:40:38 -0400, Andrej Mitrovic  
 wrote:



Here's what I can do with a variadic function:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a[a.countUntil(7) .. $]);
process(1);
}

void process(int[] vals...)
{
foreach (val; vals)
{
}
}

Very simple, pass one or multiple arguments. But then I thought about
using the `until` template instead of countUntil. However `until`
returns a range. So my next guess was to write:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a.until(7));  // ok
process(4);  // error since 4 is not a range
}

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

Is it somehow possible to automatically convert a literal to a range?
I really miss the convenience of variadic functions. I thought about
making an overload that only takes an int and constructing a simple
input range around it so it can be passed to process(), e.g.:

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

void process(int arg)
{
process(makeInputRange(arg));  // make an input range, pass to
above process()
}

But I can't overload templated and non-templated functions, I think
this is one of those old-standing bugs.


maybe:

void process(Range)(Range vals) if (isInputRange!Range &&  
is(ElementType!Range == int))

{
   ...
}

void process(Vals...)(Vals vals) if (allValsElementsAreInt)
{
   ...
}

Note that I'm not sure what to put for allValsElementsAreInt...

-Steve


Re: How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Timon Gehr

On 08/24/2011 07:40 PM, Andrej Mitrovic wrote:

Here's what I can do with a variadic function:

void main()
{
 int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

 process(a[a.countUntil(7) .. $]);
 process(1);
}

void process(int[] vals...)
{
 foreach (val; vals)
 {
 }
}

Very simple, pass one or multiple arguments. But then I thought about
using the `until` template instead of countUntil. However `until`
returns a range. So my next guess was to write:

void main()
{
 int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

 process(a.until(7));  // ok
 process(4);  // error since 4 is not a range
}

void process(Range)(Range vals) if (isInputRange!Range&&
is(ElementType!Range == int))
{
 foreach (val; vals)
 {
 }
}

Is it somehow possible to automatically convert a literal to a range?
I really miss the convenience of variadic functions. I thought about
making an overload that only takes an int and constructing a simple
input range around it so it can be passed to process(), e.g.:

void process(Range)(Range vals) if (isInputRange!Range&&
is(ElementType!Range == int))
{
 foreach (val; vals)
 {
 }
}

void process(int arg)
{
 process(makeInputRange(arg));  // make an input range, pass to
above process()
}

But I can't overload templated and non-templated functions, I think
this is one of those old-standing bugs.


Workaround: You can make it a templated function with no template 
arguments and wrap a non-templated version, if it is important that the 
implementation is not a template.








How do I simulate variadic parameters for template (range) functions?

2011-08-24 Thread Andrej Mitrovic
Here's what I can do with a variadic function:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a[a.countUntil(7) .. $]);
process(1);
}

void process(int[] vals...)
{
foreach (val; vals)
{
}
}

Very simple, pass one or multiple arguments. But then I thought about
using the `until` template instead of countUntil. However `until`
returns a range. So my next guess was to write:

void main()
{
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5];

process(a.until(7));  // ok
process(4);  // error since 4 is not a range
}

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

Is it somehow possible to automatically convert a literal to a range?
I really miss the convenience of variadic functions. I thought about
making an overload that only takes an int and constructing a simple
input range around it so it can be passed to process(), e.g.:

void process(Range)(Range vals) if (isInputRange!Range &&
is(ElementType!Range == int))
{
foreach (val; vals)
{
}
}

void process(int arg)
{
process(makeInputRange(arg));  // make an input range, pass to
above process()
}

But I can't overload templated and non-templated functions, I think
this is one of those old-standing bugs.


Re: reading in text files

2011-08-24 Thread Cristi Cobzarenco
The program reads the "file" from stdin, so you need to redirect stdin
to the file you want: try "./readingHamlet < hamlet.txt" or "cat
hamlet.txt | ./readingHamlet" without the quotes on a *NIX system.

---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco



On 24 August 2011 17:01, Brian Brady  wrote:
> All
>
> I am working through Andrei Alexandrescus "The D Programming Language" but
> have hit a road block fairly early on.
>
> There is a program in the book which is designed to read through a text file
> and do a simple word count. The program looks like this:
>
> import std.stdio, std.string;
>
> void main()
> {
>  //Compute counts
>  uint[string] freqs;
>  foreach(line; stdin.byLine())
>  {
>    foreach(word; split(strip(line)))
>    {
>      ++freqs[word.idup];
>    }
>  }
>
>  //Prints count
>  foreach(key, value; freqs)
>  {
>    writefln("%6u\t%s", value, key);
>  }
> }
>
> My query is basically how to read the text file in?
>
> currently I am trying to use
> ./readingHamlet cat hamlet.txt
>
> but it just hangs there, not doing anything(for a considerable time) so I am
> assuming I am doing something wrong. There isn't any actual mention in the
> book of *how* reading in the text file should be accomplished, so what is the
> best way to do this?
>
> std.file?
>
> Seems silly providing a program that analyses a text file, without telling the
> reader how to read in the text file, so I am wondering if there is some
> assumed knowledge I am missing?
>
> Regards.
>


Re: reading in text files

2011-08-24 Thread Christophe
> The default stdin doesn't have an end, and unless you type something 
> in, there's no input at all. That's why the program just hangs.

You can end keyboard stdin by typing ^D (Ctrl + D) under unix.


Re: What's the technical reason that class ctors aren't virtual?

2011-08-24 Thread Jacob Carlborg

On 2011-08-24 16:59, Andrej Mitrovic wrote:

class Foo
{
 this(int x, int y) { }
}

class Bar : Foo
{
}

Bar has to define its own ctor even if it only forwards the call to
the super() ctor, e.g.:

class Bar : Foo
{
 this(int x, int y) { super(x, y); }
}

But I'm curious why this works this way. If I have a large inheritance
tree of classes and I want to change the base class ctor (say I want
to add another int as a parameter), I'll have to change all the ctors
in the subclasses as well.

Isn't that counterproductive?


You can use a template mixin as a workaround.

--
/Jacob Carlborg


Re: What's the technical reason that class ctors aren't virtual?

2011-08-24 Thread Ary Manzana

On 8/24/11 12:27 PM, Ali Çehreli wrote:

On Wed, 24 Aug 2011 16:59:46 +0200, Andrej Mitrovic wrote:


class Foo
{
 this(int x, int y) { }
}

class Bar : Foo
{
}

Bar has to define its own ctor even if it only forwards the call to the
super() ctor, e.g.:

class Bar : Foo
{
 this(int x, int y) { super(x, y); }
}


That's because it is Bar that constructs its Foo part. The user
constructs a Bar and must provide only what Bar needs, potentially
nothing:

class Bar : Foo
{
 this() { super(42, 43); }


But I'm curious why this works this way.


Another reason would be function hijacking: Subtle changes in types of
contsructor parameters might bypass derived constructors just becaues
they now match better to the super's.


If I have a large inheritance
tree of classes and I want to change the base class ctor (say I want to
add another int as a parameter), I'll have to change all the ctors in
the subclasses as well.

Isn't that counterproductive?


Sounds like it but is necessary: Which of the constructors of the derived
constructors should the compiler call after calling super's matching one?
If it didn't, the derived parts would be left unconstructed.

Ali


I was against this feature but Ruby implements it and I love it. I see 
no technical reason not to do it. The rule should be: if the class 
doesn't provide any constructor, just copy the constructors from the 
base class.


Re: What's the technical reason that class ctors aren't virtual?

2011-08-24 Thread Ali Çehreli
On Wed, 24 Aug 2011 16:59:46 +0200, Andrej Mitrovic wrote:

> class Foo
> {
> this(int x, int y) { }
> }
> 
> class Bar : Foo
> {
> }
> 
> Bar has to define its own ctor even if it only forwards the call to the
> super() ctor, e.g.:
> 
> class Bar : Foo
> {
> this(int x, int y) { super(x, y); }
> }

That's because it is Bar that constructs its Foo part. The user 
constructs a Bar and must provide only what Bar needs, potentially 
nothing:

class Bar : Foo
{
this() { super(42, 43); }

> But I'm curious why this works this way.

Another reason would be function hijacking: Subtle changes in types of 
contsructor parameters might bypass derived constructors just becaues 
they now match better to the super's.

> If I have a large inheritance
> tree of classes and I want to change the base class ctor (say I want to
> add another int as a parameter), I'll have to change all the ctors in
> the subclasses as well.
> 
> Isn't that counterproductive?

Sounds like it but is necessary: Which of the constructors of the derived 
constructors should the compiler call after calling super's matching one? 
If it didn't, the derived parts would be left unconstructed.

Ali


What's the technical reason that class ctors aren't virtual?

2011-08-24 Thread Andrej Mitrovic
class Foo
{
this(int x, int y) { }
}

class Bar : Foo
{
}

Bar has to define its own ctor even if it only forwards the call to
the super() ctor, e.g.:

class Bar : Foo
{
this(int x, int y) { super(x, y); }
}

But I'm curious why this works this way. If I have a large inheritance
tree of classes and I want to change the base class ctor (say I want
to add another int as a parameter), I'll have to change all the ctors
in the subclasses as well.

Isn't that counterproductive?


Re: reading in text files

2011-08-24 Thread Andrej Mitrovic
Maybe ./readingHamlet < hamlet.txt


Re: reading in text files

2011-08-24 Thread Brian Brady
== Quote from Johannes Pfau (s...@example.com)'s article
> Brian Brady wrote:
> >All
> >
> >I am working through Andrei Alexandrescus "The D Programming Language"
> >but have hit a road block fairly early on.
> >
> >There is a program in the book which is designed to read through a
> >text file and do a simple word count. The program looks like this:
> >
> >import std.stdio, std.string;
> >
> >void main()
> >{
> >  //Compute counts
> >  uint[string] freqs;
> >  foreach(line; stdin.byLine())
> >  {
> >foreach(word; split(strip(line)))
> >{
> >  ++freqs[word.idup];
> >}
> >  }
> >
> >  //Prints count
> >  foreach(key, value; freqs)
> >  {
> >writefln("%6u\t%s", value, key);
> >  }
> >}
> >
> >My query is basically how to read the text file in?
> >
> >currently I am trying to use
> >./readingHamlet cat hamlet.txt
> >
> >but it just hangs there, not doing anything(for a considerable time)
> >so I am assuming I am doing something wrong. There isn't any actual
> >mention in the book of *how* reading in the text file should be
> >accomplished, so what is the best way to do this?
> >
> >std.file?
> >
> >Seems silly providing a program that analyses a text file, without
> >telling the reader how to read in the text file, so I am wondering if
> >there is some assumed knowledge I am missing?
> >
> >Regards.
> Hi,
> stdin.byLine() reads from the standard input, which is your
> console/keyboard input by default. The default stdin doesn't have an
> end, and unless you type something in, there's no input at all. That's
> why the program just hangs.
> On Linux/unix you can for example pipe the output from one command to
> another:
> cat hamlet.txt | ./readingHamlet
> this way readingHamlet's standard input is connected to cat's standard
> output.

This worked!! As I assumed, it was something simple :S

Thank you so much.


Re: reading in text files

2011-08-24 Thread Steven Schveighoffer

On Wed, 24 Aug 2011 10:25:18 -0400, Johannes Pfau  wrote:


On Linux/unix you can for example pipe the output from one command to
another:

cat hamlet.txt | ./readingHamlet

this way readingHamlet's standard input is connected to cat's standard
output.


I believe in all OSes (Windows included) ./readingHamlet < hamlet.txt  
works.


Could be wrong though.

-Steve


Re: reading in text files

2011-08-24 Thread Johannes Pfau
Brian Brady wrote:
>All
>
>I am working through Andrei Alexandrescus "The D Programming Language"
>but have hit a road block fairly early on.
>
>There is a program in the book which is designed to read through a
>text file and do a simple word count. The program looks like this:
>
>import std.stdio, std.string;
>
>void main()
>{
>  //Compute counts
>  uint[string] freqs;
>  foreach(line; stdin.byLine())
>  {
>foreach(word; split(strip(line)))
>{
>  ++freqs[word.idup];
>}
>  }
>
>  //Prints count
>  foreach(key, value; freqs)
>  {
>writefln("%6u\t%s", value, key);
>  }
>}
>
>My query is basically how to read the text file in?
>
>currently I am trying to use
>./readingHamlet cat hamlet.txt
>
>but it just hangs there, not doing anything(for a considerable time)
>so I am assuming I am doing something wrong. There isn't any actual
>mention in the book of *how* reading in the text file should be
>accomplished, so what is the best way to do this?
>
>std.file?
>
>Seems silly providing a program that analyses a text file, without
>telling the reader how to read in the text file, so I am wondering if
>there is some assumed knowledge I am missing?
>
>Regards.

Hi,
stdin.byLine() reads from the standard input, which is your
console/keyboard input by default. The default stdin doesn't have an
end, and unless you type something in, there's no input at all. That's
why the program just hangs.

On Linux/unix you can for example pipe the output from one command to
another:

cat hamlet.txt | ./readingHamlet

this way readingHamlet's standard input is connected to cat's standard
output.

-- 
Johannes Pfau



reading in text files

2011-08-24 Thread Brian Brady
All

I am working through Andrei Alexandrescus "The D Programming Language" but
have hit a road block fairly early on.

There is a program in the book which is designed to read through a text file
and do a simple word count. The program looks like this:

import std.stdio, std.string;

void main()
{
  //Compute counts
  uint[string] freqs;
  foreach(line; stdin.byLine())
  {
foreach(word; split(strip(line)))
{
  ++freqs[word.idup];
}
  }

  //Prints count
  foreach(key, value; freqs)
  {
writefln("%6u\t%s", value, key);
  }
}

My query is basically how to read the text file in?

currently I am trying to use
./readingHamlet cat hamlet.txt

but it just hangs there, not doing anything(for a considerable time) so I am
assuming I am doing something wrong. There isn't any actual mention in the
book of *how* reading in the text file should be accomplished, so what is the
best way to do this?

std.file?

Seems silly providing a program that analyses a text file, without telling the
reader how to read in the text file, so I am wondering if there is some
assumed knowledge I am missing?

Regards.


Re: How do "pure" member functions work?

2011-08-24 Thread Don

Simen Kjaeraas wrote:

On Mon, 22 Aug 2011 22:19:50 +0200, Don  wrote:

BTW: The whole "weak pure"/"strong pure" naming was just something I 
came up with, to convince Walter to relax the purity rules. I'd rather 
those names disappeared, they aren't very helpful.


The concepts are useful, but better names might be worth it. But what
short word eloquently conveys 'accesses no mutable global state'? :p


@nostatic  ?
stateless ?


What we call strongly pure is what in other languages is simply called
'pure', and that is likely the word that should be used for it.




Weakly pure is a somewhat different beast, and the 'best' solution would
likely be for it to be the default (But as we all know, this would
require changing the language too much. Perhaps in D3...). Noglobal
might be the best we have. My favorite thus far is 'conditionally pure'.
It conveys that the function is pure in certain circumstances, and not
in others. However, it might be somewhat diluted by the addition of pure
inference in newer versions of DMD - that definitely is conditionally
pure.

Const pure is not a concept I'm particularly familiar with. Is this the
special case of calling a conditionally pure function with only
const/immutable parameters, with arguments that are immutable in the
calling context, and that it in those cases can be considered
strongly pure?


No, it's where the signature contains const parameters, rather than 
immutable ones.
Two calls to a const-pure function, with the same parameters, may give 
different results. You'd need to do a deep inspection of the parameters,

to see if they changed.

Consider:

x = foo(y);
x = foo(y);
where foo is const-pure.
Perhaps y contains a pointer to x. In that case, foo could depend on x.
Or, there might be a mutable pointer to y somewhere, and x might have an 
opAssign which modifies y.

In each case, the second y is different to the first one.

But, if foo is immutable-pure, it will return the same value both times, 
so one of the calls can be optimized away.


Re: links to nested structure

2011-08-24 Thread Johannes Pfau
Adam D. Ruppe wrote:
>Jonathan M Davis wrote:
>> I'm fairly certain that the anchors are generated by ddoc, not
>> std.ddoc
>
>Well, it's a bit of both.
>
>DDOC_PSYMBOL = $(U $0)
>
>Looking at doc.c, DDOC_PSYMBOL is used in the toDocBuffer() methods.
>The paramater to it is always this.toChars().
>
>I'm not sure how to do it best. It could probably just do a
>parent.toChars() ~ "." ~ toChars() (conceptually) but then it
>would output stuff like:
>
>struct Outer.Inner
>
>which still isn't ideal.
>
>But, I think this might be ideal:
>
>("$(DDOC_PSYMBOL %s %s)", toChars(), parent.toChars() ~ [...]);
>
>
>Then, the macro in std.ddoc can be changed to be like this:
>
>DDOC_PSYMBOL = $(U $1)
>
>
>So you keep the simple info and the more detailed name.
>
>
>I've gotta look at the dmd source to see what's there for the
>parent member, but I'm sure there's something.
>
>
>Maybe I'll do up a pull request this weekend if nothing else
>comes up on my schedule.

Maybe toPrettyChars in dsymbol.c is useful here. AFAIK it's supposed to
output the full name of a symbol.

-- 
Johannes Pfau


signature.asc
Description: PGP signature