Re: Conway's game of life

2015-02-02 Thread gedaiu via Digitalmars-d-learn

Uf...  you are right!

I've fixed it.

Thanks!


On Monday, 2 February 2015 at 11:23:17 UTC, FG wrote:

Bloody Thunderbird has sent a reply to the OP and not to the NG.

On 2015-02-02 at 11:45, gedaiu wrote:
I don't think that the line of code is wrong. If use && the 
function will check for neighbours only on diagonals. Having 
|| allows the search on the vertical and horizontal axis and 
diagonals.


In short: Yes, && alone would check only diagonals, but I 
forgot to tell you to also change the ==.


(diff1 == 1 || diff2 == 1) -- bad, accepts whole neighbouring 
rows and columns
(diff1 == 1 && diff2 == 1) -- bad, accepts only neighbouring 
diagonals

(diff1 <= 1 && diff2 <= 1) -- correct, I think :)

This unittest should show the difference:

unittest {
CellList world = [ Cell(0,0), Cell(0,1), Cell(0,2), 
Cell(0,3) ];

assertEqual(Cell(1,1).neighbours(world), 3);
}

Cell(0,3) is not a neighbour bit fits the (diff1 == 1 || diff2 
== 1) criterion.




Re: Conway's game of life

2015-02-02 Thread gedaiu via Digitalmars-d-learn
I don't think that the line of code is wrong. If use && the 
function will check for neighbours only on diagonals. Having || 
allows the search on the vertical and horizontal axis and 
diagonals.


There are some tests that check the function:

unittest {
	CellList world = [ Cell(0,0), Cell(0,1), Cell(0,2), Cell(1,0), 
Cell(1,2), Cell(2,0), Cell(2,1), Cell(2,2) ];

assertEqual(Cell(1,1).neighbours(world), world.length);
}

unittest {
CellList world = [ Cell(0,0), Cell(1,1), Cell(2,2), Cell(3,3) ];
assertEqual(Cell(1,1).neighbours(world), 2);
}

I don't see a glitch.

Thanks,
Bogdan



On Sunday, 1 February 2015 at 22:51:42 UTC, FG wrote:

On 2015-02-01 at 22:00, gedaiu wrote:

I implemented Conway's game of life in D.


I think you are playing a different game here.

/// Count cell neighbours
long neighbours(Cell myCell, CellList list) {
long cnt;
foreach(cell; list) {
auto diff1 = abs(myCell.x - cell.x);
auto diff2 = abs(myCell.y - cell.y);
if(diff1 == 1 || diff2 == 1) cnt++;   // Why || 
instead of && ???

}
return cnt;
}




Re: Conway's game of life

2015-02-02 Thread gedaiu via Digitalmars-d-learn
It's true that I have to change that function. Thanks for the 
notice!


Why do you think that D's GC is crap?




On Sunday, 1 February 2015 at 21:54:43 UTC, Foo wrote:

On Sunday, 1 February 2015 at 21:00:07 UTC, gedaiu wrote:

Hi,

I implemented Conway's game of life in D. What do you think 
that

I can improve to this program to take advantage of more D
features?

https://github.com/gedaiu/Game-Of-Life-D

Thanks,
Bogdan


For each remove you create a new array. That is lavish. You 
should use a bool inside the struct Cell. If it's false, the 
cell is not displayed and is out of the game.
My suggestion: don't use the D GC, it's crap. Try to circumvent 
the GC wherever possible. Therefore you should use the -vgc 
compiler flag and the @nogc attribute.




Conway's game of life

2015-02-01 Thread gedaiu via Digitalmars-d-learn

Hi,

I implemented Conway's game of life in D. What do you think that
I can improve to this program to take advantage of more D
features?

https://github.com/gedaiu/Game-Of-Life-D

Thanks,
Bogdan


Re: template bug?

2014-12-04 Thread gedaiu via Digitalmars-d-learn

On Wednesday, 3 December 2014 at 18:25:41 UTC, bearophile wrote:

Ali Çehreli:

Attempting to compile with a recent dmd git head causes 
segmentation fault. Any compiler crash is a compiler bug. 
Please report it at


 https://issues.dlang.org/

Ali


A first reduction for Bugzilla:


alias TypeTuple(T...) = T;
struct A {
void foo() {}
}
template ItemProperty(item, string method) {
static if(__traits(getProtection, ItemProperty!(item, 
method)).stringof)
alias ItemProperty = TypeTuple!(ItemProperty!(item, 
method));

}
void main() {
auto l = ItemProperty!(A, "foo").length;
}


