Re: Memoization in compile-time

2015-03-13 Thread Meta via Digitalmars-d-learn

On Friday, 13 March 2015 at 13:51:43 UTC, Dennis Ritchie wrote:

And you can somehow memoization stuff at compile time?


If you want memoization at compile time I would suggest using the 
template version of Factorial as template instantiations are 
cached by the compiler. However, std.functional.memoize may work 
as well at compile time, I don't know.


Re: is eC alot like D?

2015-03-13 Thread Dude via Digitalmars-d-learn

On Wednesday, 11 March 2015 at 03:16:50 UTC, Taylor Hillegeist
wrote:
So I found http://ec-lang.org/ it seems alot like D, But it has 
a company backing it. It just seems interesting.


heh they choose a lot worse name for a language than D :D.
https://eclang.codeplex.com/wikipage?title=Intro%20to%20Ec


Re: Memoization in compile-time

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 11:28 AM, Ali Çehreli wrote:

 enum int[] factorials = memoizeFactorials(N);

Oops! That's generally a trap! The array better be 'static' because a 
manifest constant like 'enum factorials' would be inserted everywhere it 
is used. (Similar to a C macro.)


I've been scratching my head why the assertion below was failing.

It sould be 'static':

static int[] factorials = memoizeFactorials(N);

If it's an enum, the following assert will fail:

foreach (i; 0 .. N) {
assert(factorials.ptr + i == (factorials[i]));
}

Make it a 'static', it will pass because then there will be just one 
factorials array.


Ali



Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 16:30:07 UTC, Matt wrote:


Thank you, adding the subConfigurations section to dub.json 
seems to allow the program to compile and run successfully.


However, to test the code, I first tried in the main program:

---

py_eval!string(import sys\nprint(sys.path));

---

and it didn't print anything. No error, the program just ran 
and closed.
Then, I removed that and added print(sys.path) to my site.py 
as you suggested (the sys module is already imported), and 
still the program runs without printing anything, so I'm a 
little confused by that. I'll probably try running some of the 
example code, see if I can figure it out, but if you can 
advise, that would be fantastic. Thank you for all the help so 
far, it's really been appreciated


I've tried the hello.d example from GitHub, and get the error 
message:
   Error: template instance py_eval!string template 'py_eval' is 
not defined


Unfortunately, that's all I seem to get. Is py_eval not 
deprecated in 0.9.4?


Re: Memoization in compile-time

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 06:16 AM, weaselcat wrote:

 confusingly, D uses enum for named compile-time constants.
 http://ddili.org/ders/d.en/enum.html

Actually, 'static' works as well. I have enumerated the cases here:

http://ddili.org/ders/d.en/functions_more.html#ix_functions_more.CTFE

quote
For a function to be executed at compile time, it must appear in an 
expression that in fact is needed at compile time:


- Initializing a static variable

- Initializing an enum variable

- Calculating the length of a fixed-length array

- Calculating a template value argument
/quote

Are there other cases that is worth adding to that list?

Ali



Re: is eC alot like D?

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 10:18 AM, Dude wrote:

On Wednesday, 11 March 2015 at 03:16:50 UTC, Taylor Hillegeist
wrote:

So I found http://ec-lang.org/ it seems alot like D, But it has a
company backing it. It just seems interesting.


heh they choose a lot worse name for a language than D :D.
https://eclang.codeplex.com/wikipage?title=Intro%20to%20Ec


Following Go's lead: :D

  http://en.wikipedia.org/wiki/Go!_%28programming_language%29

Ali



Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 13 March 2015 at 18:38:16 UTC, Ali Çehreli wrote:

On 03/13/2015 11:28 AM, Ali Çehreli wrote:

 enum int[] factorials = memoizeFactorials(N);

Oops! That's generally a trap! The array better be 'static' 
because a manifest constant like 'enum factorials' would be 
inserted everywhere it is used. (Similar to a C macro.)


I've been scratching my head why the assertion below was 
failing.


It sould be 'static':

static int[] factorials = memoizeFactorials(N);

If it's an enum, the following assert will fail:

foreach (i; 0 .. N) {
assert(factorials.ptr + i == (factorials[i]));
}

