Re: Looking for writing parallel foreach kind of statement for nested for-loops

2013-02-09 Thread FG

On 2013-02-10 02:22, Sparsh Mittal wrote:

The current reply answers my question, but I was just curious. Can we have a
method which divides the 2d region as follows: 8*12 divided into 4*6 to each of
4 threads.


Think again if you need that. Things start getting pretty ugly. :)

const uint parts = 2; // in each dimension
foreach (block; parallel(iota(parts*parts))) {
uint p1 = block / parts;
foreach (i; (p1*N/parts)..(p1==parts-1 ? N : (p1+1)*N/parts)) {
uint p2 = block % parts;
foreach (j; (p2*M/parts)..(p2==parts-1 ? M : (p2+1)*M/parts)) {
func(i, j, p1, p2, block);
}
}
}




Re: Creating an array of default-constructed class instances

2013-02-09 Thread Simon

On Sunday, 10 February 2013 at 06:57:42 UTC, Ali Çehreli wrote:

On 02/09/2013 10:52 PM, Ali Çehreli wrote:

>   auto things = iota(10).map!(i => new Thing(i))().array;

Actually, without the extra parentheses:

auto things = iota(10).map!(i => new Thing(i)).array;

Ali


It's a shame there isn't any simple syntax for it, but that's 
some neat and flexible code.


Thanks!


Re: Creating an array of default-constructed class instances

2013-02-09 Thread Ali Çehreli

On 02/09/2013 10:52 PM, Ali Çehreli wrote:

>   auto things = iota(10).map!(i => new Thing(i))().array;

Actually, without the extra parentheses:

auto things = iota(10).map!(i => new Thing(i)).array;

Ali



Re: Creating an array of default-constructed class instances

2013-02-09 Thread Ali Çehreli

On 02/09/2013 10:14 PM, Simon wrote:
> Hi, I'm new to the D programming language. Overall I'm liking
> things very much, but I'm still getting the hang of a few things.
>
> Here's a basic programming pattern: I have a class called Thing,
> and while I'm coding I decide I need N Thing instances.
>
> In C++ that's a matter of
>
> std::vector things(N);
>
> In python, I can use a list comprehension.
>
> things = [Thing() for _ in range(N)]
>
> However, the obvious D version doesn't work.
>
> auto things = new Thing[N];
>
> Because Thing.init is null, this produces an array of null
> references. Of course, I can write a for loop to fill in the
> array after creation, but this feels very un-D-like. Is there a
> straightforward way to create a bunch of class instances?

Here is one way:

import std.stdio;
import std.range;
import std.algorithm;

class Thing
{
int i;

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

void main()
{
auto things = iota(10).map!(i => new Thing(i))().array;
}

Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html



Re: Creating an array of default-constructed class instances

2013-02-09 Thread Maxim Fomin

On Sunday, 10 February 2013 at 06:14:37 UTC, Simon wrote:

Hi, I'm new to the D programming language.  Overall I'm liking
things very much, but I'm still getting the hang of a few 
things.


Here's a basic programming pattern: I have a class called Thing,
and while I'm coding I decide I need N Thing instances.

In C++ that's a matter of

std::vector things(N);

In python, I can use a list comprehension.

things = [Thing() for _ in range(N)]

However, the obvious D version doesn't work.

auto things = new Thing[N];

Because Thing.init is null, this produces an array of null
references.  Of course, I can write a for loop to fill in the
array after creation, but this feels very un-D-like.  Is there a
straightforward way to create a bunch of class instances?


You can create separate function which fills array with instances 
and return it. You can make Thing a struct aliased to array of 
classes which again initializes properly in constructor an array. 
But then you should take care that struct constructor is called.


Creating an array of default-constructed class instances

2013-02-09 Thread Simon

Hi, I'm new to the D programming language.  Overall I'm liking
things very much, but I'm still getting the hang of a few things.

Here's a basic programming pattern: I have a class called Thing,
and while I'm coding I decide I need N Thing instances.

In C++ that's a matter of

std::vector things(N);

In python, I can use a list comprehension.

things = [Thing() for _ in range(N)]

However, the obvious D version doesn't work.

auto things = new Thing[N];

Because Thing.init is null, this produces an array of null
references.  Of course, I can write a for loop to fill in the
array after creation, but this feels very un-D-like.  Is there a
straightforward way to create a bunch of class instances?


Re: Error: not a property - ?

2013-02-09 Thread Era Scarecrow
On Sunday, 10 February 2013 at 03:45:37 UTC, Jonathan M Davis 
wrote:

On Sunday, February 10, 2013 04:38:56 Era Scarecrow wrote:
Where should I add to the bug tracker so the error message can 
be improved?


Just create a new bug report for it: 
http://d.puremagic.com/issues


- Jonathan M Davis


