Re: Trying Multiple Aggregate reduce

2014-04-02 Thread monarch_dodra

On Monday, 31 March 2014 at 22:25:40 UTC, Nordlöw wrote:

You can't use reduce with a const seed.



This, surely, must be a compiler bug right?

/Per


Arguably, it's a user bug ;) The user should have provided a 
non-const seed.


*But*, there have been many cases of reduce being made to accept 
const seeds before, via unqualified copy. This case must have 
just been a missed one.


In any case, I had submitted a re-write for reduce, and it just 
so happens to support this. So I added your code to the test 
cases:

https://github.com/D-Programming-Language/phobos/pull/2060


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Russel Winder
On Wed, 2014-04-02 at 00:34 +, bearophile wrote:
 Alexandre L.:

  int main(string[] args)
  {
 
 If you don't need args, then I suggest to not put it as main 
 argument. So probably this is better (note no int nor return, in 
 D they are not needed):
 
 void main() {
 ...
 }

I am not convinced by this argument. The return value (aka exit code) is
always present, if you ignore this by having a void main, it will return
0, i.e. main is not a void function even if you claim it is. As for
ignoring the parameters, this is making use of the fact that main is the
only function where you do not have to present the correct signature.
Perhaps this exception should be removed.

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)

what is the real signature in D? 

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



Re: Trying Multiple Aggregate reduce

2014-04-02 Thread Nordlöw
so happens to support this. So I added your code to the test 
cases:


Great!

BTW: Why is static qualifier needed on definition of 
minmaxElement() in the unittest?


Re: Trying Multiple Aggregate reduce

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 09:25:53 UTC, Nordlöw wrote:
so happens to support this. So I added your code to the test 
cases:


Great!

BTW: Why is static qualifier needed on definition of 
minmaxElement() in the unittest?


Whenever you declare something in a nested context, it may or may 
not have a hidden context pointer, depending on the type, and the 
implementation.


static, in this context, ensures this does not happen. It's not 
actually *needed* in this context though. It's more of a matter 
of style.


In your example, the function was declared in global context, so 
the static would have been gratuitous.


core.atomic and -profile switch

2014-04-02 Thread Saurabh Das

Hello

For this test program ('test.d'):

import core.atomic;
int func1(shared int a)
{
return atomicLoad(a);
}

These invocations of dmd succeed:
1. dmd -main test.d
2. dmd -main -debug test.d
3. dmd -main -release test.d

But this one fails:
dmd -main -profile test.d

With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: asm 
statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile with 
'-profile'.


Warm Regards,
Saurabh



Re: core.atomic and -profile switch

2014-04-02 Thread bearophile

Saurabh Das:


With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: asm 
statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile 
with '-profile'.


Isn't this a recent regression?

Bye,
bearophile


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread bearophile

Russel Winder:


what is the real signature in D?


The _real_ signature of main() is flexible, you can use:

void main()
int main(in string[] args) pure nothrow

D allows you to omit the args if you don't need them, returns 0 
if you don't it, and it can be pure/nothrow/@safe as desired.




Perhaps this exception should be removed.