Make it a 'static', it will pass because then there will be 
just one factorials array.


Ali


Thanks.

And you can make the same memoized variable was available at 
compile time and at run time? :)


Re: Compilation changes

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 05:26 AM, bearophile wrote:


Now I think it doesn't work any more, giving errors like this:


That may be related to the recent changes in the build system. Have you 
been following the following threads? (I haven't been; so, I am sure 
whether they apply.)


[dmd-internals] DMD now requires a working D compiler to be build

Proposal : aggregated dlang git repository

dmd 2.066.1 cannot build phobos 2.066.1

Ali



Re: Memoization in compile-time

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 06:51 AM, Dennis Ritchie wrote:

And you can somehow memoization stuff at compile time?


Scarily simple. :D

import std.stdio;

enum N = 15;
enum int[] factorials = memoizeFactorials(N);

int[] memoizeFactorials(int n)
{
if (!__ctfe) {
// Make sure that this function is never called at run time
assert(false);
}

int[] result = new int[n];

result[0] = 1;

foreach (i; 1 .. n) {
result[i] = result[i - 1] * i;
}

return result;
}

int fact(int n)
{
return factorials[n];
}

void main()
{
foreach (i; 0 .. N) {
writeln(fact(i));
}
}

Ali



Re: OutputDebugString()

2015-03-13 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 13 March 2015 at 21:12:52 UTC, Robert M. Münch wrote:

How do I declare such missing Windows API functions myself?


In the file you want to use it, you can just write

extern(Windows) void OutputDebugStringA(in char*);

and it should work... or whatever the signature is, check msdn, 
and remember the ones that take strings tend to need A or W for 
the ascii or Unicode variants.


dmd comes with a bunch of windows lib link files but not all of 
them. This one should work though. Just add the file, e.g. 
user32.lib, to the compile command line. You can also do


pragma(lib, user32);

and such in the main D file itself you are building and it will 
add automatically.


Re: Memoization in compile-time

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/13/2015 12:09 PM, Dennis Ritchie wrote:

 And you can make the same memoized variable was available at compile
 time and at run time? :)

Yes, run-time is always possible and if it can be computed at compile 
time, compile-time is also possible.


Ali



Re: Compilation changes

2015-03-13 Thread bearophile via Digitalmars-d-learn

Ali Çehreli:


That may be related to the recent changes in the build system.


Right.


Have you been following the following threads? (I haven't been; 
so, I am sure whether they apply.)


I have not.


[dmd-internals] DMD now requires a working D compiler to be 
build


Proposal : aggregated dlang git repository

dmd 2.066.1 cannot build phobos 2.066.1


I will try to get something out of those threads, thank you :-)

Bye,
bearophile


Re: OutputDebugString()

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 21:12:52 UTC, Robert M. Münch wrote:
Hi, I want to use the Windows OutputDebugString() which is not 
defined anywhere.


How do I declare such missing Windows API functions myself? And 
with which libaries do I then need to link? Does DMD contain 
all necessary Windows link libs or am I supposed to get them 
myself?


So, it's not clear what to do if a Windows API function is 
missing.


And, how can I contribute new declared ones to get them 
integrated into the standard distribution?


I'm pretty sure the correct way to do it is as follows:

---

import core.sys.windows.windows;

extern (Windows) OutputDebugString( LPCTSTR );
/// Add other externs here

---


Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 13 March 2015 at 21:16:24 UTC, Ali Çehreli wrote:

On 03/13/2015 12:09 PM, Dennis Ritchie wrote:

 And you can make the same memoized variable was available at
compile
 time and at run time? :)

Yes, run-time is always possible and if it can be computed at 
compile time, compile-time is also possible.


Thanks.


OutputDebugString()

2015-03-13 Thread Robert M. Münch via Digitalmars-d-learn
Hi, I want to use the Windows OutputDebugString() which is not defined 
anywhere.


How do I declare such missing Windows API functions myself? And with 
which libaries do I then need to link? Does DMD contain all necessary 
Windows link libs or am I supposed to get them myself?


So, it's not clear what to do if a Windows API function is missing.