 Found it's bug 5010; Added comment for that specific case.

 http://d.puremagic.com/issues/show_bug.cgi?id=5010


Re: Error: not a property - ?

2013-02-09 Thread Jonathan M Davis
On Sunday, February 10, 2013 04:38:56 Era Scarecrow wrote:
>   Where should I add to the bug tracker so the error message can
> be improved?

Just create a new bug report for it: http://d.puremagic.com/issues

- Jonathan M Davis


Re: Error: not a property - ?

2013-02-09 Thread Era Scarecrow
On Sunday, 10 February 2013 at 03:24:37 UTC, Jonathan M Davis 
wrote:

On Sunday, February 10, 2013 04:16:26 Era Scarecrow wrote:
This is an unexpected error; Seems it's the result of not 
down-casting a variable-type. Is this a bug? Or just need a 
better error message?


Version: DMD32 D Compiler v2.061


   struct S {
 void test(uint v) @property {}
 void test2(ulong v) @property {}
   }

   ulong x;
   S s;

   s.test2 = x;
   s.test = x;  //s.test is not a property


It's not a bug. The assignment is illegal, because a you can't 
assign a ulong to a uint without casting, but the error message 
obviously isn't very good. It probably gives that error 
whenever you try and use the property syntax, and there's no 
variable or function with that name that compiles with the 
given argument. It really should be indicating that the 
argument itself is invalid, but however the combination of 
lowering and error messages works, you end up with a less than 
helpful error message.


 I figured as much. Very difficult to figure out the culprit when 
it's from bitfields mixed with perfectly normal code for 
assignment.


 Where should I add to the bug tracker so the error message can 
be improved?


Re: Error: not a property - ?

2013-02-09 Thread Jonathan M Davis
On Sunday, February 10, 2013 04:16:26 Era Scarecrow wrote:
>   This is an unexpected error; Seems it's the result of not
> down-casting a variable-type. Is this a bug? Or just need a
> better error message?
> 
> Version: DMD32 D Compiler v2.061
> 
> 
>struct S {
>  void test(uint v) @property {}
>  void test2(ulong v) @property {}
>}
> 
>ulong x;
>S s;
> 
>s.test2 = x;
>s.test = x;  //s.test is not a property

It's not a bug. The assignment is illegal, because a you can't assign a ulong 
to a uint without casting, but the error message obviously isn't very good. It 
probably gives that error whenever you try and use the property syntax, and 
there's no variable or function with that name that compiles with the given 
argument. It really should be indicating that the argument itself is invalid, 
but however the combination of lowering and error messages works, you end up 
with a less than helpful error message.

- Jonathan M Davis


Error: not a property - ?

2013-02-09 Thread Era Scarecrow
 This is an unexpected error; Seems it's the result of not 
down-casting a variable-type. Is this a bug? Or just need a 
better error message?


Version: DMD32 D Compiler v2.061


  struct S {
void test(uint v) @property {}
void test2(ulong v) @property {}
  }

  ulong x;
  S s;