This special cases cause zero bugs and zero problems, so there is 
no chance to change this, even if we want (and I don't want).


Bye,
bearophile


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Dicebot

On Wednesday, 2 April 2014 at 08:55:23 UTC, Russel Winder wrote:

On Wed, 2014-04-02 at 00:34 +, bearophile wrote:

Alexandre L.:



 int main(string[] args)
 {

If you don't need args, then I suggest to not put it as main 
argument. So probably this is better (note no int nor return, 
in D they are not needed):


void main() {
...
}


I am not convinced by this argument. The return value (aka exit 
code) is
always present, if you ignore this by having a void main, it 
will return
0, i.e. main is not a void function even if you claim it is. As 
for
ignoring the parameters, this is making use of the fact that 
main is the
only function where you do not have to present the correct 
signature.

Perhaps this exception should be removed.

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)

what is the real signature in D?


D main != C main, latter is implemented in D runtime to call the 
former. 0 will be also returned by latter, not the former. Also 
exception will result in 0 status code even if return type is 
void. It effectively just says that you won't manage status code 
manually and allows runtime to take care of it.


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Andrej Mitrovic
On 4/2/14, Dicebot pub...@dicebot.lv wrote:
 D main != C main, latter is implemented in D runtime to call the
 former. 0 will be also returned by latter, not the former.

Actually, the compiler injects a return statement in D's main.

It generates the actual C main function (unless WinMain/DllMain is
provided), which calls another special D runtime init function, which
itself calls the D main function (which itself has the injected return
statement unless it's already an int return).

It's quite complicated, but it's all open-source so you can inspect it.


Re: Signature of main [was Sockets between D and C(++) app]

2014-04-02 Thread Ola Fosheim Grøstad

On Wednesday, 2 April 2014 at 08:55:23 UTC, Russel Winder wrote:

The real signature of main in C/C++ is, I believe:

int main(int, char**, char**)


I believe in C it is:

int main(void)
int main(int,char**)

or implementation defined (e.g. the third env pointer in the main 
signature from unix)


But the actual entry point is system dependent and can be 
specified as a link option? On linux _start or something like 
that.


unicode console output

2014-04-02 Thread Denis Mezhov

I'm trying out to windows console unicode latin string

writeln(aaabbb);
console out: aaabbb

trying out to console unicode cyrillic string
writeln(бббггг);
console out: ╨│╨│╨│╨▒╨▒╨▒

How to out unicode cyrillic string to console?


Re: unicode console output

2014-04-02 Thread bearophile

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


Re: unicode console output

2014-04-02 Thread MGW

On Wednesday, 2 April 2014 at 12:47:06 UTC, Denis Mezhov wrote:

I'm trying out to windows console unicode latin string

writeln(aaabbb);
console out: aaabbb

trying out to console unicode cyrillic string
writeln(бббггг);
console out: ╨│╨│╨│╨▒╨▒╨▒

How to out unicode cyrillic string to console?

-

On the website http://dlang.ru all the details are written about 
re-encode the video into Cyrillic.


Re: core.atomic and -profile switch

2014-04-02 Thread Saurabh Das


I see. I wasn't sure - hence I asked.

In general, how do I check if this is a known issue?

Thanks,
Saurabh

On Wednesday, 2 April 2014 at 11:19:16 UTC, bearophile wrote:

Saurabh Das:


With error:
/usr/include/dmd/druntime/import/core/atomic.d(910): Error: 
asm statements are assumed to throw


Is there a workaround for this? I have a decent sized codebase 
which I wish to profile for hotspots - but it won't compile 
with '-profile'.


Isn't this a recent regression?

Bye,
bearophile




Re: core.atomic and -profile switch

2014-04-02 Thread bearophile

Saurabh Das:


I see. I wasn't sure - hence I asked.


https://d.puremagic.com/issues/show_bug.cgi?id=11471



In general, how do I check if this is a known issue?


Search in Bugzilla and/or ask to people.

Bye,
bearophile


Re: unicode console output

2014-04-02 Thread Denis Mezhov

On Wednesday, 2 April 2014 at 12:51:57 UTC, bearophile wrote:

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


chcp 65001 dont'work


start.bat

mode con cols=150 lines=50
chcp 65001
%Path%\Minesweeper\Debug\Mi.exe

don't out correct string


Re: unicode console output

2014-04-02 Thread Jos van Uden

On 2-4-2014 15:38, Denis Mezhov wrote:

On Wednesday, 2 April 2014 at 12:51:57 UTC, bearophile wrote:

Denis Mezhov:


How to out unicode cyrillic string to console?


Try this command on the command line:

chcp 65001

Bye,
bearophile


chcp 65001 dont'work


start.bat

mode con cols=150 lines=50
chcp 65001
%Path%\Minesweeper\Debug\Mi.exe

don't out correct string


Do you have a Unicode font selected in the dosbox properties,
like Lucida Console ?


When this will be freed?

2014-04-02 Thread Andrea Fontana


auto example(char* test) { return toStringz(to!string(test) ~  
world!); }


When that return string will be freed?

What about:

extern(C) auto example()

?





Re: When this will be freed?

2014-04-02 Thread Jacob Carlborg

On 2014-04-02 17:45, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~  world!); }