And, how can I contribute new declared ones to get them integrated into 
the standard distribution?


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



Re: OutputDebugString()

2015-03-13 Thread Kagamin via Digitalmars-d-learn

On Friday, 13 March 2015 at 21:12:52 UTC, Robert M. Münch wrote:
Hi, I want to use the Windows OutputDebugString() which is not 
defined anywhere.


The declaration can be already part of WindowsAPI project: 
https://github.com/AndrejMitrovic/WindowsAPI just see it there.


How do I declare such missing Windows API functions myself? And 
with which libaries do I then need to link?


This function has rather unique implementation too. Are you sure, 
you don't want to see its MSDN entry?


Re: embedding Pyd in Windows program

2015-03-13 Thread Ellery Newcomer via Digitalmars-d-learn

On Friday, 13 March 2015 at 19:05:59 UTC, Matt wrote:

example code, see if I can figure it out, but if you can 
advise, that would be fantastic. Thank you for all the help so 
far, it's really been appreciated


My penitence for not putting this information on readthedocs.



I've tried the hello.d example from GitHub, and get the error 
message:
   Error: template instance py_eval!string template 'py_eval' 
is not defined


Unfortunately, that's all I seem to get. Is py_eval not 
deprecated in 0.9.4?


hmm. that's odd. are you trying to build with dub? the only 
examples that build with dub are in examples/dub. the rest use 
distutils.


Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread Rikki Cattermole via Digitalmars-d-learn

On 13/03/2015 7:47 p.m., zhmt wrote:

On Friday, 13 March 2015 at 06:39:31 UTC, Rikki Cattermole wrote:

On 13/03/2015 7:35 p.m., zhmt wrote:

ubyte[] arr ;

I pass the arr.ptr to c program, it fails silently.

Is there any way to cast a ubyte[] to a clang pointer?


Theoretically this should work.

D:

void func(ubyte[] value) {
func(value.length, value.ptr);
}

extern(C) void func(size_t length, ubyte*);

C:
void func(size_t length, ubyte*) {
// ...
}


Thank you for your confirm,I will try again.


Just be careful about GC pointers. They could be free'd and the c 
library thinks it is still live.
Either keep a copy of the pointer alive on the heap somewhere or tell 
the GC to ignore it.


Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread zhmt via Digitalmars-d-learn

On Friday, 13 March 2015 at 06:39:31 UTC, Rikki Cattermole wrote:

On 13/03/2015 7:35 p.m., zhmt wrote:

ubyte[] arr ;

I pass the arr.ptr to c program, it fails silently.

Is there any way to cast a ubyte[] to a clang pointer?


Theoretically this should work.

D:

void func(ubyte[] value) {
func(value.length, value.ptr);
}

extern(C) void func(size_t length, ubyte*);

C:
void func(size_t length, ubyte*) {
// ...
}


Thank you for your confirm,I will try again.


how to pass a ubyte[] to c interface?

2015-03-13 Thread zhmt via Digitalmars-d-learn

ubyte[] arr ;

I pass the arr.ptr to c program, it fails silently.

Is there any way to cast a ubyte[] to a clang pointer?


Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread Rikki Cattermole via Digitalmars-d-learn

On 13/03/2015 7:35 p.m., zhmt wrote:

ubyte[] arr ;

I pass the arr.ptr to c program, it fails silently.

Is there any way to cast a ubyte[] to a clang pointer?


Theoretically this should work.

D:

void func(ubyte[] value) {
func(value.length, value.ptr);
}

extern(C) void func(size_t length, ubyte*);

C:
void func(size_t length, ubyte*) {
// ...
}


Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread zhmt via Digitalmars-d-learn
I have some source code of vibe.d, it does this in the same way, 
and it works .


void read(ubyte[] dst)
{
checkConnected(false);
acquireReader();
scope(exit) releaseReader();
while (dst.length  0) {
checkConnected(false);
			logTrace(evbuffer_read %d bytes (fd %d), dst.length, 
m_ctx.socketfd);
			auto nbytes = bufferevent_read(m_ctx.event, dst.ptr, 
dst.length);

logTrace( .. got %d bytes, nbytes);
dst = dst[nbytes .. $];

if( dst.length == 0 ) break;

checkConnected(false);
m_ctx.core.yieldForEvent();
}
logTrace(read data);
}


Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread Ali Çehreli via Digitalmars-d-learn

