Re: Caching of Template Instantiations

2015-10-17 Thread Marc Schütz via Digitalmars-d-learn

On Saturday, 17 October 2015 at 07:48:39 UTC, Nordlöw wrote:

Does DMD cache template instantiations?


Yes, and it's required by the spec:

"Multiple instantiations of a TemplateDeclaration with the same 
TemplateArgumentList all will refer to the same instantiation."

http://dlang.org/template.html



That is, is it preferred to do, for instance, either

static  if (isIntegral!T && isUnsigned!(T)) {}
else static if (isIntegral!T && isSigned!(T)) {}

or

enum integral = isIntegral!T;
static  if (integral && isUnsigned!(T)) {}
else static if (integral && isSigned!(T)) {}


It *might* make a little difference if you have thousands of 
instantiations, because for each instantiation the compiler needs 
to check whether it has already instantiated the templation with 
that combination of arguments, but I wouldn't worry about it.


Caching of Template Instantiations

2015-10-17 Thread Nordlöw via Digitalmars-d-learn

Does DMD cache template instantiations?

That is, is it preferred to do, for instance, either

static  if (isIntegral!T && isUnsigned!(T)) {}
else static if (isIntegral!T && isSigned!(T)) {}

or

enum integral = isIntegral!T;
static  if (integral && isUnsigned!(T)) {}
else static if (integral && isSigned!(T)) {}



what is wrong with this code??

2015-10-17 Thread steven kladitis via Digitalmars-d-learn
import std.string, std.typecons, std.exception, 
std.algorithm,std.stdio;

import std.traits: hasIndirections;

struct GrowableCircularQueue(T) {
public size_t length;
private size_t first, last;
private T[] A = [T.init];

this(T[] items...) pure nothrow @safe {
foreach (x; items)
push(x);
}

@property bool empty() const pure nothrow @safe @nogc {
return length == 0;
}

@property T front() pure nothrow @safe @nogc {
assert(length != 0);
return A[first];
}

T opIndex(in size_t i) pure nothrow @safe @nogc {
assert(i < length);
return A[(first + i) & (A.length - 1)];
}

void push(T item) pure nothrow @safe {
if (length >= A.length) { // Double the queue.
immutable oldALen = A.length;
A.length *= 2;
if (last < first) {
A[oldALen .. oldALen + last + 1] = A[0 .. last + 
1];

static if (hasIndirections!T)
A[0 .. last + 1] = T.init; // Help for the GC.
last += oldALen;
}
}
last = (last + 1) & (A.length - 1);
A[last] = item;
length++;
}

@property T pop() pure nothrow @safe @nogc {
assert(length != 0);
auto saved = A[first];
static if (hasIndirections!T)
A[first] = T.init; // Help for the GC.
first = (first + 1) & (A.length - 1);
length--;
return saved;
}
}