When that return string will be freed?


When there is no reference left to the string, the garbage collector is 
free to collect it when it chooses to.



What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is collected. If it 
is a C function, then it depends entirely on that particular function.


--
/Jacob Carlborg


Re: When this will be freed?

2014-04-02 Thread John Colvin

On Wednesday, 2 April 2014 at 15:45:06 UTC, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~  
world!); }


When that return string will be freed?

What about:

extern(C) auto example()

?


to!string allocates on the GC heap when given a char* (it has to, 
in order to safely produce immutable data in the string).


It will be freed by the first garbage collection ocurring after 
all references to that memory are dead.


Re: When this will be freed?

2014-04-02 Thread Andrea Fontana

On Wednesday, 2 April 2014 at 15:53:52 UTC, Jacob Carlborg wrote:

On 2014-04-02 17:45, Andrea Fontana wrote:

auto example(char* test) { return toStringz(to!string(test) ~ 
 world!); }


When that return string will be freed?


When there is no reference left to the string, the garbage 
collector is free to collect it when it chooses to.




That's expected.


What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is 
collected. If it is a C function, then it depends entirely on 
that particular function.


I mean: if it is an exported function (of a shared library) what 
happens? There's no reference kept anywhere (in D). So if I'm 
right it could be freed immediatly by GC. Right?




Re: When this will be freed?

2014-04-02 Thread Benjamin Thaut

Am 02.04.2014 17:57, schrieb Andrea Fontana:

On Wednesday, 2 April 2014 at 15:53:52 UTC, Jacob Carlborg wrote:

On 2014-04-02 17:45, Andrea Fontana wrote:


auto example(char* test) { return toStringz(to!string(test) ~ 
world!); }

When that return string will be freed?


When there is no reference left to the string, the garbage collector
is free to collect it when it chooses to.



That's expected.


What about:

extern(C) auto example()


Same as above, extern(C) does not change how memory is collected. If
it is a C function, then it depends entirely on that particular function.


I mean: if it is an exported function (of a shared library) what
happens? There's no reference kept anywhere (in D). So if I'm right it
could be freed immediatly by GC. Right?



If you pass that string to a C function, there is a reference on the 
stack. So this string will not be freed until that C-function returns. 
If that C-Function returns, it is very likely however that this was the 
only reference and the string will be freed the next time the garbage 
collector runs.


Kind Regards
Benjamin Thaut


How to repeat a function call?

2014-04-02 Thread Simen Kjærås
I'm trying to create a function that repeats a function call N times. 
The exact use case is generating an N-dimensional tuple:


import std.typetuple;
import std.typecons;

template Repeat(size_t n, T...) {
static if (n == 1) {
alias Repeat = T;
} else static if (n) {
alias Repeat = TypeTuple!(Repeat!(n /2, T), Repeat!((n +1)/2, T));
} else {
alias Repeat = TypeTuple!();
}
}

auto fun1(size_t dim, T)(lazy T fn) {
return tuple(Repeat!(dim, fn));
}

auto fun2(size_t dim, alias fn)() {
return tuple(Repeat!(dim, fn));
}

void main() {
int a = 0;
assert(fun1!3(a++) == tuple(0,1,2));
//assert(fun2!(3, ()=a++) == tuple(0,1,2));
}

Now, the call to fun1 works great. But I'd like to specify fn at 
compile-time, thus doing something more like fun2. fun2 of course, does 
not work (else why would I ask?).