On 03/12/2015 11:35 PM, zhmt wrote:

 ubyte[] arr ;

 I pass the arr.ptr to c program

Unless there is sentinel value at the end of the array, you must also 
pass the number of elements (as Rikki Cattermole has shown).


However, if the C function holds on to that pointer for later use, you 
must also keep the array alive. You can ensure that by two general ways:


a) The D-side slice must not be a local slice. For example, it can be a 
member of a long-living object or it is a module-scope variable.


b) Tell the garbage collector that there is indeed a reference to that 
memory block somewhere else (even though there is no D-side reference to 
it). You can do this with GC.addRoot. (The opposite function is 
GC.removeRoot.)


, it fails silently.

That's not good.

 Is there any way to cast a ubyte[] to a clang pointer?

Going off-topic, I think you mean the C language when you say clang, 
which may be confusing because there is also the compiler called clang. :)


Ali



Re: how to pass a ubyte[] to c interface?

2015-03-13 Thread zhmt via Digitalmars-d-learn

On Friday, 13 March 2015 at 06:56:33 UTC, Ali Çehreli wrote:

On 03/12/2015 11:35 PM, zhmt wrote:

 ubyte[] arr ;

 I pass the arr.ptr to c program

Unless there is sentinel value at the end of the array, you 
must also pass the number of elements (as Rikki Cattermole has 
shown).


However, if the C function holds on to that pointer for later 
use, you must also keep the array alive. You can ensure that by 
two general ways:


a) The D-side slice must not be a local slice. For example, it 
can be a member of a long-living object or it is a module-scope 
variable.


b) Tell the garbage collector that there is indeed a reference 
to that memory block somewhere else (even though there is no 
D-side reference to it). You can do this with GC.addRoot. (The 
opposite function is GC.removeRoot.)


, it fails silently.

That's not good.

 Is there any way to cast a ubyte[] to a clang pointer?

Going off-topic, I think you mean the C language when you say 
clang, which may be confusing because there is also the 
compiler called clang. :)


Ali



sorry for clang, I mean c , not clang of mac.

Thanks for you advice, I use coroutine, so the array will hold in 
stack, will not be gc before return.


And I successfully passed the arr.ptr to c program. I found the 
root of problem:
The order of method declaration in c++ class and d interface must 
be exactly the same:


this is d code:

interface CConn
 {
void free();
int connect(ubyte* ip,int port,CFiberCtx fiberctx);
int read(ubyte* buf,int offset, int len,CFiberCtx fiberctx);
int readSome(ubyte* buf,int offset, int len,CFiberCtx fiberctx);
int write(ubyte* buf,int offset, int len,CFiberCtx fiberctx);
}

this is c++ class:

class CConn
{
public:
  // dlang interfaces;
  virtual void free();
  virtual int connect(char* ip,int port,CFiberCtx *fiberctx);
  virtual int read(char* buf,int offset, int len,CFiberCtx 
*fiberctx);
  virtual int write(char* buf,int offset, int len,CFiberCtx 
*fiberctx); //ERROR : swap this line with below, everything will 
be ok
  virtual int readSome(char* buf,int offset, int len,CFiberCtx 
*fiberctx);

}

After swapping the last two lines, everything goes well.

The tutorial of d dont mention this.

Thanks for all replies.


Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 01:40:34 UTC, Ellery Newcomer wrote:

On 03/11/2015 07:59 PM, Matt wrote:


Right, copying site.py into my program's working dir sorts out 
the

missing module error, but I now get a syntax error:

file=sys.stderr)
^
SyntaxError: invalid syntax
Error executing command run: Program exited with code 1

I googled this, and apparently it's caused by an older 
interpreter. The

only files I have transferred over are:
  python3.DLL
  python3.LIB
  python34.LIB

Does anyone have any idea what I might be doing wrong, or how 
I can fix

this?


I'm guessing your python path is screwed up. In your embedded 
python,