  s.test2 = x;
  s.test = x;  //s.test is not a property


Re: Line gaps with text files

2013-02-09 Thread Joel

This works better:

import std.stdio;
import std.file;

void main() {
//string s = readText("s.txt");

string s;
foreach(line; File("s.txt", "r").byLine()) {
s ~= line~"\r\n";
}
s = s[0..$-2];

writeln([s]);

File("s.txt", "w").write(s);
}

But one text file program says it has solitary carriage returns 
in it.


Re: For DLLs, what does export actually do?

2013-02-09 Thread Ben Davis

On 09/02/2013 20:39, Benjamin Thaut wrote:

Am 09.02.2013 21:35, schrieb Ben Davis:

Hi,

I'm working on a multimedia driver DLL, i.e. one that Windows loads on
behalf of any program that uses the Windows multimedia API.

I'm using a .def file with an EXPORTS section, and I've also got all the
relevant functions marked as 'export' in the code.

What I'm finding is, if I remove the 'export' attribute but keep the
EXPORTS section, it all still works; but if I keep the 'export'
attribute and remove the EXPORTS section, then it doesn't work.

Just wondering why this is, and whether the 'export' attribute is
actually programmed to do anything at the moment :)

Thanks,

Ben :)


Yes it is, but its only working properly for global functions. It works
partially for classes and not at all for all remaining cases. The export
attribute is currently only usefull if you want to do a C-Style
interface for your Dll.


My functions are "export extern (Windows)" - I think they're global...?

For example:

export extern(Windows) LRESULT DriverProc(DWORD_PTR dwDriverId, HDRVR 
hdrvr, UINT msg, LONG lParam1, LONG lParam2) nothrow { ... }


Re: For DLLs, what does export actually do?

2013-02-09 Thread Ben Davis

On 09/02/2013 20:44, Andrej Mitrovic wrote:

On 2/9/13, Ben Davis  wrote:

Hi,

I'm working on a multimedia driver DLL, i.e. one that Windows loads on
behalf of any program that uses the Windows multimedia API.

I'm using a .def file with an EXPORTS section, and I've also got all the
relevant functions marked as 'export' in the code.


Export in code allows you to avoid listing exported functions in the
DEF file. However you should likely still use the DEF file even if it
lacks a list, as I recall there's a bug related to not using DEF files
(I can't find the issue right now).


(Oops, silly Thunderbird)

I know which issue you mean, because I came across it earlier. It's 
something about needing "EXETYPE NT" and "SUBSYSTEM WINDOWS" or else 
something bad happens with thread-local storage. I don't think it had 
anything to do with exports.


What you're telling me (about the purpose of export in code) is what I 
assumed was the intention, but the fact is it's not behaving that way. 
If I mark the functions as export in the code, but remove the EXPORTS 
section from the .def file (but keep the rest of the .def file), then 
the DLL doesn't work, which suggests that the export table did not 
contain those functions.


Perhaps the exports in the code only apply if the .def is entirely 
missing? Is that the desired behaviour? (I won't be trying that, as I 
know it would break in other ways then.)


Ben :)


Re: std.process.system and friends

2013-02-09 Thread bioinfornatics



I'm trying to write a script in D for building D projects.




They are dbuilder who do this . you can fork it 
https://github.com/dbuilder-developers/dbuilder


Line gaps with text files

2013-02-09 Thread Joel
I'm using Windows and get gaps (blank lines) when using readText 
and std.stdio.write.


import std.stdio;
import std.file;

void main() {
string s = readText("s.txt");
writeln('[', s, ']');
File("s.txt", "w").write(s);
}

notepad.exe opens alright, (if you don't save as File(.., "wb")). 
But I'm having trouble with some others like GtkD. Also writeln 
always does it right.


Re: Looking for writing parallel foreach kind of statement for nested for-loops

2013-02-09 Thread Sparsh Mittal



for(int i=1; i< N; i++)<==>foreach(i; iota(1, N))
so you can use:  foreach(i; parallel(iota(1, N))) { ... }
Thanks a lot. This one divides the x-cross-y region by rows. 
Suppose dimension is 8*12 and 4 parallel threads are there, so 
current method is dividing by 2*12 to each of 4 threads.


The current reply answers my question, but I was just curious. 
Can we have a method which divides the 2d region as follows: 8*12 
divided into 4*6 to each of 4 threads.








Re: A little of coordination for Rosettacode

2013-02-09 Thread bearophile

SomeDude:

...

Partial translation of rcrpg-Python:
http://codepad.org/SflrKqbT

Partial translation of 
permutations_rank_of_a_permutation-Python:

http://codepad.org/El9SQwOE
Plus a better Perl implementation of the uniform() on BigInts:
http://codepad.org/LGcMpk2f

Partial translation of the universal_turing_machine-Ruby:
http://codepad.org/nUXLzAg2

Bye,
bearophile


codepad.org doesn't work at all here. Maybe you should use 
dpaste (or pastebin for other languages) instead ?


Links in the same order:
http://dpaste.dzfl.pl/10eff346
http://dpaste.dzfl.pl/bfde8010
http://dpaste.dzfl.pl/3a172308
http://dpaste.dzfl.pl/22b7a931

Bye,
bearophile


Re: A little of coordination for Rosettacode

2013-02-09 Thread Jos van Uden

On 9-2-2013 23:14, SomeDude wrote:
 

codepad.org doesn't work at all here. Maybe you should use dpaste (or pastebin 
for other languages) instead ?