I tried staticMap'ing a template that calls its parameter over the 
result of Repeat in Fun2, but that did not work:


template call(alias fn) {
alias call = TypeTuple!(fn());
}

auto fun3(size_t dim, alias fn)() {
return tuple(staticMap!(call, Repeat!(dim, fn)));
}

fun3 ends up trying to evaluate the function call at compile-time, and 
fails because a++ can't be executed until run-time.


Better ideas, please?

--
  Simen


Re: How to repeat a function call?

2014-04-02 Thread w0rp
tuples are definitely a compile-time job. You could do something 
like this to build an N tuple by calling a function N many times.


---
import std.typecons;

int foo() {
return 3;
}

auto initTuple(size_t N, alias func)() {
string magic() {
string result = return tuple(;

foreach(i; 0..N) {
result ~= func(),;
}

result ~= );;

return result;
}

mixin(magic());
}

void main(string[] argv) {
enum Tuple!(int, int, int) tup = initTuple!(3, foo);
}

---

This just builds the tuple by building up a mixin string which 
calls the function so many times, with no guarantees about the 
order of arguments. If you have side-effects in your function 
though, you probably want to move it runtime anyway and use a 
range with the 'repeat' function, etc.


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
  Simen


I only skimmed through your post, but have you tried taking a 
look at adjoin? Preferably, the version in head? Something like 
adjoin!(repeat!(fun, N)) seems to natively do what you are 
asking for?


I'll re-read your post later tonight, and if I'm misunderstood, 
I'll try to provide a better solution.


How do I obtain the default hash of a user-defined struct

2014-04-02 Thread dnspies
How can I get the default-hash of a struct I've defined (to be 
used as part of the hash for some containing type)?


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread FreeSlave

On Wednesday, 2 April 2014 at 20:14:31 UTC, dnspies wrote:
How can I get the default-hash of a struct I've defined (to be 
used as part of the hash for some containing type)?


UserDefined userDefined;
writeln(typeid(UserDefined).getHash(userDefined));

Probably there is a better way. I don't like to call typeid for 
this purpose.


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:33:28 UTC, w0rp wrote:

auto initTuple(size_t N, alias func)() {
string magic() {
string result = return tuple(;

foreach(i; 0..N) {
result ~= func(),;
}

result ~= );;

return result;
}

mixin(magic());
}


auto initTuple(size_t N, alias func)()
{
return mixin(q{tuple(%-(%s, %))}.format(func.repeat(N)));
}


Re: How to repeat a function call?

2014-04-02 Thread monarch_dodra

On Wednesday, 2 April 2014 at 19:54:38 UTC, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
 Simen


I only skimmed through your post, but have you tried taking a 
look at adjoin? Preferably, the version in head? Something like 
adjoin!(repeat!(fun, N)) seems to natively do what you are 
asking for?


I'll re-read your post later tonight, and if I'm misunderstood, 
I'll try to provide a better solution.


Indeed, assert(adjoin!(Repeat!(3, ()=a++))() == tuple(0,1,2)); 
seems to work for me.


That said, while I think the behavior is *defined*, I'm not sure 
it's *specified*.


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread dnspies

Thanks,
Actually I'm realizing there's a lot I'm unclear about when it 
comes to default comparison, equality, hashing, etc.


If my struct contains a dynamic array, are the contents of the 
array compared by default, or just the pointers/lengths?


Also, when two arrays are compared for content, are their 
pointers compared first in case they happen to be the same so the 
deep comparison can be shortcut?
Also, how do I obtain the default hash of a dynamic array ('s 
contents)?  Is there one?  I assume there must be since 
associative arrays can take string as a key-type.