import sys
print (sys.path)

make sure everything is pointing where it should.

Failing that,

are you using the correct dub configuration? without checking, 
I believe it defaults to python 2.7.


Failing that,

you transferred python3.dll from somewhere to somewhere? I 
believe pyd links to \windows\system32\python3.dll or some 
such. At least, I think that's where those lib files point to. 
not sure. If that is an issue and you want to use your own lib 
files, you will need to generate OMF files from them.


I used the ~0.9.4 branch in dub, and I'm not sure how to 
change configuration. Do you mean I should be using ~master 
or ~develop?


as for the transferring the python3.dll, I downloaded and 
installed a fresh copy of the latest Python build, version 3.4, 
and copied the libraries from there. Even so, I had to add 
PYD_PACKAGE_DIR manually to get it to work.


I'm not sure I'm going to be able to run the python snippet you 
sent, since my program, which currently opens a window for three 
seconds before closing again, doesn't even get that far, and 
chokes when I run py_init().


How to reference another dub project?

2015-03-13 Thread zhmt via Digitalmars-d-learn
I have two dub projects, one is library, the other one is app. 
The library is referenced by the app. How to reference it in 
dub.json?


I found the subPackage, It does not meet my needs exactly.


Re: How to reference another dub project?

2015-03-13 Thread zhmt via Digitalmars-d-learn

On Friday, 13 March 2015 at 08:24:11 UTC, zhmt wrote:
I have two dub projects, one is library, the other one is app. 
The library is referenced by the app. How to reference it in 
dub.json?


I found the subPackage, It does not meet my needs exactly.


I got it:

dependencies: {
gamelibd:{path:../gamelibd/}
},


Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole wrote:
You could assign it to e.g. an enum. Or force it over using 
meta-programming.


And this code can be rewritten to D?

template int n
struct Factorial
{
enum { value = n * Factorialn - 1::value };
};

template 
struct Factorial0
{
enum { value = 1 };
};

int main()
{
constexpr auto x = Factorial5::value;
constexpr auto y = Factorial7::value;
}


Compilation changes

2015-03-13 Thread bearophile via Digitalmars-d-learn
Time ago Dmitry Olshansky gave me a nice script to compile 
dmd+Phobos from github, starting like this:



My recipe on Windows, by Dmitry Olshansky:
0. Make sure there are no other DMD in the Windows path.
1. Get the latest DMD release zip you can find, unzip to some 
drive root(! otherwise get's trickier)

2. Delete all within src subfolder
3. Get a Git console in \dmd2\src, then the usual:
...


Now I think it doesn't work any more, giving errors like this:

make -fwin32.mak C=backend TK=tk ROOT=root HOST_DC= reldmd
make -fwin32.mak C=backend TK=tk ROOT=root HOST_DC= OPT=-o 
DEBUG= LFLAGS=-L/delexe/la dmd.exe

run idgen
Error: 'run' not found

--- errorlevel 1

--- errorlevel 1


So is Dmitry (or someone else) willing and able to tell me how to 
fix my compilation script and what to do?


Thank you,
bye,
bearophile


Re: is eC alot like D?

2015-03-13 Thread Chris via Digitalmars-d-learn
On Wednesday, 11 March 2015 at 03:16:50 UTC, Taylor Hillegeist 
wrote:
So I found http://ec-lang.org/ it seems alot like D, But it has 
a company backing it. It just seems interesting.


Seems to me that structs are not the same as in D, and structs in 
D are very powerful. I don't like the fact that they mention 
object orientation right from the start, as if it was the be all 
end all of programming - which it isn't.


Hm. I have to admit that I'm biased towards D, but D has come a 
long way and is the result of serious input from loads of users 
and experts. Any new language has a lot of catching up to do. In 
a way it's funny that every new language builds up an 
infrastructure first, toolchains etc. Like someone who's trying 
to learn how to play guitar buys expensive amps and flashy effect 
devices before s/he can even play a single chord.


However, language features aside, how is D doing in the mobile 
sector? Most new languages boast Android/iOS support. Even if the 
language is inferior to D, people will opt for any language that 
can be ported to mobile platforms, regardless of its features.


Re: Memoization in compile-time

2015-03-13 Thread Meta via Digitalmars-d-learn

On Friday, 13 March 2015 at 12:49:48 UTC, Dennis Ritchie wrote:
On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole 
wrote:
You could assign it to e.g. an enum. Or force it over using 
meta-programming.


And this code can be rewritten to D?

template int n
struct Factorial
{
enum { value = n * Factorialn - 1::value };
};

template 
struct Factorial0
{
enum { value = 1 };
};

int main()
{
constexpr auto x = Factorial5::value;
constexpr auto y = Factorial7::value;
}


You can translate it directly:

template Factorial(int n)
{
static if (n == 0)
{
enum Factorial = 1;
}
else static if (n  0)
{
enum Factorial = n * Factorial!(n - 1);
}
else
{
static assert(false, n cannot be negative);
}
}

int main()
{
//Calculated at compile time
auto x = Factorial!5;
auto y = Factorial!7;
}




However, it's much easier to just write a function and decide 
whether you want to calculate its value at runtime or compile 
time.


int factorial(int n)
in
{
assert(n = 0, n cannot be 0);
}
body
{
return n == 0 ? 1 : n * factorial(n - 1);
}

void main()
{
//Evaluated at compile time
enum x = factorial(5);

//Evaluated at runtime
auto y = factorial(7);
}


Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn

On Friday, 13 March 2015 at 12:58:13 UTC, Meta wrote:

On Friday, 13 March 2015 at 12:49:48 UTC, Dennis Ritchie wrote:
On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole 
wrote:
You could assign it to e.g. an enum. Or force it over using 
meta-programming.


And this code can be rewritten to D?

template int n
struct Factorial
{
   enum { value = n * Factorialn - 1::value };
};

template 
struct Factorial0
{
   enum { value = 1 };
};

int main()
{
   constexpr auto x = Factorial5::value;
   constexpr auto y = Factorial7::value;
}


You can translate it directly:

template Factorial(int n)
{
static if (n == 0)
{
enum Factorial = 1;
}
else static if (n  0)
{
enum Factorial = n * Factorial!(n - 1);
}
else
{
static assert(false, n cannot be negative);
}
}

int main()
{
//Calculated at compile time
auto x = Factorial!5;
auto y = Factorial!7;
}




However, it's much easier to just write a function and decide 
whether you want to calculate its value at runtime or compile 
time.


int factorial(int n)
in
{
assert(n = 0, n cannot be 0);
}
body
{
return n == 0 ? 1 : n * factorial(n - 1);
}

void main()
{
//Evaluated at compile time
enum x = factorial(5);

//Evaluated at runtime
auto y = factorial(7);
}


Thanks.


Re: Memoization in compile-time

2015-03-13 Thread weaselcat via Digitalmars-d-learn

On Friday, 13 March 2015 at 13:16:27 UTC, weaselcat wrote:

On Friday, 13 March 2015 at 12:49:48 UTC, Dennis Ritchie wrote:
On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole 
wrote:
You could assign it to e.g. an enum. Or force it over using 
meta-programming.


And this code can be rewritten to D?

template int n
struct Factorial
{
   enum { value = n * Factorialn - 1::value };
};

template 
struct Factorial0
{
   enum { value = 1 };
};

int main()
{
   constexpr auto x = Factorial5::value;
   constexpr auto y = Factorial7::value;
}


confusingly, D uses enum for named compile-time constants.
http://ddili.org/ders/d.en/enum.html

If you take Rikki's example and apply it to an enum(i.e,
enum x = factorial(5);
)

the program will fail to compile if it can't be computed at 
compile-time.


woops, walked away to get coffee before I submitted and got
beaten to the punch by a better answer : )


Re: Memoization in compile-time

2015-03-13 Thread weaselcat via Digitalmars-d-learn

On Friday, 13 March 2015 at 12:49:48 UTC, Dennis Ritchie wrote:
On Friday, 13 March 2015 at 02:38:18 UTC, Rikki Cattermole 
wrote:
You could assign it to e.g. an enum. Or force it over using 
meta-programming.