It's not working for me either. It's probably a temporary problem. It worked 
fine before.


Re: search of a workaround

2013-02-09 Thread Namespace

This works so far:

auto funcAttr = functionAttributes!(fun);
if (FunctionAttribute.pure_ & funcAttr) s ~= " pure";
if (FunctionAttribute.nothrow_ & funcAttr) s ~= " nothrow";
if (FunctionAttribute.ref_ & funcAttr) s ~= " ref";
if (!isMutable!(typeof(fun))) s ~= " const";

But it's curious that FunctionAttributes has no const, immutable, 
inout or shared...


Re: Looking for writing parallel foreach kind of statement for nested for-loops

2013-02-09 Thread FG

On 2013-02-09 23:32, Sparsh Mittal wrote:

I saw foreach and parallel commands on
http://dlang.org/phobos/std_parallelism.html

I have nested for-loops as follows:

for(int i=1; i< N; i++)
  {
   for(int j=1; j < M; j++)
{
  func(i,j);
}
  }

Here func(i,j) is such that there is no dependency or communication b/w
different (i,j) tuple values. Thus, problem is very simple.


Huh?
for(int i=1; i< N; i++)<==>foreach(i; iota(1, N))
so you can use:  foreach(i; parallel(iota(1, N))) { ... }



Re: search of a workaround

2013-02-09 Thread Namespace

On Saturday, 9 February 2013 at 22:23:07 UTC, Namespace wrote:

It seems your template has problems with this:

struct A { }

class B {
public:
const int bar(ref A a) {
return 42;
}

mixin(rvalue!(bar));
}

remove the 'const' and it works fine.


It seems that D has no functionality, to detect if a method is 
const.


Re: search of a workaround

2013-02-09 Thread Namespace

It seems your template has problems with this:

struct A { }

class B {
public:
const int bar(ref A a) {
return 42;
}

mixin(rvalue!(bar));
}

remove the 'const' and it works fine.


Re: A little of coordination for Rosettacode

2013-02-09 Thread SomeDude

On Tuesday, 5 February 2013 at 20:10:37 UTC, bearophile wrote:

Jos van Uden:

I'll give it a shot if you like. The RCRPG I'd like to try 
first.


I have already partially written those:

Partial translation of rcrpg-Python:
http://codepad.org/SflrKqbT

Partial translation of 
permutations_rank_of_a_permutation-Python:

http://codepad.org/El9SQwOE
Plus a better Perl implementation of the uniform() on BigInts:
http://codepad.org/LGcMpk2f

Partial translation of the universal_turing_machine-Ruby:
http://codepad.org/nUXLzAg2

Bye,
bearophile


codepad.org doesn't work at all here. Maybe you should use dpaste 
(or pastebin for other languages) instead ?


Re: eating whitespace

2013-02-09 Thread Peter Sommerfeld
Am 09.02.2013, 22:30 Uhr, schrieb Andrej Mitrovic  
:



 "f\too\t\n\rb\nar".removechars("\t\n\r")


thanks, works...

Peter


Re: eating whitespace

2013-02-09 Thread Andrej Mitrovic
On 2/9/13, Peter Sommerfeld  wrote:
> I wonder what the best, that means the fastest  way is
> to remove all whitespace " \t\n\r" from as string.
> Whitespace are distributed erratically within the string.
>
> Peter
>