To make a struct a valid key type, do I need to implement both 
opCmp and opEquals or just one or the other.  It says on the page 
about Associative Arrays: The implementation may use either 
opEquals or opCmp or both.  Does that mean it uses whichever one 
is user-defined (or both if they're both user-defined)?  Or does 
it mean the user is responsible for defining both?


Also, it says Care should be taken so that the results of 
opEquals and opCmp are consistent with each other when the 
struct/union objects are the same or not., certainly this means 
that if a.opEquals(b), then a.opCmp(b) should be 0, but does the 
converse have to be true?


Is there somewhere I can find information about default operator 
implementations and consistency?


Re: How to repeat a function call?

2014-04-02 Thread Simen Kjærås

On 2014-04-02 20:48, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:54:38 UTC, monarch_dodra wrote:

On Wednesday, 2 April 2014 at 19:02:29 UTC, Simen Kjærås wrote:

Better ideas, please?

--
 Simen


I only skimmed through your post, but have you tried taking a look at
adjoin? Preferably, the version in head? Something like
adjoin!(repeat!(fun, N)) seems to natively do what you are asking for?

I'll re-read your post later tonight, and if I'm misunderstood, I'll
try to provide a better solution.


Indeed, assert(adjoin!(Repeat!(3, ()=a++))() == tuple(0,1,2)); seems
to work for me.

That said, while I think the behavior is *defined*, I'm not sure it's
*specified*.


True, in the case of a++, it's probably a bad idea. In my case, the 
function is uniform(0.0, 1.0, rng), and I'm happy with it simply being 
random.


Thanks a lot!

--
  Simen


Re: Sockets between D and C(++) app

2014-04-02 Thread FreeSlave

It's only server. Maybe problem is on client side.

Try this if you are on Linux:

//Linux C client
#include sys/socket.h
#include sys/types.h
#include netinet/in.h
#include arpa/inet.h
#include stddef.h
#include string.h
#include stdio.h

int main()
{
int sock, res;
struct sockaddr_in addr;
const char* hello;
size_t len;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock  0)
{
printf(Can't create socket\n);
return -1;
}

addr.sin_family = AF_INET;
addr.sin_port = htons(5432);
addr.sin_addr.s_addr = inet_addr(127.0.0.1);

hello = Hello from C;
len = strlen(hello);
while (1)
{
res = sendto(sock, hello, len, 0, (struct 
sockaddr*)addr, sizeof(addr));

if (res = 0)
{
printf(Can't send\n);
return -1;
}
}
close(sock);
return 0;
}


//D server
import std.socket;
import std.stdio;

int main(string[] args)
{
auto s = new UdpSocket(AddressFamily.INET);

auto addr = new InternetAddress(127.0.0.1, 5432);
s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, 
true);

s.bind(addr);

while (true)
{
ubyte[2048] recv_buf;
int count = s.receiveFrom(recv_buf[]);
char[] test = cast(char[])recv_buf;

writefln(Received: %s\n, test);
}
return 0;
}


Note that you don't need to remove zero-symbol if you don't pass 
it from client.


Re: How do I obtain the default hash of a user-defined struct

2014-04-02 Thread FreeSlave
Contents of struct are compared field by field using comparison 
for the type of each field. Dynamic arrays are compared by 
contents. If you want to compare them by pointer use .ptr 
property.


opEquals and opCmp are not about hashing, I believe. They are 
just operators to help when dealing with chaining when different 
objects have same hash (since hash may be not unique)


execute commands in the terminal OSX

2014-04-02 Thread Joel
It says in the Language Reference not to use system and gives 
other options. But the other options don't seem to work. I just 
have code system(clear);


Re: template interface and delegates

2014-04-02 Thread Kenji Hara
On Tuesday, 1 April 2014 at 19:55:05 UTC, Steven Schveighoffer 
wrote:
On Tue, 01 Apr 2014 15:47:42 -0400, anonymous 
n...@trash-mail.com wrote:


Is this bug allready reported? or can somebody who has a 
deeper insight to this report it?


I don't know. I think you should report it. If it's already 
reported, someone will close it as a duplicate


-Steve


I filed it.

https://d.puremagic.com/issues/show_bug.cgi?id=12508

Kenji Hara