And this code can be rewritten to D?

template int n
struct Factorial
{
enum { value = n * Factorialn - 1::value };
};

template 
struct Factorial0
{
enum { value = 1 };
};

int main()
{
constexpr auto x = Factorial5::value;
constexpr auto y = Factorial7::value;
}


confusingly, D uses enum for named compile-time constants.
http://ddili.org/ders/d.en/enum.html

If you take Rikki's example and apply it to an enum(i.e,
enum x = factorial(5);
)

the program will fail to compile if it can't be computed at 
compile-time.


Re: Memoization in compile-time

2015-03-13 Thread Dennis Ritchie via Digitalmars-d-learn

And you can somehow memoization stuff at compile time?


Re: is eC alot like D?

2015-03-13 Thread ketmar via Digitalmars-d-learn
On Fri, 13 Mar 2015 04:02:07 +, Jerome St-Louis wrote:

 To confirm guys eC does support C preprocessing.
 The C preprocessor is invoked before the eC compiler does its parsing.

ah, i see.

 eC tries to be a superset of C as much possible, with only a few keyword
 clashes like 'class' as an exception.

so they choose to throw away a possibility to include good type system 
into the language... ahem... looks bad for me. nice try, nevertheless.

signature.asc
Description: PGP signature


Re: embedding Pyd in Windows program

2015-03-13 Thread Ellery Newcomer via Digitalmars-d-learn

On Friday, 13 March 2015 at 09:38:45 UTC, Matt wrote:


I used the ~0.9.4 branch in dub, and I'm not sure how to 
change configuration. Do you mean I should be using ~master 
or ~develop?


nope. configurations are a different thing.

you can set them in your project's dub.json. example:

https://github.com/ariovistus/pyd/blob/master/examples/dub/simple_embedded/dub.json

for your project, I think you'd want

subConfigurations: {
  pyd: python34,
}



as for the transferring the python3.dll, I downloaded and 
installed a fresh copy of the latest Python build, version 3.4, 
and copied the libraries from there. Even so, I had to add 
PYD_PACKAGE_DIR manually to get it to work.


PYD_PACKAGE_DIR is necessary for linking the OMF files found in 
infrastructure/windows. hmm. why did it not Just Work?


I'm not sure I'm going to be able to run the python snippet you 
sent, since my program, which currently opens a window for 
three seconds before closing again, doesn't even get that far, 
and chokes when I run py_init().


you can put it in the top of site.py



Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 14:21:15 UTC, Ellery Newcomer wrote:

On Friday, 13 March 2015 at 09:38:45 UTC, Matt wrote:


I used the ~0.9.4 branch in dub, and I'm not sure how to 
change configuration. Do you mean I should be using 
~master or ~develop?


nope. configurations are a different thing.

you can set them in your project's dub.json. example:

https://github.com/ariovistus/pyd/blob/master/examples/dub/simple_embedded/dub.json

for your project, I think you'd want

subConfigurations: {
  pyd: python34,
}



as for the transferring the python3.dll, I downloaded and 
installed a fresh copy of the latest Python build, version 
3.4, and copied the libraries from there. Even so, I had to 
add PYD_PACKAGE_DIR manually to get it to work.


PYD_PACKAGE_DIR is necessary for linking the OMF files found in 
infrastructure/windows. hmm. why did it not Just Work?


I'm not sure I'm going to be able to run the python snippet 
you sent, since my program, which currently opens a window for 
three seconds before closing again, doesn't even get that far, 
and chokes when I run py_init().


you can put it in the top of site.py


Thank you, adding the subConfigurations section to dub.json 
seems to allow the program to compile and run successfully.


However, to test the code, I first tried in the main program:

---

py_eval!string(import sys\nprint(sys.path));

---

and it didn't print anything. No error, the program just ran and 
closed.
Then, I removed that and added print(sys.path) to my site.py as 
you suggested (the sys module is already imported), and still the 
program runs without printing anything, so I'm a little confused 
by that. I'll probably try running some of the example code, see 
if I can figure it out, but if you can advise, that would be 
fantastic. Thank you for all the help so far, it's really been 
appreciated