Try "f\too\t\n\rb\nar".removechars("\t\n\r")


eating whitespace

2013-02-09 Thread Peter Sommerfeld

I wonder what the best, that means the fastest  way is
to remove all whitespace " \t\n\r" from as string.
Whitespace are distributed erratically within the string.

Peter


Re: For DLLs, what does export actually do?

2013-02-09 Thread Andrej Mitrovic
On 2/9/13, Ben Davis  wrote:
> Hi,
>
> I'm working on a multimedia driver DLL, i.e. one that Windows loads on
> behalf of any program that uses the Windows multimedia API.
>
> I'm using a .def file with an EXPORTS section, and I've also got all the
> relevant functions marked as 'export' in the code.

Export in code allows you to avoid listing exported functions in the
DEF file. However you should likely still use the DEF file even if it
lacks a list, as I recall there's a bug related to not using DEF files
(I can't find the issue right now).


Re: For DLLs, what does export actually do?

2013-02-09 Thread Benjamin Thaut

Am 09.02.2013 21:35, schrieb Ben Davis:

Hi,

I'm working on a multimedia driver DLL, i.e. one that Windows loads on
behalf of any program that uses the Windows multimedia API.

I'm using a .def file with an EXPORTS section, and I've also got all the
relevant functions marked as 'export' in the code.

What I'm finding is, if I remove the 'export' attribute but keep the
EXPORTS section, it all still works; but if I keep the 'export'
attribute and remove the EXPORTS section, then it doesn't work.

Just wondering why this is, and whether the 'export' attribute is
actually programmed to do anything at the moment :)

Thanks,

Ben :)


Yes it is, but its only working properly for global functions. It works 
partially for classes and not at all for all remaining cases. The export 
attribute is currently only usefull if you want to do a C-Style 
interface for your Dll.


Kind Regards
Benjamin Thaut


For DLLs, what does export actually do?

2013-02-09 Thread Ben Davis

Hi,

I'm working on a multimedia driver DLL, i.e. one that Windows loads on 
behalf of any program that uses the Windows multimedia API.


I'm using a .def file with an EXPORTS section, and I've also got all the 
relevant functions marked as 'export' in the code.


What I'm finding is, if I remove the 'export' attribute but keep the 
EXPORTS section, it all still works; but if I keep the 'export' 
attribute and remove the EXPORTS section, then it doesn't work.


Just wondering why this is, and whether the 'export' attribute is 
actually programmed to do anything at the moment :)


Thanks,

Ben :)


windows 64bit variadic functions

2013-02-09 Thread Benjamin Thaut
I have this small test program, it will crash when compiled on x64 
windows, but it works fine on 32bit windows. What am I doing wrong?


import core.vararg;
import std.stdio;

void print(string fmt, ...)
{
  auto arg = va_arg!(const(char)[])(_argptr);
  writefln(fmt ~ arg);
}

void main(string[] args)
{
  print("+++","---");
}

Kind Regards
Benjamin Thaut


Re: best idiom for insert if not present; else use value in assoc array

2013-02-09 Thread Dan
On Saturday, 9 February 2013 at 00:54:58 UTC, Andrej Mitrovic 
wrote:

Feel free to file an enhancement request.


http://d.puremagic.com/issues/show_bug.cgi?id=9491

Hopefully that is clear.


Re: How to read fastly files ( I/O operation)

2013-02-09 Thread bioinfornatics

some idea such as letter counting:
rename identifier
trimming sequence from quality value to cutoff
convert to a binary format
more idea later


Re: search of a workaround

2013-02-09 Thread bearophile

