Re: Abstractioning away main/winMain

2015-09-05 Thread anonymous via Digitalmars-d-learn
On Saturday 05 September 2015 07:52, anonymous wrote:

> This doesn't work because delegates and static initialization don't go
> together. You can use a static constructor:
> 
> 
> const Application MyApp;
> static this()
> {
> Application.New({import std.stdio; std.stdio.writeln("MY APP IS
> COOL");

Should be: MyApp = Application.New({...});

> });
> }
> 



Re: spawn X different workers & wait for results from all of them

2015-09-05 Thread Robert M. Münch via Digitalmars-d-learn

On 2015-09-04 17:32:48 +, Justin Whear said:


How would receive know?


Well, it could be pretty simple. At the moment:

receive(
int ...,
long ...,
myStruct ...
)

Will wait for one out of the three. So it's an OR.

reveive_all(
int ...,
long ...,
myStruct ...
)

would finish if every type was returned once (maybe at least once). 
With this I would have an AND combination.



If you're using std.concurrency, the receiving
function needs to encode doneness, e.g.

const numJobs = 4;
foreach (_; 0 .. numJobs)
receive(...);


That's what you can do if the type is the same. Or just put four 
receives sequentially in the code. The not so nice side effect is, that 
you define an order for processing but the tasks might finish in an 
other order. So, possible work is blocked.



Or you could use std.parallelism:

foreach (pieceOfWork; parallel(listOfWork))
doIt(pieceOfWork);


My "pieceOfWork" is not the same. So I don't have the case: Do 4 time 
this 1thing. Instead, do 1 time these 4 things.


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Interfacing Chromium & SQLite

2015-09-05 Thread Mike McKee via Digitalmars-d-learn
On a Mac (Yosemite version), how would I create a window in D, 
embed Chromium, use D to show a local SQLite test database (id, 
firstname, lastname) inside Chromium, and let someone have a 
small search form to do like a "full name" search that goes back 
to D to query the database again?




Re: Superfluous code in switch statement

2015-09-05 Thread Paul via Digitalmars-d-learn

On Friday, 4 September 2015 at 21:20:11 UTC, Timon Gehr wrote:

On 09/04/2015 11:12 PM, anonymous wrote:

On Friday 04 September 2015 23:04, Timon Gehr wrote:


DMD never warns about dead code.


It warns here:


import std.stdio;
void main()
{
 return;
 writeln("hi"); /* Warning: statement is not reachable */
}




You are right, it does. Then I suppose there is no reason why 
it shouldn't warn in the switch case.


I see, thanks.


static array is no range?

2015-09-05 Thread Sebastiaan Koppe via Digitalmars-d-learn

```
import std.algorithm;
char[1024] buffer;
buffer.find("LOCATION: "); // get error about how all the 
different versions of find don't match

```

```
import std.algorithm;
char[1024] buffer;
buffer[0..$].find("LOCATION: "); // works as expected
```

Before trying the slice I manually pragma(msg) all the template 
constraints to see why it was failing. Apparently a static array 
is not a ForwardRange. Now, there is probably a good reason for 
that, that is not what I want to discuss.


The point is that it is rather hard to find out what went wrong.

What I would like the compiler to emit is this: `Error: buffer is 
not a ForwardRange`. But I know that wouldn't be so easy.


At least the compiler shouldn't show me candidates with 
non-matching arguments length (e.g. 
`std.algorithm.searching.find(alias pred, InputRange)(InputRange 
haystack) if (isInputRange!InputRange)`)




Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn

On Saturday, 5 September 2015 at 18:57:52 UTC, deed wrote:

On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:

Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.


What exactly is not compiling?


the last codesample on the bottom. I think because of the old D? 
Index for the last array.length-1 is now $-1. But also I get


Error: undefined identifier 'file'

for the read line. But even when I fixed those errors the strings 
I got were with those quotation marks and backslashes. However, 
with your help I could solve it now.


I moved to the next problem and wrote the program for it

import std.stdio, std.algorithm, std.array;

bool abundant(int n){

int[] a;

foreach(i;1..n)
if(!(n%i))
a~=i;
auto sum = reduce!((a,b)=>a+b)(0,a);

return sum>n;
}



void main(){

long sum;
int[] arr;
int[28123] mark;

foreach(i;1..28124)
if(abundant(i))
arr~=i;

foreach(i;arr)
foreach(j;arr){

if(i+j>28123)
break;
mark[i+j-1] = 1;
}
for(auto i = 0;i

Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn

On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:

Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.


What exactly is not compiling?


Re: Windows Resources

2015-09-05 Thread BBasile via Digitalmars-d-learn

On Saturday, 5 September 2015 at 19:06:15 UTC, Prudence wrote:

[...]


Check this file 
https://github.com/D-Programming-Language/dmd/blob/master/samples/winsamp.d ,it's distributed with your D setup.


Create a delegate function

2015-09-05 Thread Prudence via Digitalmars-d-learn
I have code setup in such a way that I call a user defined 
function, e.g.,


void myFunc(Data d)
{

}

myFunc has to be passed to the main code using something like

void SetFunc(void function(Data) func) { ... func(myData); }

What I would like to do is, instead of having to pass data to 
myFunc(and use the type Data in all the function types), is to 
sort of create a delegate:


what I want to do:

void myFunc()
{
  this.d;  // Ok, because somehow this = Data;
}

then, of course,

void SetFunc(void delegate() func) { func.context = myData; 
func(); }





void delegate() dg =
{
auto t = this;
return;
};

doesn't even work because this is not defined.


My guess this is impossible without compiler support.

effectively though, I don't see why we can't use this(because 
myFunc is being executed in a context, I simply want to set it to 
the right one so that the user can take advantage of it... 
instead of having to pass an argument instead.



Any ideas how to do this? It seems we can't actually create 
"delegate objects" but only delegate pointers? (simply because of 
the restrictions the compiler places on *this*. (can't be used 
outside of a context, even though we can always guarantee it is 
in a context)


How bout a new syntax for such concepts?

void delegate!T(...) dg
{

}

// identical to

void dg(T this, ...)
{

}


Hence, to call dg, we have to pass it a "this" object... hence it 
has a context. They can be called just like functions. dg(myData, 
...);





Windows Resources

2015-09-05 Thread Prudence via Digitalmars-d-learn
I'm trying to create a win32 window. I coped the code pretty much 
directly from msdn:


MSG msg;
BOOL bRet;
WNDCLASS wc;

// Register the window class for the main window.

if (!hPrevInstance)
{
wc.style = 0;
wc.lpfnWndProc = cast(WNDPROC)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
//wc.hIcon = LoadIcon(cast(HINSTANCE) NULL, 
IDI_APPLICATION);
//wc.hCursor = LoadCursor(cast(HINSTANCE) NULL, 
IDC_ARROW);
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName =  "MainMenu";
wc.lpszClassName = "MainWndClass";

if (!RegisterClass())
return FALSE;
}

// Create the main window.
hwndMain = CreateWindow("MainWndClass", "Sample",

WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, 
CW_USEDEFAULT, cast(HWND) NULL,
cast(HMENU) 
NULL, hInstance, cast(LPVOID) NULL);
if (!hwndMain)
{
auto x = GetLastError();
return FALSE;
}


x is 1812 = ERROR_RESOURCE_DATA_NOT_FOUND

That's about as far as I can get. (what resource data? Where I do 
put it? How, who, when?)





Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn

On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote:

On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:

 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")


should be

 .map!(s => chomp(s, "\""))
 .map!(s => chompPrefix(s, "\""))


Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.


Re: Interfacing Chromium & SQLite

2015-09-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 5 September 2015 at 13:32:04 UTC, Mike McKee wrote:
Note: I'm not asking for source code examples, just wondering 
what technologies I would have to use to connect these pieces 
together?


I would basically look up how you'd do it in C and follow the 
same steps in D. I haven't used Chrominum though so I don't know 
more than that...


Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn

On Saturday, 5 September 2015 at 12:41:37 UTC, Namal wrote:
Thx guys. Now I try out the split function. I read the file as 
a single string?


auto arr = split(cast(string)read(filename),",");

where the file has "A", "B", "C"

and I get the output ["\"A\"", " \"B\"", " \"C\"\n"]

I can understand that read functions reads the endl but what 
does it with the quotation marks? how can I modify read so I 
get just ["A", "B", "C"]


'\' is the escape character and is used to disambiguate start or 
end of string (") and a quotation mark within the string (\"), 
the same way as "\n" means newline and not '\' 'n', which would 
have been "\\n".


So what you have is [`"A"`, ` "B"`, ` "C"\n`], if you use ` for 
start\stop of string. You say you want ["A", "B", "C"], so you 
need to remove whitespace. You can do that with std.string.strip. 
Assuming you also want to remove the quotation marks present in 
the file, one solution is to use std.string.chomp and 
std.string.chompPrefix, for example:


string s = cast(string) read(filename);
s.split(",")
 .map!strip
 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")
 .writeln
 ;


Re: spawn X different workers & wait for results from all of them

2015-09-05 Thread thedeemon via Digitalmars-d-learn
On Thursday, 3 September 2015 at 16:50:21 UTC, Robert M. Münch 
wrote:

Hi, I'm not sure how to best implement the following:

1. I have 4 different tasks to do.
2. All can run in parallel
3. Every task will return some result that I need

Now how to best do it?


I think the Task and taskPool from std.parallelism are a good fit.
Something like:

auto task1 = task!fun1(params1);
auto task2 = task!fun2(params2);
auto task3 = task!fun3(params3);
auto task4 = task!fun4(params4);

taskPool.put(task1);
taskPool.put(task2);
taskPool.put(task3);
taskPool.put(task4);

auto res1 = task1.workForce();
auto res2 = task2.workForce();
auto res3 = task3.workForce();
auto res4 = task4.workForce();

//here we have all the results, they were calculated in parallel



Re: spawn X different workers & wait for results from all of them

2015-09-05 Thread Robert M. Münch via Digitalmars-d-learn

On 2015-09-05 15:44:02 +, thedeemon said:


I think the Task and taskPool from std.parallelism are a good fit.
Something like:

auto task1 = task!fun1(params1);
auto task2 = task!fun2(params2);
auto task3 = task!fun3(params3);
auto task4 = task!fun4(params4);

taskPool.put(task1);
taskPool.put(task2);
taskPool.put(task3);
taskPool.put(task4);

auto res1 = task1.workForce();
auto res2 = task2.workForce();
auto res3 = task3.workForce();
auto res4 = task4.workForce();

//here we have all the results, they were calculated in parallel


Thanks, that looks pretty good. Going to x-check how this could be used.

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn
On Friday, 4 September 2015 at 12:09:19 UTC, Edwin van Leeuwen 
wrote:
On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen 
wrote:

On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote:


import std.algorithm, std.range, std.array, std.string, 
std.stdio,

std.conv;

int[] arr1 = [1, 2, 30];
//arr1.max.writeln; // Doesn't work, as you say
arr1.reduce!max.writeln;// This does. Prints 30.


Again using reduce is the functional way to do it. The above 
basically boils down to:


int[] arr1 = [1, 2, 30];
int maxElement = arr1[1];
foreach( element; arr1[2..$] ) //2..$ is short hand for second 
till last ($) element

{
  maxElement = max( maxElement, element );
}
writeln( maxElement );


Sorry been using too much R, so my indexes are off by 1:

int[] arr1 = [1, 2, 30];
int maxElement = arr1[0];
foreach( element; arr1[1..$] ) //1..$ is short hand for second 
till last ($) element

{
  maxElement = max( maxElement, element );
}
writeln( maxElement );



Thx guys. Now I try out the split function. I read the file as a 
single string?


auto arr = split(cast(string)read(filename),",");

where the file has "A", "B", "C"

and I get the output ["\"A\"", " \"B\"", " \"C\"\n"]

I can understand that read functions reads the endl but what does 
it with the quotation marks? how can I modify read so I get just 
["A", "B", "C"]






Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn

On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:

 .map!(s => chomp(s, "\"")
 .map!(s => chompPrefix(s, "\"")


should be

 .map!(s => chomp(s, "\""))
 .map!(s => chompPrefix(s, "\""))


Re: Windows Resources

2015-09-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 5 September 2015 at 19:06:15 UTC, Prudence wrote:
That's about as far as I can get. (what resource data? Where I 
do put it? How, who, when?)


Resource data in Windows is data compiled into your exe. It is 
stuff like icons, menus, or other arbitrary stuff you attach.


However, the code you posted doesn't access any of that so it 
shouldn't be an issue here... what import did you use to access 
the windows api? `import core.sys.windows.windows;` or something 
else? Also, are you building 32 bit (the default) or 64 bit?


Re: static array is no range?

2015-09-05 Thread cym13 via Digitalmars-d-learn
On Saturday, 5 September 2015 at 11:12:17 UTC, Sebastiaan Koppe 
wrote:

```
import std.algorithm;
char[1024] buffer;
buffer.find("LOCATION: "); // get error about how all the 
different versions of find don't match

```

```
import std.algorithm;
char[1024] buffer;
buffer[0..$].find("LOCATION: "); // works as expected
```


You can do instead:
buffer[].find("LOCATION: ");

Before trying the slice I manually pragma(msg) all the template 
constraints to see why it was failing. Apparently a static 
array is not a ForwardRange. Now, there is probably a good 
reason for that, that is not what I want to discuss.


The point is that it is rather hard to find out what went wrong.

What I would like the compiler to emit is this: `Error: buffer 
is not a ForwardRange`. But I know that wouldn't be so easy.


At least the compiler shouldn't show me candidates with 
non-matching arguments length (e.g. 
`std.algorithm.searching.find(alias pred, 
InputRange)(InputRange haystack) if (isInputRange!InputRange)`)


Yes, static arrays aren't ranges. The main reason is that static 
arrays are value type (ie: you copy them arround when passing 
them to functions which usually has a huge cost) where ranges are 
reference type (no copy, lighter, not always better as it makes 
optimisation more complicated).


The standard library is designed arround ranges to make sure that 
you are not copying 1024-bytes long structures arround by 
accident: you'd have to do that explicitely. As a consequence, 
you must generally slice arrays when passing them to phobos 
functions (not always true but a good rule of thumb).


That said, if you want to benefit from array-specific 
optimisations such as loop-unrolling you are generally better of 
using a good old foreach and implementing the logic yourself. 
Yes, it is sad, I agree.


Re: Windows Resources

2015-09-05 Thread Rikki Cattermole via Digitalmars-d-learn

This may interest you:
https://github.com/Devisualization/window/blob/master/platforms/win32/devisualization/window/window.d


Re: Windows Resources

2015-09-05 Thread Prudence via Digitalmars-d-learn

On Sunday, 6 September 2015 at 00:29:13 UTC, Adam D. Ruppe wrote:

On Saturday, 5 September 2015 at 19:06:15 UTC, Prudence wrote:
That's about as far as I can get. (what resource data? Where I 
do put it? How, who, when?)


Resource data in Windows is data compiled into your exe. It is 
stuff like icons, menus, or other arbitrary stuff you attach.


However, the code you posted doesn't access any of that so it 
shouldn't be an issue here... what import did you use to access 
the windows api? `import core.sys.windows.windows;` or 
something else? Also, are you building 32 bit (the default) or 
64 bit?


32-bit and I'm using the latest win32 wrappers distributed on 
github from someone. (suppose to be more complete than 
core.sys.windows)


Obviously the issue is that I'm not using any resources yet it is 
giving me such an error. (again, I pretty much copied the code 
directly from MSDN)






Re: static array is no range?

2015-09-05 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Sunday, 6 September 2015 at 00:25:10 UTC, cym13 wrote:
Yes, static arrays aren't ranges. The main reason is that 
static arrays are value type (ie: you copy them arround when 
passing them to functions which usually has a huge cost) where 
ranges are reference type (no copy, lighter, not always better 
as it makes optimisation more complicated).


The standard library is designed arround ranges to make sure 
that you are not copying 1024-bytes long structures arround by 
accident: you'd have to do that explicitely. As a consequence, 
you must generally slice arrays when passing them to phobos 
functions (not always true but a good rule of thumb).


That actually makes a lot of sense.

That said, if you want to benefit from array-specific 
optimisations such as loop-unrolling you are generally better 
of using a good old foreach and implementing the logic 
yourself. Yes, it is sad, I agree.


While I like speed, 95% of my applications can be 4x as slow, and 
no-one would give a damn. Plus, I have only limited time so I try 
to be efficient by writing as little code as possible (while 
still getting work done.)


What I was trying to say is that endorsed idiomatic D code (UFCS 
combined with function overloading+constraints), produces 
terrible error messages. Which means every newcomer sees that 
awful stuff and decides to implement said algorithm himself. That 
is not productivity.


This is arguably the poorest and most neglected aspect of D.

Without messing up internals the best I can think of is this:

```
auto forwardRange(alias R)()
{
import std.traits;
import std.range;
	static if(!isForwardRange!(typeof(R))) static 
assert(0,"Nonono... "~__traits(identifier,R)~" not a 
ForwardRange");

return R;
}

unittest
{
char[1024] buffer;
import std.algorithm;
forwardRange!buffer.find("LOCATION: ");
}
```

Which is ugly as hell, and probably even worse than the current 
state.


Re: Create a delegate function

2015-09-05 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 5 September 2015 at 18:00:53 UTC, Prudence wrote:
I have code setup in such a way that I call a user defined 
function, e.g.,


void myFunc(Data d)
{

}

myFunc has to be passed to the main code using something like



You can do that if and only if the this is the last argument to 
the function and is a pointer.


Even then, you're kinda hacking it, but with casts you can make 
it work:


struct Test {
int a;
}
void foo(Test* _this) {
_this.a = 10;
}
void main() {
auto _this = new Test();
void delegate() dg;
dg.funcptr = cast(typeof(dg.funcptr)) 
dg.ptr = _this;
dg();
import std.stdio;
writeln(_this.a);
}



You could probably put that in a template that does better type 
checking.



But the idea is that a delegate is simply a pair of function 
pointer and context pointer. When you call it, it automatically 
adds the context pointer as the last hidden argument to the 
function.


Knowing how it works at the low level, we can use some casts to 
get the compiler to trust us and make it work.



Similarly with arguments:


struct Test {
int a;
}
void foo(int a, Test* _this) {
_this.a = 10 + a;
}
void main() {
auto _this = new Test();
void delegate(int) dg;
dg.funcptr = cast(typeof(dg.funcptr)) 
dg.ptr = _this;
dg(10);
import std.stdio;
writeln(_this.a);
}



To type check this, you could probably use 
ParameterTypeTuple!foo[0 .. $-1] and check for match there on the 
typeof(dg.funcptr). And check return value and that the last 
argument is indeed a pointer of the type you are giving.




Re: Create a delegate function

2015-09-05 Thread BBasile via Digitalmars-d-learn

On Saturday, 5 September 2015 at 18:00:53 UTC, Prudence wrote:
I have code setup in such a way that I call a user defined 
function, e.g.,


void myFunc(Data d)
{

}

myFunc has to be passed to the main code using something like

void SetFunc(void function(Data) func) { ... func(myData); }

What I would like to do is, instead of having to pass data to 
myFunc(and use the type Data in all the function types), is to 
sort of create a delegate:


what I want to do:

void myFunc()
{
  this.d;  // Ok, because somehow this = Data;
}

then, of course,

void SetFunc(void delegate() func) { func.context = myData; 
func(); }





void delegate() dg =
{
auto t = this;
return;
};

doesn't even work because this is not defined.


My guess this is impossible without compiler support.

effectively though, I don't see why we can't use this(because 
myFunc is being executed in a context, I simply want to set it 
to the right one so that the user can take advantage of it... 
instead of having to pass an argument instead.



Any ideas how to do this? It seems we can't actually create 
"delegate objects" but only delegate pointers? (simply because 
of the restrictions the compiler places on *this*. (can't be 
used outside of a context, even though we can always guarantee 
it is in a context)


How bout a new syntax for such concepts?

void delegate!T(...) dg
{

}

// identical to

void dg(T this, ...)
{

}


Hence, to call dg, we have to pass it a "this" object... hence 
it has a context. They can be called just like functions. 
dg(myData, ...);


Wow, it's hard to get what you mean. It's a bit confuse.
But, IIUC you want to link the parameter value to the delegate 
type ?
If so then it's time for you to lean 'std.typecons.Tuple' and 
'std.typecons.tuple'.


For example, is this what you meant ?

---
module runnable;

import std.stdio;
import std.typecons;
import std.traits;

alias Fun = void function(int);
alias FunAndData = Tuple!(Fun, ParameterTypeTuple!Fun);

struct MainCode
{
int myData;
void setFunc(FunAndData funAndData)
{
funAndData[0](funAndData[1..$]);
}
}

void test(int param)
{
writeln(param);
}

void main(string[] args)
{
MainCode mainCode;
mainCode.setFunc(tuple(,46));
}
---