Re: PHP to D Conversion

2019-10-21 Thread zoujiaqing via Digitalmars-d-learn

On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:

Hi All,

  Request your help in converting a PHP code to D equivalent 
code




You can get one connection object and close it :)

```D

import hunt.database;

class AvmTest {

private Database _db;

this() {
_db = new 
Database("mysql://testusr:x...@test.srv.com:3910/test");

}

string[string][] getHostname() {

string[string][] data;

auto conn = _db.getConnection();


foreach(row; conn.query("SELECT host_name FROM 
hosts_coll"))

{
string[string] host;
host["HostName"] = row["host_name"];

data ~= host;
}

conn.close();

return data;
}
}

```



Re: PHP to D Conversion

2019-10-21 Thread zoujiaqing via Digitalmars-d-learn

On Monday, 21 October 2019 at 15:36:25 UTC, Andre Pany wrote:

On Monday, 21 October 2019 at 15:29:33 UTC, zoujiaqing wrote:

On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:

[...]



import hunt.database;

class avmtest {

private Database db;

this() {
		db = new 
Database("mysql://testusr:x...@test.srv.com:3910/test");

}

string[string][] getHostname() {

string[string] data;


foreach(row; db.query("SELECT host_name FROM hosts_coll"))
{
string[string] host;
host["HostName"] = row["host_name"];
data ~= host;
}

return data;
}
}


Is this database library compatible with the fiber programming 
model of vibe-d?


Kind regards
Andre


Yes.
There are no conflicts.


Re: exceptions and optimization

2019-10-21 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 21 October 2019 at 21:09:32 UTC, Peter Jacobs wrote:
On Monday, 21 October 2019 at 20:37:32 UTC, Nicholas Wilson 
wrote:


What kind of conditions are you wanting to throw exception on? 
infinities, NaNs, ill conditioning, something else?


As always the best way to check is to mark the function of 
interest, nothrow take a look at the disassembly and compare 
to without nothrow. You may also want to look to the 
optimisation summary that I _think_ you can get LDC to 
generate.


Our methods take a few short cuts and occasionally step over a 
stability boundary, so I guess that it may look like 
ill-conditioning.


The reason I asked that is to see what sort of action you take 
because there may be better architectures that you can use to 
handle that kind of change.


For example, you are integrating with some scheme and hit a 
region of high curvature and need to rollback and change scheme 
(to either a less coarse time step or better integrator). In 
which case it may be best to take a page out of databases and use 
a change-commit kind of approach to short circuit through your 
calculations and if at the end of your update loop a flag is set 
that says this update is invalid then retry with another scheme.



Thank you for the explanation and suggestions.




Re: exceptions and optimization

2019-10-21 Thread Peter Jacobs via Digitalmars-d-learn

On Monday, 21 October 2019 at 20:37:32 UTC, Nicholas Wilson wrote:


What kind of conditions are you wanting to throw exception on? 
infinities, NaNs, ill conditioning, something else?


As always the best way to check is to mark the function of 
interest, nothrow take a look at the disassembly and compare to 
without nothrow. You may also want to look to the optimisation 
summary that I _think_ you can get LDC to generate.


Our methods take a few short cuts and occasionally step over a 
stability boundary, so I guess that it may look like 
ill-conditioning.


Thank you for the explanation and suggestions.



Re: contains method on immutable sorted array

2019-10-21 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 21 October 2019 at 10:14:54 UTC, Andrey wrote:

Hello,
I have got a global constant immutable array:
immutable globalvalues = sort(cast(wstring[])["й", "ц", "ук", 
"н"]);


Somewhere in program I want to check an existance:

globalvalues.contains("ук"w).writeln;