(I'll ask to remove one limitation):


http://d.puremagic.com/issues/show_bug.cgi?id=9489



typeof(scoped!Foo(1))[10] foos; // Not initialized.

foreach (i, ref f; foos)
// f = new Foo(i * 10);
// f = scoped!Foo(i * 10);
f.x = i * 10;


What's the right/good way to initialize a scoped class instance? 
Currently this doesn't work:



import std.typecons;

class Foo {
int x;
this(int x_) { initialize(x_); }
void initialize(int x_) {
this.x = x_;
}
}

void main() {
typeof(scoped!Foo(1))[10] foos;
foreach (i, ref f; foos)
f.initialize(i * 10);
}


Bye,
bearophile


Re: search of a workaround

2013-02-09 Thread bearophile

monarch_dodra:


I mean structs you can pack together in an array, eg:

S[10] s10; //10 structs S

You can't do that with classes. Sure, you can put 10 class 
*references* in an array, but the *instances* themselves need 
to be individually allocated.


This kind of works, with some limitations (I'll ask to remove one 
limitation):



import std.stdio, std.conv, std.typecons;

class Foo {
int x;
this(int x_) { this.x = x_; }
override string toString() { return text(x); }
}

void main() {
// scoped!Foo[10] foos;
typeof(scoped!Foo(1))[10] foos; // Not initialized.

foreach (i, ref f; foos)
// f = new Foo(i * 10);
// f = scoped!Foo(i * 10);
f.x = i * 10;

// writeln(foos);
foreach (ref f; foos)
writeln(f.x);
}


Bye,
bearophile


Re: search of a workaround

2013-02-09 Thread monarch_dodra

On Saturday, 9 February 2013 at 12:10:57 UTC, Namespace wrote:


In particular, you can't pack classes in a table, for example.

I don't understand.


I mean structs you can pack together in an array, eg:

S[10] s10; //10 structs S

You can't do that with classes. Sure, you can put 10 class 
*references* in an array, but the *instances* themselves need to 
be individually allocated.


This can be a problem if you want to represent an image, for 
example, and "S" is an RGB quad.


Re: search of a workaround

2013-02-09 Thread Namespace
I'm not sure what "in spite of" is. No offense, but you seem to 
be the only person having problems with this.
Maybe I'm the only one who uses so many structs. That happens 
when you have strong C++ background. I miss '&'. :D


And the workarounds are plenty: My proposal, templatizing for 
auto ref, pass by value...
I don't know. Templatizing just for auto ref seems a bad Idea for 
me.

But pass by value for small structs shouldn't be much overhead.


In particular, you can't pack classes in a table, for example.

I don't understand.

That said, if you pass everything by value, and make things < 
16 bytes structs, and > 16 bytes classes, you should get a good 
deal.

Yes, that sounds good.


Re: search of a workaround

2013-02-09 Thread monarch_dodra

On Saturday, 9 February 2013 at 10:05:21 UTC, Namespace wrote:

I that directed at me? I'm not sure how it relates?


Yes, that's a question for you. Why would you take in spite of 
everything still structs rather than classes?


I'm not sure what "in spite of" is. No offense, but you seem to 
be the only person having problems with this.


And the workarounds are plenty: My proposal, templatizing for 
auto ref, pass by value...


As for classes, they're fine if the object in question is 
somewhat big, but it's not a solution for tiny struct, such as 
color info or whatnot. In particular, you can't pack classes in a 
table, for example.


That said, if you pass everything by value, and make things < 16 
bytes structs, and > 16 bytes classes, you should get a good deal.


Re: search of a workaround

2013-02-09 Thread Namespace

I that directed at me? I'm not sure how it relates?


Yes, that's a question for you. Why would you take in spite of 
everything still structs rather than classes?


Re: search of a workaround

2013-02-09 Thread monarch_dodra

On Friday, 8 February 2013 at 23:45:09 UTC, Namespace wrote:

Thank you.
But it looks ugly, and I dislike the use of such constructs for 
such simple feature.

But cool thing. :D I like the meta stuff of D. :)

Other favorites?


One of the advantages of using this approach though is that it 
emulates auto ref. That means that if and when they get 
implemented for non-temlates, you'll need minimal code change to 
exploit it.


Also, instead of using the mixin, (indeed, kind of ugly), you 
could just print out the "rvalue!foo" in a unittest block, and 
copy paste it. It's boilerplate code, but copy paste is at least 
a bit safer than hand writen. And more convenient.


On Friday, 8 February 2013 at 23:49:01 UTC, Namespace wrote:

BTW: What's stopping you from using a class instead of a struct?


I that directed at me? I'm not sure how it relates?