Bye,
bearophile


Thanks for the code revised version. I thought that the compiler 
crashes because I was trying to get the access of an overrided 
method.


Bogdan


template bug?

2014-12-03 Thread gedaiu via Digitalmars-d-learn

Hi,

Is this a bug in the compiler?


import std.stdio;
import std.typetuple;

class A {

int foo() {
return 0;
}
}

class B : A {
alias A.foo foo;

override int foo() {
return 1;
}

}


template ItemProperty(item, string method) {
static if(__traits(hasMember, item, method)) {
		static if(__traits(getProtection, ItemProperty!(item, 
method)).stringof[1..$-1] == "public") {

alias ItemProperty = TypeTuple!(ItemProperty!(item, 
method));
} else {
alias ItemProperty = TypeTuple!();
}

} else {
alias ItemProperty = TypeTuple!();
}
}

void test()() {

static if(ItemProperty!(B, "foo").length > 1) {
writeln("found");
} else {
writeln("not found");
}
}


void main()
{

test;


}


Re: find all public properties at compile time

2014-09-30 Thread gedaiu via Digitalmars-d-learn

[sorry... this is the edit for the prev post]

Thank you for your response!

I don't think that it helps me...

I wanted to get an array like this [ "a", "b", "c" ] for this
class

class test {

int a;
string b;
double c;

}

Bogdan


On Tuesday, 30 September 2014 at 14:20:04 UTC, Rene Zwanenburg 
wrote:

On Monday, 29 September 2014 at 20:21:43 UTC, gedaiu wrote:

Hi,

There is a way to determine all public properties (not 
methods) from a struct/class at compile time?


I seen that there are traits to get only methods but not 
properties. Am I wrong?


thanks,
Bogdan


You can get the function attributes of the returned methods 
using std.traits:


http://dlang.org/library/std/traits/functionAttributes.html
http://dlang.org/library/std/traits/FunctionAttribute.html

Vibe's serializer is also a good place to look. The code is a 
bit intimidating but it's handling a ton of corner cases:


https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/serialization.d#L710




Re: find all public properties at compile time

2014-09-30 Thread gedaiu via Digitalmars-d-learn


Thank you for your response!

I don't think that it helps me...

I wanted to get an array like this [ "a", "b", "c" ] for this 
class


class test {


}

Bogdan


On Tuesday, 30 September 2014 at 14:20:04 UTC, Rene Zwanenburg 
wrote:

On Monday, 29 September 2014 at 20:21:43 UTC, gedaiu wrote:

Hi,

There is a way to determine all public properties (not 
methods) from a struct/class at compile time?


I seen that there are traits to get only methods but not 
properties. Am I wrong?


thanks,
Bogdan


You can get the function attributes of the returned methods 
using std.traits:


http://dlang.org/library/std/traits/functionAttributes.html
http://dlang.org/library/std/traits/FunctionAttribute.html

Vibe's serializer is also a good place to look. The code is a 
bit intimidating but it's handling a ton of corner cases:


https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/serialization.d#L710




find all public properties at compile time

2014-09-29 Thread gedaiu via Digitalmars-d-learn

Hi,

There is a way to determine all public properties (not methods) 
from a struct/class at compile time?


I seen that there are traits to get only methods but not 
properties. Am I wrong?


thanks,
Bogdan


using regex at compiletime

2014-06-15 Thread gedaiu via Digitalmars-d-learn

Hi,

I am trying to use regex at compile time for pasing of some html 
files. The code works perfect at runtime but at compile time i 
get this error:




/usr/include/dmd/phobos/std/regex.d(5824): Error: malloc cannot 
be interpreted at compile time, because it has no available 
source code
/usr/include/dmd/phobos/std/regex.d(5824):called from 
here: enforce(malloc(size), delegate const(char)[]() => null, 
"/usr/include/dmd/phobos/std/regex.d", 5824LU)
/usr/include/dmd/phobos/std/regex.d(5961):called from 
here: matchOnce(input, re)
source/cthtml.d(121):called from here: matchFirst(text, 
rTag)
source/cthtml.d(397):called from here: 
structure.this(content, "", "ROOT")



It is not possible to use regex at compiletime and I should use 
palain text matching?


Thanks,
Bogdan