const struct Board {
private enum El { floor = ' ', wall = '#', goal = '.',
  box = '$', player = '@', boxOnGoal='*' }
private alias CTable = string;
private immutable size_t ncols;
private immutable CTable sData, dData;
private immutable int playerx, playery;

this(in string[] board) immutable
in {
foreach (const row; board) {
assert(row.length == board[0].length,
   "Unequal board rows.");
foreach (immutable c; row)
assert(c.inPattern(" #.$@*"), "Not valid input");
}
} body {
/*static*/ immutable sMap =
[' ':' ', '.':'.', '@':' ', '#':'#', '$':' '];
/*static*/ immutable dMap =
[' ':' ', '.':' ', '@':'@', '#':' ', '$':'*'];
ncols = board[0].length;
writeln("ncols =",ncols);
int plx = 0, ply = 0;
CTable sDataBuild, dDataBuild;

foreach (immutable r, const row; board)
foreach (immutable c, const ch; row) {
sDataBuild ~= sMap[ch];
dDataBuild ~= dMap[ch];
if (ch == El.player) {
plx = c;
ply = r;
}
//   writeln("c ch row ",c," ",ch," ",row);
//   writeln("board =>",board);
}


this.sData = sDataBuild;
this.dData = dDataBuild;
this.playerx = plx;
this.playery = ply;
}

private bool move(in int x, in int y, in int dx,
  in int dy, ref CTable data)
const pure nothrow /*@safe*/ {
if (sData[(y + dy) * ncols + x + dx] == El.wall ||
data[(y + dy) * ncols + x + dx] != El.floor)
return false;

auto data2 = data.dup;
data2[y * ncols + x] = El.floor;
data2[(y + dy) * ncols + x + dx] = El.player;
data = data2.assumeUnique; // Not enforced.
return true;
}

private bool push(in int x, in int y, in int dx,
  in int dy, ref CTable data)
const pure nothrow /*@safe*/ {
if (sData[(y + 2 * dy) * ncols + x + 2 * dx] == El.wall ||
data[(y + 2 * dy) * ncols + x + 2 * dx] != El.floor)
return false;

auto data2 = data.dup;
data2[y * ncols + x] = El.floor;
data2[(y + dy) * ncols + x + dx] = El.player;
data2[(y + 2 * dy) * ncols + x + 2*dx] = El.boxOnGoal;
data = data2.assumeUnique; // Not enforced.
return true;
}

private bool isSolved(in CTable data)
const pure nothrow @safe @nogc {
foreach (immutable i, immutable d; data)
if ((sData[i] == El.goal) != (d == El.boxOnGoal))
return false;
return true;
}

string solve() pure nothrow /*@safe*/ {
bool[immutable CTable] visitedSet = [dData: true];

alias Four = Tuple!(CTable, string, int, int);
GrowableCircularQueue!Four open;
open.push(Four(dData, "", playerx, playery));

static immutable dirs = [tuple( 0, -1, 'u', 'U'),
 tuple( 1,  0, 'r', 'R'),
 tuple( 0,  1, 'd', 'D'),
 tuple(-1,  0, 'l', 'L')];

while (!open.empty) {
//immutable (cur, cSol, x, y) = open.pop;
immutable item = open.pop;
immutable cur = item[0];
immutable cSol = item[1];
immutable x = 

Re: LuaD: creating a flexible data filter system

2015-10-17 Thread Chris via Digitalmars-d-learn

On Saturday, 17 October 2015 at 02:02:16 UTC, Jakob Ovrum wrote:

On Friday, 16 October 2015 at 10:45:52 UTC, Chris wrote:
Later you call the function with the Lua C API like 
"lua_pcall(L, 0, 1, 0);". It's a bit tricky to move things 
around on the Lua stack, but you'll get there! ;)


Or you could use LuaD which doesn't require you to mess around 
with the relatively unproductive, bug-prone C API :)


I've used both, LuaD and DerelictLua + my own smaller D library 
that wraps all these nasty Lua stack operations (e.g. creating, 
accessing and adding to tables). The better I got to know the Lua 
C API while writing my own wrappers, the more I came to 
appreciate LuaD :-) However, LuaD is still 5.1 and I didn't want 
to be stuck with 5.1. So I rolled my own. It's not as 
comprehensive as LuaD but did the trick.


The purpose was to experiment with Lua server pages + vibe.d 
which worked fine. Rikki gave me the idea to compile each page as 
a Lua function into memory for faster execution (etLua style[1]). 
It works fine with both LuaD and my own wrappers. In my own 
version I use the Lua C API directly in some places, though I 
don't know, if that's really a big speedup.


If I set up my own homepage, I'd probably give vibe.d + Lua a 
shot. No more PHP and sh*t like that.


[1] https://github.com/leafo/etlua


Re: LuaD: creating a flexible data filter system

2015-10-17 Thread yawniek via Digitalmars-d-learn

many thanks for the valuable insights.
so far i made a simple prototype with LuaD and classes, works 
nicely for when my niput


what so far is not 100% clear is if there is a way to have a 
parsed
msgpack or json documents being exposed in my lua code in a way 
so it behaves

like a lua object.
Ideally in a RW fashion so that changed then again can be 
processed by D code.




Re: OT: why do people use python when it is slow?

2015-10-17 Thread Laeeth Isharc via Digitalmars-d-learn
On Wednesday, 14 October 2015 at 18:17:29 UTC, Russel Winder 
wrote:
On Wed, 2015-10-14 at 14:48 +, John Colvin via 
Digitalmars-d-learn wrote:

On Wednesday, 14 October 2015 at 14:32:00 UTC, jmh530 wrote:
> On Tuesday, 13 October 2015 at 23:26:14 UTC, Laeeth Isharc 
> wrote:

> > https://www.quora.com/Why-is-Python-so-popular-despite-being-so-s
> > low
> > Andrei suggested posting more widely.
> 
> I was just writing some R code yesterday after playing 
> around with D for a couple weeks. I accomplished more in an 
> afternoon of R coding than I think I had in like a month's 
> worth of playing around with D. The same is true for python.


As someone who uses both D and Python every day, I find that - 
once you are proficient in both - initial productivity is 
higher in Python and then D starts to overtake as a project 
gets larger and/or has stricter requirements. I hope never to 
have to write anything longer than a thousand lines in Python 
ever again.


The thing about Python is NumPy, SciPy, Pandas, Matplotlib, 
IPython, Jupyter, GNU Radio. The data science, bioinformatics, 
quant, signal provessing, etc. people do not give a sh!t which 
language they used, what they want is to get their results as 
fast as possible. Most of them do not write programs that are 
to last, they are effectively throw away programs. This leads 
them to Python (or R) and they are not really interested in 
learning anything else.


The fact that NumPy sort of sucks in terms of performance, isn't
noticed by them
as they get their results "fast enough" and a lot faster than
sequential Python. The fact that if they used Chapel or even D 
for
their compute intensive code they would rapidly discover that 
NumPy
sort of sucks never really occurs to these people as they are 
focussed

on the results not the means of achieving them.

Polyglot Python/D or Python/Chapel with Matplotlib is the way 
to go. But that really requires a D replacement for Pandas.



Russell, thanks for your thoughts - I appreciate it.

What would a Pandas replacement look like in D?



Re: LuaD: creating a flexible data filter system

2015-10-17 Thread Laeeth Isharc via Digitalmars-d-learn

On Saturday, 17 October 2015 at 13:15:17 UTC, yawniek wrote:

many thanks for the valuable insights.
so far i made a simple prototype with LuaD and classes, works 
nicely for when my niput


what so far is not 100% clear is if there is a way to have a 
parsed
msgpack or json documents being exposed in my lua code in a way 
so it behaves

like a lua object.
Ideally in a RW fashion so that changed then again can be 
processed by D code.


http://luajit.org/ext_ffi_tutorial.html

C meta methods


Re: what is wrong with this code??

2015-10-17 Thread anonymous via Digitalmars-d-learn
On Saturday, October 17, 2015 04:17 PM, steven kladitis wrote:

> // it thows a range exception

On which line?


Re: what is wrong with this code??

2015-10-17 Thread anonymous via Digitalmars-d-learn
On Saturday, October 17, 2015 04:50 PM, steven kladitis wrote:

> core.exception.RangeError@sokuban.d(84): Range violation

Line 84 being this:

sDataBuild ~= sMap[ch];


Where sMap is:

/*static*/ immutable sMap =
[' ':' ', '.':'.', '@':' ', '#':'#', '$':' '];


Apparently, ch is some char that's not a key in sMap. Try printing out ch to 
see what it is exactly. Then, assuming it's really not a key in sMap, figure 
out if sMap should have that key, or if ch should not end up with that 
value.


Compiling a .d file both as library and executable

2015-10-17 Thread Shriramana Sharma via Digitalmars-d-learn
In Python there is:

if __name__ == "__main__":

to allow the same source file to be treated as both an importable library 
and as an executable script. In D is there any such mechanism to make a 
main() compiled selectively, i.e. by some compile-time flag or such?

-- 
Shriramana Sharma, Penguin #395953


Re: what is wrong with this code??

2015-10-17 Thread steven kladitis via Digitalmars-d-learn

On Saturday, 17 October 2015 at 14:47:11 UTC, anonymous wrote:

On Saturday, October 17, 2015 04:17 PM, steven kladitis wrote:


// it thows a range exception


On which line?


core.exception.RangeError@sokuban.d(84): Range violation


Re: what is wrong with this code??

2015-10-17 Thread steven kladitis via Digitalmars-d-learn

On Saturday, 17 October 2015 at 14:59:41 UTC, anonymous wrote:

On Saturday, October 17, 2015 04:50 PM, steven kladitis wrote:


core.exception.RangeError@sokuban.d(84): Range violation


Line 84 being this:

sDataBuild ~= sMap[ch];


Where sMap is:

/*static*/ immutable sMap =
[' ':' ', '.':'.', '@':' ', '#':'#', '$':' '];


Apparently, ch is some char that's not a key in sMap. Try 
printing out ch to see what it is exactly. Then, assuming it's 
really not a key in sMap, figure out if sMap should have that 
key, or if ch should not end up with that value.


Yes!!! Now it makes sense!!!  Thanks


Re: Compiling a .d file both as library and executable

2015-10-17 Thread anonymous via Digitalmars-d-learn
On Saturday, October 17, 2015 05:36 PM, Shriramana Sharma wrote:

> In Python there is:
> 
> if __name__ == "__main__":
> 
> to allow the same source file to be treated as both an importable library
> and as an executable script. In D is there any such mechanism to make a
> main() compiled selectively, i.e. by some compile-time flag or such?
> 

http://stackoverflow.com/questions/18537761/hyrbid-modul-and-program-behaviour-for-a-d-source-file