But get an error:
Error: template std.range.SortedRange!(wstring[], "a < 
b").SortedRange.contains cannot deduce function from argument 
types !()(wstring) immutable, candidates are:

/dlang/ldc-1.17.0/bin/../import/std/range/package.d(10993):
std.range.SortedRange!(wstring[], "a < 
b").SortedRange.contains(V)(V value) if 
(isRandomAccessRange!Range)


Why I can't check?


pragma(msg,typeof(globalvalues));

gives immutable(SortedRange!(wstring[], "a < b"))

and

(cast(SortedRange!(wstring[], "a < 
b"))globalvalues).contains("ук"w).writeln;


works, so I guess contains doesn't work with immutable?
If you can do some more research into this and confirm it then, 
please file a bug report.


Re: exceptions and optimization

2019-10-21 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 21 October 2019 at 20:12:19 UTC, Peter Jacobs wrote:
Toward the end of Walter's recent talk, D at 20, he says 
something to the effect that optimizations are disabled when 
exceptions can be thrown.  We have a compressible flow solver 
in which it is very convenient to be able to throw an exception 
from deep within the code and catch it at a relatively high 
level where we can partially recover and continue the 
calculation.  Because our calculations can run for days across 
hundreds of processors, we also care about letting the 
optimizer do its best. In what parts of our program would the 
optimizer be disabled because of the presence of the exception 
and its handling code?  How do we tell?


It really comes into effect if you use:

a) structs with destructors
b) scope(exit) and scope(failure)

as these need to be run, regardless of the return mode of the 
function i.e. normally or via an exception.


Also if the function is nothrow (possibly inferred because its a 
template, n.b. also that nothrow function can throw Errors as 
these are considered terminal) then you should also be fine. The 
effect of exceptions on optimisation effect logic heavy code a 
lot more that math heavy code.


What kind of conditions are you wanting to throw exception on? 
infinities, NaNs, ill conditioning, something else?


As always the best way to check is to mark the function of 
interest, nothrow take a look at the disassembly and compare to 
without nothrow. You may also want to look to the optimisation 
summary that I _think_ you can get LDC to generate.




exceptions and optimization

2019-10-21 Thread Peter Jacobs via Digitalmars-d-learn
Toward the end of Walter's recent talk, D at 20, he says 
something to the effect that optimizations are disabled when 
exceptions can be thrown.  We have a compressible flow solver in 
which it is very convenient to be able to throw an exception from 
deep within the code and catch it at a relatively high level 
where we can partially recover and continue the calculation.  
Because our calculations can run for days across hundreds of 
processors, we also care about letting the optimizer do its best. 
In what parts of our program would the optimizer be disabled 
because of the presence of the exception and its handling code?  
How do we tell?




Re: Small program producing binary with large filesize

2019-10-21 Thread Prokop Hapala via Digitalmars-d-learn
On Wednesday, 1 August 2018 at 15:58:53 UTC, Jacob Shtokolov 
wrote:

On Tuesday, 31 July 2018 at 15:19:19 UTC, Dan Barbarito wrote:

Hi all,

I am starting to write a command line tool.


Hi!

First, Vibe.d will increase your binary size because it 
contains a lot of unnecessary things inside it. So instead of 
using the entire vibe.d library, you may point dub to specific 
vibe.d parts, like `vibe.d:core`, `vibe.d:http` etc.


If you put the whole vibe.d framework as a dub dependency and 
use it like `import vibe;` the things like mongodb drivers, 
email libraries, redis driver etc. will be linked to your 
binary as well, even if you don't need them.


Second, you need to compile your code in the release mode to 
cut all debug information out of it. Also, you can try to use 
the `strip` command line tool to cut all export symbols if your 
binary is an executable file, not a library. This will reduce 
the size. In some cases a lot.


The third thing is already mentioned: by default, the DMD 
compiler builds and links the code statically. In other words, 
your binary contains parts of the DRuntime and the Phobos 
libraries (those parts that you've used in your program).


This thing helps to distribute compiled binaries without 
external dependencies (except libc and libpthread), because 
your binary is already contains all needed stuff.


If you're not happy with it, you can try to use the LDC 
compiler, which uses the dynamic linking by default. Your 
binary will be really tiny, but in order to execute it, you'll 
need to have the libdruntime.so and libphobos.so installed in 
your system. This may add additional issues if you plan to 
distribute your program.


Sadly, we still don't have the D runtime libraries installed in 
all popular OSes. Currently only the libc has this privilege 


I hope the situation will change in the future.



Hi, I was trying with ldc (source ~/dlang/ldc-1.17.0/activate) 
and the size of binary is even worse


dub with dmd 2.89 MB
dub with ldc 3.50 MB

It is very simple program but use derelict-gl3 and derelict-sdl2

I want to link them all dynamically (I don't plan to distribute 
binary any time soon)


What exactly should I specify to make it link dynamcially and 
produce as small binary as possible (without increasing 
compilation time) ?




Re: ... use of ... is hidden by ...; use alias ... to introduce base class overload set ??

2019-10-21 Thread Robert M. Münch via Digitalmars-d-learn

On 2019-10-21 07:04:33 +, John Chapman said:


This should work:

class FilterSubject : SubjectObject!message {
   alias subscribe = typeof(super).subscribe;
   Disposable subscribe(myWidget observer){...}
}


This now gives:

rx_filter_subject.d(66,23): Error: 
rx_filter_subject.FilterSubject.subscribe called with argument types 
(myWidget) matches both:


/Users/robby/.dub/packages/rx-0.13.0/rx/source/rx/subject.d(72,16):
rx.subject.SubjectObject!(message).SubjectObject.subscribe!(myWidget).subscribe(myWidget 
observer)


and:

rx_filter_subject.d(47,14): 
rx_filter_subject.FilterSubject.subscribe(myWidget observer)


So, now there is an ambiguty.

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



Re: PHP to D Conversion

2019-10-21 Thread Andre Pany via Digitalmars-d-learn

On Monday, 21 October 2019 at 15:29:33 UTC, zoujiaqing wrote:

On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:

[...]



import hunt.database;

class avmtest {

private Database db;

this() {
		db = new 
Database("mysql://testusr:x...@test.srv.com:3910/test");

}

string[string][] getHostname() {

string[string] data;


foreach(row; db.query("SELECT host_name FROM hosts_coll"))
{
string[string] host;
host["HostName"] = row["host_name"];
data ~= host;
}

return data;
}
}


Is this database library compatible with the fiber programming 
model of vibe-d?


Kind regards
Andre



Re: PHP to D Conversion

2019-10-21 Thread zoujiaqing via Digitalmars-d-learn

On Friday, 18 October 2019 at 06:22:33 UTC, Vino wrote:

Hi All,

  Request your help in converting a PHP code to D equivalent 
code


PHP Code:
class avmtest {
private $con;

function __construct() {
global $config;
$this->con = new mysqli(test.srv.com:3910, 
testusr, #, test);
if($this->con->connect_errno) { die("Connection 
Failed.\n"); }

}

function getHostname() {
$qdata = $this->con->prepare("SELECT host_name 
FROM hosts_coll");

$qdata->execute();
$qdata->bind_result($host);
while($qdata->fetch()) {
$data[] = array("HostName" => $host);
}
$sdata->close();
return $data;
}
}

D Code:
module avm.test;

import mysql;
import std.array : array;
import std.conv;
import std.variant;

class avmtest
{
private conn;

auto avmconnect()
{
	auto connectionStr = 
"host=test.srv.com;port=3910;user=testusr;pwd=#;db=test";

Connection conn = new Connection(connectionStr);
scope(exit) conn.close();
}

auto getHostname()
{
	  ResultRange qdata = conn.query("SELECT host_name FROM 
`hosts_coll`");

  Row row = qdata.front;
  Variant h = row[0];
  qdata.close();
  return h.to!string;
}
}

Error: Error: no identifier for declarator conn

From,
Vino.B



import hunt.database;

class avmtest {

private Database db;

this() {
		db = new 
Database("mysql://testusr:x...@test.srv.com:3910/test");

}

string[string][] getHostname() {

string[string] data;


foreach(row; db.query("SELECT host_name FROM hosts_coll"))
{
string[string] host;
host["HostName"] = row["host_name"];
data ~= host;
}

return data;
}
}



Re: PHP to D Conversion

2019-10-21 Thread Andre Pany via Digitalmars-d-learn

On Monday, 21 October 2019 at 11:36:00 UTC, Vino wrote:
On Saturday, 19 October 2019 at 20:40:36 UTC, Boris Carvajal 
wrote:

[...]


Hi Boris,

[...]


Hi,

Where do you call avmconnect?
Is this.conn null when connection fails?
What happens if the query does not contains rows?

Kind regards
Andre


Re: PHP to D Conversion

2019-10-21 Thread Vino via Digitalmars-d-learn
On Saturday, 19 October 2019 at 20:40:36 UTC, Boris Carvajal 
wrote:

On Saturday, 19 October 2019 at 19:08:45 UTC, Vino wrote:

On Friday, 18 October 2019 at 14:56:05 UTC, Andre Pany wrote:

On Friday, 18 October 2019 at 09:21:46 UTC, Vino wrote:

On Friday, 18 October 2019 at 09:17:24 UTC, Vino wrote:

[...]


And now getting the error : Program exited with code 
-1073741819


Hi,

Maybe port 8080 is blocked, because there is an instance of 
your application running on the background. Just check by 
changing the port in the source code.


Kind regards
Andre


Hi Andre,

  Tried with different ports still no luck getting this error 
: Program exited with code -1073741819.


From,
Vino.B


Your program is crashing probably because you are dereferencing 
a null/ dangling pointer.
Build your program in debug mode and run it in a debugger, that 
way you will get a backtrace telling you the exactly line of 
the code when it happens.


Hi Boris,

  I tired to build the code in debug mode and ran the program in 
debugger , the debugger is complaining about the line "this.conn 
= new Connection(connectionStr);", not sure what is wrong in the 
below code, hence request your help. Have the checked the 
connection string and the info is correct.


import vibe.vibe;
import mysql;
import std.array : array;
import std.conv;
import std.variant;

class avmtest
{
private Connection conn;
auto avmconnect()
{
  auto connectionStr = 
"host=test.srv.com;port=3910;user=testusr;pwd=#;db=test";

  this.conn = new Connection(connectionStr);
}

auto getHostname()
{
 ResultRange qdata = conn.query("SELECT host_name FROM 
`hosts_coll`");

 Row row = qdata.front;
 Variant h = row[0];
 qdata.close();
 return h.to!string;
   }
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
 auto t = new avmtest();
 res.writeBody(t.getHostname);
}

void main()
{
  auto settings = new HTTPServerSettings;
  settings.port = 8130;
  settings.bindAddresses = ["127.0.0.1"];
  listenHTTP(settings, );
  logInfo("Please open http://127.0.0.1:8130/ in your browser.");
  runApplication();
}

From,
Vino.B


contains method on immutable sorted array

2019-10-21 Thread Andrey via Digitalmars-d-learn

Hello,
I have got a global constant immutable array:
immutable globalvalues = sort(cast(wstring[])["й", "ц", "ук", 
"н"]);


Somewhere in program I want to check an existance:

globalvalues.contains("ук"w).writeln;


But get an error:
Error: template std.range.SortedRange!(wstring[], "a < 
b").SortedRange.contains cannot deduce function from argument 
types !()(wstring) immutable, candidates are:

/dlang/ldc-1.17.0/bin/../import/std/range/package.d(10993):
std.range.SortedRange!(wstring[], "a < 
b").SortedRange.contains(V)(V value) if 
(isRandomAccessRange!Range)


Why I can't check?


SQL Questions Query

2019-10-21 Thread Arjunkumar via Digitalmars-d-learn

Hello Everyone,

I am looking for SQL interview questions list. I have scheduled 
my interviews in the upcoming week,  Recruiters told me they 
might test my SQL knowledge too. What kind of questions should I 
expect?
One is a consultant swe / data scientist and the other is an 
application developer.


Re: Any 3D Game or Engine with examples/demos which just work (compile) out of the box on linux ?

2019-10-21 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Sunday, 20 October 2019 at 22:48:25 UTC, Jonathan Marler wrote:
On Friday, 18 October 2019 at 06:11:37 UTC, Ferhat Kurtulmuş 
wrote:
On Friday, 18 October 2019 at 05:52:19 UTC, Prokop Hapala 
wrote:
Already >1 year I consider to move from C++ to Dlang or to 
Rust in my hobby game development (mostly based on physical 
simulations 
https://github.com/ProkopHapala/SimpleSimulationEngine). I 
probably prefer Dlang because it compiles much faster, and I 
can copy C/C++ code to it without much changes.


[...]


I cannot make any comment for others. But Dagon should work. I 
wrote a very little demo game some time ago 
https://github.com/aferust/dagon-shooter. I didn't try to 
compile and run it on Linux.I think you need to have a 
nuklear.so in your path, since Bindbc loaders try to load


It looks like the maintainer of dagon changed the API a lot. I 
could run my demo game using version 0.10.0. dub.json:


{
"description": "My first project",
"dependencies": {
"dagon": "0.10.0",
"bindbc-soloud": "*"
},
"authors": [
"aferust"
],
"copyright": "Copyright © 2019, aferust",
"license": "Boost",
"name": "dagon-shooter"
}

But texture rendering seems weird for some reason. Obviously, 
dagon-shooter needs some updates according to new dagon API


Re: ... use of ... is hidden by ...; use alias ... to introduce base class overload set ??

2019-10-21 Thread John Chapman via Digitalmars-d-learn

On Sunday, 20 October 2019 at 21:45:35 UTC, Robert M. Münch wrote:

class myWidget : Observer!message {...}

class FilterSubject : SubjectObject!message {
 Disposable subscribe(myWidget observer){...}
}


I tried to add "alias subscribe = SubjectObject.subscribe;" in 
different places, but that didn't help. Nor do I have any how 
that should help...


This should work:

class FilterSubject : SubjectObject!message {
  alias subscribe = typeof(super).subscribe;
  Disposable subscribe(myWidget observer){...}
}