Re: mscoff x86 invalid pointers

2015-05-09 Thread Etienne Cimon via Digitalmars-d-learn

On 2015-05-09 05:44, Baz wrote:

On Saturday, 9 May 2015 at 06:21:11 UTC, extrawurst wrote:

On Saturday, 9 May 2015 at 00:16:28 UTC, Etienne wrote:

I'm trying to compile a library that I think used to work with
-m32mscoff flag before I reset my machine configurations.

https://github.com/etcimon/memutils

Whenever I run `dub test --config=32mscoff` it gives me an assertion
failure, which is a global variable that already has a pointer value
for some reason..

I'm wondering if someone here could test this out on their machine
with v2.067.1? There's no reason why this shouldn't work, it runs
fine in DMD32/optlink  and DMD64/mscoff, just not in DMD32/mscoff.
Thanks!


you can always use travis-ci to do such a job for you ;)


doesn't -m32mscoff recquire phobos to be compiled as COFF too ? I think
that travis uses the official releases (win32 releases have phobos as
OMF) so he can't run the unittests like that...

The dark side of the story is that you have to recompile phobos by hand
with -m32mscoff...I'm not even sure that there is a option for this in
the win32.mak...



Meh, I ended up upgrading to 2.068 and everything went well. I clearly 
remember 2.067.1 working but spent a whole day recompiling 
druntime/phobos COFF versions in every configuration possible and never 
got it working again


Re: how does 'shared' affect member variables?

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

On Sat, 09 May 2015 21:32:42 -0400, Mike  wrote:

it looks like what you are trying to implement is what `synchronized`  
already provides:   
http://ddili.org/ders/d.en/concurrency_shared.html#ix_concurrency_shared.synchronized


Mike


Yes, but synchronized uses a mutex. Spin locks can perform better in  
situations where there won't be much contention for the lock.


  Bit


Re: how does 'shared' affect member variables?

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

On Saturday, 9 May 2015 at 20:17:59 UTC, bitwise wrote:

On Sat, 09 May 2015 15:38:05 -0400, Mike  wrote:


On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:

Also, I wasn't able to find any thorough documentation on 
shared, so if someone has a link, that would be helpful.




Here are a few interesting links:

Iain Buclaw (lead developer for GDC) with his interpretation:
http://forum.dlang.org/post/mailman.739.1431034764.4581.digitalmar...@puremagic.com

Andrei Alexandrescu highlighting a critical flaw with `shared`
http://forum.dlang.org/post/lruc3n$at1$1...@digitalmars.com

"The truth about shared"
http://p0nce.github.io/d-idioms/#The-truth-about-shared

Interesting deprecation warning in latest compiler (See 
compiler output):

http://goo.gl/EGvK72

But I don't know what the semantics are *supposed* to be, and 
I get the impression noone else knows either.  I'll be 
watching this thread myself to see if someone can provide some 
insight.


Mike


So it seems that although it's not properly implemented, it's 
still not completely benign, right?


I am trying to create a shared queue/array of delegates that 
run on the main thread. I don't know if D's 'shared' semantics 
will affect it or not, and whether or not casting to/from 
shared will cause problems with 'cas' or 'atomicStore'


Would the following code work as expected?



I'm not sure if it would or no, as I haven't used these features 
of D before, but it looks like what you are trying to implement 
is what `synchronized` already provides:  
http://ddili.org/ders/d.en/concurrency_shared.html#ix_concurrency_shared.synchronized


Mike


Re: How to translate this to D: const char *const* someConstPtr;

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn

On 05/09/2015 04:18 PM, ParticlePeter wrote:

> const char *const* someConstPtr;

Disecting:

1) const char : There are these chars that cannot be modified

2) * : There are these pointers to such const chars.

3) const: Those pointers cannot point to anything else

4) * : There are these pointers that can point to previously described 
pointers.


The following are the D equivalents, adding more at each step:

1) const(char)

2) const(char)*

3) const(const(char)*)

4) const(const(char)*)*

As Adam Ruppe said, the first const (the inner-most in the D version) is 
redundant:


  const(char*)*

Ali



Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 13:00:01 UTC, wobbles wrote:
On Linux, I'm able to edit a file descriptor after I've created 
it to tell it to read/write asynchronously, I cant seem to find 
anything similar on windows however.


Asynchronous I/O on Windows is called "Overlapped I/O". It is a 
bit involved to use though, especially since D by default doesn't 
come with all the necessary headers. You can download the win32 
api bindings or you can just declare the bits as needed.


My terminal emulator uses overlapped I/O and a spawned process 
(and on Linux, it uses forkpty!) to talk to ssh on Windows.


https://github.com/adamdruppe/terminal-emulator/blob/master/terminalemulator.d

I had to write a function to make an async pipe, then spawn a 
process using them, then get and send data. I also declared all 
the needed functions myself.


A lot of code to go through but it is a working example... a few 
things to look for are MyCreatePipeEx, CreateProcess, the word 
'overlapped', ReadFileEx, and the simpledisplay.d library it 
imports also uses SleepEx which lets other things trigger.


Re: How to translate this to D: const char *const* someConstPtr;

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

That was fast, thanks :-)


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 18:19:16 UTC, Baz wrote:

You need a loop that run until the PID is invalid.


You could also call WaitForSingleObject on the process handle
https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032%28v=vs.85%29.aspx

The HANDLE it expects can be gotten from std.process with this
http://dlang.org/phobos/std_process.html#osHandle


But then you'll block for it to end so it probably isn't what you 
really want...


Re: How to translate this to D: const char *const* someConstPtr;

2015-05-09 Thread Adam D. Ruppe via Digitalmars-d-learn
The second const isn't needed in D, the first one will carry 
through for it too.


const char* in D is equivalent to that C declaration.

const(char)* in D is what const char* in C would be.


How to translate this to D: const char *const* someConstPtr;

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

Hi,

const char *const* someConstPtr;
Error: no identifier for declarator char*
Error: declaration expected, not '*'

How would I translate this properly to d?

Cheers, PP


Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Saturday, 9 May 2015 at 21:48:05 UTC, Timon Gehr wrote:
Well, it is much slower due to all the allocated closures, owed 
to the fact that the implementations of 'fix' on that page are 
expected to mirror a particular famous implementation in 
untyped lambda calculus.


In case you have a use for 'fix', a more efficient 
implementation might be:


auto fix(S,T...)(S delegate(T) delegate (S delegate(T)) f){
S delegate(T) g=(T a){ assert(0,"f is too eager."); };
return g=f((T a)=>g(a));
}

(In particular, this will only allocate two closures for the 
plumbing instead of a number of them linear in the number of 
recursive invocations.)




Even something like Common Lisp.


(Be aware that Common Lisp implementations typically have 
better garbage collectors than what is available for D.)


Maybe in the future, that D will be added to optimize tail 
recursion delegates?

And why garbage collectors in Lisp is better?


Re: how does 'shared' affect member variables?

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

On Sat, 09 May 2015 15:59:57 -0400, tcak  wrote:
If a variable/class/struct etc is not shared, for variables and struct,  
you find their initial value. For class, you get null.


For first timers (I started using shared keyword more than 2 years ago),  
do not forget that: a shared method is all about saying that this method  
is defined for shared object. So, do not get confused. It happened to me  
a lot.


Bad part of shared is that, you will be repeating it again, and again,  
and again, and again, everywhere. So, try to be patient if you are going  
to be using it for a long time.


Stupidly, shared variables' value cannot be increased/decreased  
directly. Compiler says it is deprecated, and tells me to use  
core.atomic.atomicop. You will see this as well. Hey compiler! I know  
100% that no other thing will be touching this variable.



using the SpinLock and LockGuard from my code above, I created a working  
test case. It works as expected, with no shared keyword on anything. The  
variable "App._instance" is __gshared, but that's about all.


///
import spinlock;
import std.stdio;
import std.concurrency;
import std.container;
import core.thread;

class App
{
SpinLock _lock;
Array!(void delegate()) _actions;

__gshared App _instance = null;

this() {
assert(!_instance);
_instance = this;
}

~this() {
_instance = null;
}

@property public static App instance() {
return _instance;
}

void run(void delegate() dg) {
auto lk = LockGuard!SpinLock(_lock);
_actions.insertBack(dg);
writeln("queued delegate");
}

void update()
{
writeln("updating");

auto lk = LockGuard!SpinLock(_lock);

foreach(act; _actions)
act();

_actions.clear();
}
}

void WorkerThread()
{
writeln("started worker, going to sleep");

Thread.sleep(500.msecs);

App.instance.run({
writeln("running delegate queued from thread");
});
}

void main()
{
App app = new App();

Thread workerThread = new Thread(&WorkerThread).start();

int ms = 0;

while(ms < 1000)
{
app.update();
Thread.sleep(100.msecs);
ms += 100;
}

workerThread.join();
}

//

OUTPUT:

updating
started worker, going to sleep
updating
updating
updating
updating
queued delegate
updating
running delegate queued from thread
updating
updating
updating
updating

//

No null references or 'init' values. A little confused now, but at least  
it works.


Finally, I tested to see if the lock was actually working:

void WorkerThread()
{
writeln("worker aquiring lock");
App.instance._lock.lock();
writeln("worker aquired lock");
App.instance._lock.unlock();
}

void main()
{
App app = new App();

writeln("main locked");
App.instance._lock.lock();

Thread workerThread = new Thread(&WorkerThread).start();

writeln("main sleeping");
Thread.sleep(2.seconds);
writeln("main woke");
App.instance._lock.unlock();
writeln("main unlocked");

workerThread.join();
}

As expected, output was:

main locked
main sleeping
worker aquiring lock
main woke
main unlocked
worker aquired lock



And no 'shared' in sight. So now I'm confused as to when it has the affect  
that you were describing with null/init.


  Bit


Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn

On 05/09/2015 05:52 PM, Dennis Ritchie wrote:

On Saturday, 9 May 2015 at 14:15:21 UTC, Ali Çehreli wrote:

On 05/09/2015 04:59 AM, Dennis Ritchie wrote:

On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:

assert((function int(int
x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);


Thanks. Yes, it is similar to what I wanted :)


Also interesting:

  http://rosettacode.org/wiki/Y_combinator#D

I think that code was improved by Timon Gehr as well.

Ali


Yes, it's much better.


Well, it is much slower due to all the allocated closures, owed to the 
fact that the implementations of 'fix' on that page are expected to 
mirror a particular famous implementation in untyped lambda calculus.


In case you have a use for 'fix', a more efficient implementation might be:

auto fix(S,T...)(S delegate(T) delegate (S delegate(T)) f){
S delegate(T) g=(T a){ assert(0,"f is too eager."); };
return g=f((T a)=>g(a));
}

(In particular, this will only allocate two closures for the plumbing 
instead of a number of them linear in the number of recursive invocations.)




Even something like Common Lisp.


(Be aware that Common Lisp implementations typically have better garbage 
collectors than what is available for D.)


Re: how does 'shared' affect member variables?

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

On Saturday, 9 May 2015 at 19:59:58 UTC, tcak wrote:
Stupidly, shared variables' value cannot be increased/decreased 
directly. Compiler says it is deprecated, and tells me to use 
core.atomic.atomicop. You will see this as well.


How's that stupid? Sounds like the compiler is doing its job 
guarding you from races.


Hey compiler! I know 100% that no other thing will be touching 
this variable.


Informing the compiler about this is done by casting shared away.


Re: how does 'shared' affect member variables?

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

On Sat, 09 May 2015 15:38:05 -0400, Mike  wrote:


On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:

Also, I wasn't able to find any thorough documentation on shared, so if  
someone has a link, that would be helpful.




Here are a few interesting links:

Iain Buclaw (lead developer for GDC) with his interpretation:
http://forum.dlang.org/post/mailman.739.1431034764.4581.digitalmar...@puremagic.com

Andrei Alexandrescu highlighting a critical flaw with `shared`
http://forum.dlang.org/post/lruc3n$at1$1...@digitalmars.com

"The truth about shared"
http://p0nce.github.io/d-idioms/#The-truth-about-shared

Interesting deprecation warning in latest compiler (See compiler output):
http://goo.gl/EGvK72

But I don't know what the semantics are *supposed* to be, and I get the  
impression noone else knows either.  I'll be watching this thread myself  
to see if someone can provide some insight.


Mike


So it seems that although it's not properly implemented, it's still not  
completely benign, right?


I am trying to create a shared queue/array of delegates that run on the  
main thread. I don't know if D's 'shared' semantics will affect it or not,  
and whether or not casting to/from shared will cause problems with 'cas'  
or 'atomicStore'


Would the following code work as expected?

A simplified example:

struct SpinLock {
private int _lock = 0;

void lock() {
while(!cas(cast(shared(int)*)&_lock, 0, 1)) {}
}
void unlock() {
atomicStore(*cast(shared(int)*)&_lock, 0);
}
}

struct LockGuard(T) {
private T* _lock;

this(ref T lock) {
_lock = &lock;
_lock.lock();
}
~this() {
_lock.unlock();
}
}

class App {
public:
@property public static App instance() {
return _instance;
}
this() {
assert(!_instance);
_instance = this;
}
~this() {
_instance = null;
}

void run(void delegate() dg) {
auto lk = LockGuard!SpinLock(_lock);
_actions.insertBack(dg);
}
void update()
{
auto lk = LockGuard!SpinLock(_lock);

foreach(act; _actions)
act();

_actions.clear();
}
package:
__gshared App _instance = null;

SpinLock _lock;
Array!(void delegate()) _actions;
}


Thread1:
App.instance.run({ doSomething1(); });

Thread2:
App.instance.run({ doSomething2(); });

Main Thread:
App app = new MyAppType();
while(true) {
app.update();
}


Thanks,
  Bit


Re: how does 'shared' affect member variables?

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

On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:

What does 'shared' do to member variables?


Makes them `shared`. :P

It makes sense to me to put it on a global variable, but what 
sense does it make putting it on a member of a class?


Globals are not the only way to pass data to other threads. E.g., 
you can std.concurrency.send a shared Object:



import core.thread: thread_joinAll;
import std.concurrency;
class C {int x;}
void main()
{
auto c = new shared C;
c.x = 1;
auto tid = spawn(() {
receive(
(shared C c) {c.x = 2;}
);
});
send(tid, c);
thread_joinAll();
assert(c.x == 2);
}


That shared C could come from a class/struct member, of course.

What happens if you try to access a member of a class/struct 
instance from another thread that is not marked 'shared'?


I think you're not supposed to be able to do that.


Re: how does 'shared' affect member variables?

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

On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:

What does 'shared' do to member variables?

It makes sense to me to put it on a global variable, but what 
sense does it make putting it on a member of a class? What 
happens if you try to access a member of a class/struct 
instance from another thread that is not marked 'shared'?


Also, I wasn't able to find any thorough documentation on 
shared, so if someone has a link, that would be helpful.


 Thanks
   Bit


If a variable/class/struct etc is not shared, for variables and 
struct, you find their initial value. For class, you get null.


For first timers (I started using shared keyword more than 2 
years ago), do not forget that: a shared method is all about 
saying that this method is defined for shared object. So, do not 
get confused. It happened to me a lot.


Bad part of shared is that, you will be repeating it again, and 
again, and again, and again, everywhere. So, try to be patient if 
you are going to be using it for a long time.


Stupidly, shared variables' value cannot be increased/decreased 
directly. Compiler says it is deprecated, and tells me to use 
core.atomic.atomicop. You will see this as well. Hey compiler! I 
know 100% that no other thing will be touching this variable.


Re: how does 'shared' affect member variables?

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

On Saturday, 9 May 2015 at 18:41:59 UTC, bitwise wrote:

Also, I wasn't able to find any thorough documentation on 
shared, so if someone has a link, that would be helpful.




Here are a few interesting links:

Iain Buclaw (lead developer for GDC) with his interpretation:
http://forum.dlang.org/post/mailman.739.1431034764.4581.digitalmar...@puremagic.com

Andrei Alexandrescu highlighting a critical flaw with `shared`
http://forum.dlang.org/post/lruc3n$at1$1...@digitalmars.com

"The truth about shared"
http://p0nce.github.io/d-idioms/#The-truth-about-shared

Interesting deprecation warning in latest compiler (See compiler 
output):

http://goo.gl/EGvK72

But I don't know what the semantics are *supposed* to be, and I 
get the impression noone else knows either.  I'll be watching 
this thread myself to see if someone can provide some insight.


Mike


how does 'shared' affect member variables?

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

What does 'shared' do to member variables?

It makes sense to me to put it on a global variable, but what sense does  
it make putting it on a member of a class? What happens if you try to  
access a member of a class/struct instance from another thread that is not  
marked 'shared'?


Also, I wasn't able to find any thorough documentation on shared, so if  
someone has a link, that would be helpful.


 Thanks
   Bit


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 13:01:27 UTC, wobbles wrote:

On Saturday, 9 May 2015 at 13:00:01 UTC, wobbles wrote:

On Saturday, 9 May 2015 at 12:48:16 UTC, Kagamin wrote:

On Saturday, 9 May 2015 at 12:26:58 UTC, wobbles wrote:
What I mean is, if the cmd.exe hasnt flushed it's output, my 
cmdPid.stdout.readln (or whatever) will block until it does. 
I dont really want this.


Are you sure cmd is the culprit? It should have sensible 
buffering. Also do you want just a console window or also a 
command interpreter attached to it?


My windows knowledge isnt marvelous, but I believe I'll need 
the interpreter attached.


Just as an example of running cmd through std.process, running 
this on my system:

auto pipes = pipeShell("cmd.exe");
write(pipes.stdout.readln);
write(pipes.stdout.readln);
write(pipes.stdout.readln);
   return;
will print
`
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

`
and then exits.

However, adding another "write" line before the return; will 
cause the program to hang there, waiting for the cmd.exe 
process to flush it's next line.


On Linux, I'm able to edit a file descriptor after I've 
created it to tell it to read/write asynchronously, I cant 
seem to find anything similar on windows however.


Spoke too soon. Looks like this is what I need:
http://www.codeproject.com/Articles/534/An-Introduction-to-Processes-Asynchronous-Process


You need a loop that run until the PID is invalid. something like:

---
while(true)
{
Thread.sleep(dur!"msecs"(10));

// operation on i/o streams
write(pipes.stdout.readln);

if (pipes.pid.tryWait.terminated)
break;
}
---

also note that the piped process needs its whole output to be 
read before it terminates, otherwise it can stick (never ends).


Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn

On 05/09/2015 10:45 AM, Russel Winder via Digitalmars-d-learn wrote:

On Sat, 2015-05-09 at 09:49 -0700, Ali Çehreli via Digitalmars-d-learn wrote:



[…]

BigInt factorial(size_t n)
{
  return bigInts(1).take(n).reduce!((a, b) => a *= b);
}


I wonder if that should be a * b rather than a *= b?


Yes. :) Luckily, the difference is an unused side-effect to parameter 
'a' in my wrong version.


Ali



Re: Lambda functions in D

2015-05-09 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2015-05-09 at 09:49 -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> 
[…]
> BigInt factorial(size_t n)
> {
>  return bigInts(1).take(n).reduce!((a, b) => a *= b);
> }

I wonder if that should be a * b rather than a *= b?

It turns out that 2.067 fixes the integrality of BigInts so:

reduce!"a * b"(one, iota(BigInt(one), n + one));

works fine – one is immutable(BigInt(1)).

Many interesting performance issues around using take versus iota.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder




signature.asc
Description: This is a digitally signed message part


Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn

On 05/09/2015 07:47 AM, Russel Winder via Digitalmars-d-learn wrote:

> Of course none of the implementation can calculate factorial(24) as
> they are using hardware values which are bounded and cannot store
> reasonable numbers.
>
> Could use iota. Oh no we can't as BigNums are not integral.

I don't have experience with BigInt but the following worked:

import std.stdio;
import std.bigint;
import std.range;
import std.algorithm;

struct BigIntRange
{
BigInt front;

enum empty = false;

void popFront()
{
++front;
}
}

BigIntRange bigInts(long first = 0)
{
return BigIntRange(BigInt(first));
}

BigInt factorial(size_t n)
{
return bigInts(1).take(n).reduce!((a, b) => a *= b);
}

void main()
{
writeln(factorial(1000));// prints many digits
}

Ali



Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Saturday, 9 May 2015 at 14:15:21 UTC, Ali Çehreli wrote:

On 05/09/2015 04:59 AM, Dennis Ritchie wrote:

On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:

assert((function int(int
x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);


Thanks. Yes, it is similar to what I wanted :)


Also interesting:

  http://rosettacode.org/wiki/Y_combinator#D

I think that code was improved by Timon Gehr as well.

Ali


Yes, it's much better. Even something like Common Lisp.


Re: Lambda functions in D

2015-05-09 Thread John Colvin via Digitalmars-d-learn

On Saturday, 9 May 2015 at 14:47:21 UTC, Russel Winder wrote:
On Sat, 2015-05-09 at 07:15 -0700, Ali Çehreli via 
Digitalmars-d-learn wrote:

On 05/09/2015 04:59 AM, Dennis Ritchie wrote:
> On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:
> > assert((function int(int
> > x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);
> 
> Thanks. Yes, it is similar to what I wanted :)


Also interesting:

   http://rosettacode.org/wiki/Y_combinator#D

I think that code was improved by Timon Gehr as well.

Ali


Sadly all the solutions are unsound since they are recursive 
but not

tail recursive. Oh it doesn't matter as D doesn't have tail call
optimization.

There are lots of good imperative implementations.

Of course none of the implementation can calculate 
factorial(24) as
they are using hardware values which are bounded and cannot 
store

reasonable numbers.

Could use iota. Oh no we can't as BigNums are not integral.


you could probably use sequence, or even recurrence.


Re: Lambda functions in D

2015-05-09 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2015-05-09 at 07:15 -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> On 05/09/2015 04:59 AM, Dennis Ritchie wrote:
> > On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:
> > > assert((function int(int
> > > x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);
> > 
> > Thanks. Yes, it is similar to what I wanted :)
> 
> Also interesting:
> 
>http://rosettacode.org/wiki/Y_combinator#D
> 
> I think that code was improved by Timon Gehr as well.
> 
> Ali

Sadly all the solutions are unsound since they are recursive but not
tail recursive. Oh it doesn't matter as D doesn't have tail call
optimization.

There are lots of good imperative implementations.

Of course none of the implementation can calculate factorial(24) as
they are using hardware values which are bounded and cannot store
reasonable numbers.

Could use iota. Oh no we can't as BigNums are not integral.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part


Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn

On 05/09/2015 04:59 AM, Dennis Ritchie wrote:

On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:

assert((function int(int
x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);


Thanks. Yes, it is similar to what I wanted :)


Also interesting:

  http://rosettacode.org/wiki/Y_combinator#D

I think that code was improved by Timon Gehr as well.

Ali




Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 13:00:01 UTC, wobbles wrote:

On Saturday, 9 May 2015 at 12:48:16 UTC, Kagamin wrote:

On Saturday, 9 May 2015 at 12:26:58 UTC, wobbles wrote:
What I mean is, if the cmd.exe hasnt flushed it's output, my 
cmdPid.stdout.readln (or whatever) will block until it does. 
I dont really want this.


Are you sure cmd is the culprit? It should have sensible 
buffering. Also do you want just a console window or also a 
command interpreter attached to it?


My windows knowledge isnt marvelous, but I believe I'll need 
the interpreter attached.


Just as an example of running cmd through std.process, running 
this on my system:

auto pipes = pipeShell("cmd.exe");
write(pipes.stdout.readln);
write(pipes.stdout.readln);
write(pipes.stdout.readln);
return;
will print
`
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

`
and then exits.

However, adding another "write" line before the return; will 
cause the program to hang there, waiting for the cmd.exe 
process to flush it's next line.


On Linux, I'm able to edit a file descriptor after I've created 
it to tell it to read/write asynchronously, I cant seem to find 
anything similar on windows however.


Spoke too soon. Looks like this is what I need:
http://www.codeproject.com/Articles/534/An-Introduction-to-Processes-Asynchronous-Process


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 12:48:16 UTC, Kagamin wrote:

On Saturday, 9 May 2015 at 12:26:58 UTC, wobbles wrote:
What I mean is, if the cmd.exe hasnt flushed it's output, my 
cmdPid.stdout.readln (or whatever) will block until it does. I 
dont really want this.


Are you sure cmd is the culprit? It should have sensible 
buffering. Also do you want just a console window or also a 
command interpreter attached to it?


My windows knowledge isnt marvelous, but I believe I'll need the 
interpreter attached.


Just as an example of running cmd through std.process, running 
this on my system:

auto pipes = pipeShell("cmd.exe");
write(pipes.stdout.readln);
write(pipes.stdout.readln);
write(pipes.stdout.readln);
return;
will print
`
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

`
and then exits.

However, adding another "write" line before the return; will 
cause the program to hang there, waiting for the cmd.exe process 
to flush it's next line.


On Linux, I'm able to edit a file descriptor after I've created 
it to tell it to read/write asynchronously, I cant seem to find 
anything similar on windows however.


Re: Safe Usage of Mutable Ranges in foreach scopes

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

On Friday, 8 May 2015 at 11:25:26 UTC, Per Nordlöw wrote:

Could the scope keyword be used here?

Could the work done in DIP-25 be reused here, Walter?


I had `scope!(const ...)` in my original proposal [1] to handle 
exactly this problem. The latest iteration doesn't have it as an 
explicit annotation anymore, but the functionality is still there 
in the way it interacts with `@safe` [2].


It's no longer opt-in, because it turned out that `byLine` is 
just a special case of a more general problem. This became clear 
during the discussion of RCArray/DIP25 [3].


[1] 
http://wiki.dlang.org/User:Schuetzm/scope#scope.21.28const_29
[2] 
http://wiki.dlang.org/User:Schuetzm/scope3#.40safe-ty_violations_with_borrowing
[3] 
http://forum.dlang.org/thread/huspgmeupgobjubts...@forum.dlang.org


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 12:26:58 UTC, wobbles wrote:
What I mean is, if the cmd.exe hasnt flushed it's output, my 
cmdPid.stdout.readln (or whatever) will block until it does. I 
dont really want this.


Are you sure cmd is the culprit? It should have sensible 
buffering. Also do you want just a console window or also a 
command interpreter attached to it?


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 12:25:32 UTC, wobbles wrote:

On Saturday, 9 May 2015 at 12:16:52 UTC, Rikki Cattermole wrote:

On 10/05/2015 12:13 a.m., wobbles wrote:
This isn't specifically a D question, but seeing as it's for 
a D library

I figure it can go here :)

On Windows, I want to be able to spawn a console and then 
interact with
its stdin/out asynchronously, similar to how forkpty [1] 
works on linux.


I'm improving my dexpect library [2] to work with windows 
machines and
this bit has me stumped. There doesnt seem to be much info 
about it that

I can find (though my google-fu mightn't be good enough!!)

I'm sure theres someone here who knows something?

Thanks!

[1] http://linux.die.net/man/3/forkpty
[2] https://github.com/grogancolin/dexpect


Did you try creating a new process which is cmd?
Because std.process should be able to handle the IO part.


I have, but they all block i/o and so I cant continually read 
from its output :/


What I mean is, if the cmd.exe hasnt flushed it's output, my 
cmdPid.stdout.readln (or whatever) will block until it does. I 
dont really want this.


I guess I need to make another thread to do this so I wont block 
the main thread?


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On Saturday, 9 May 2015 at 12:16:52 UTC, Rikki Cattermole wrote:

On 10/05/2015 12:13 a.m., wobbles wrote:
This isn't specifically a D question, but seeing as it's for a 
D library

I figure it can go here :)

On Windows, I want to be able to spawn a console and then 
interact with
its stdin/out asynchronously, similar to how forkpty [1] works 
on linux.


I'm improving my dexpect library [2] to work with windows 
machines and
this bit has me stumped. There doesnt seem to be much info 
about it that

I can find (though my google-fu mightn't be good enough!!)

I'm sure theres someone here who knows something?

Thanks!

[1] http://linux.die.net/man/3/forkpty
[2] https://github.com/grogancolin/dexpect


Did you try creating a new process which is cmd?
Because std.process should be able to handle the IO part.


I have, but they all block i/o and so I cant continually read 
from its output :/


Re: Spawning a console in Windows (similar to forkpty on linux)

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

On 10/05/2015 12:13 a.m., wobbles wrote:

This isn't specifically a D question, but seeing as it's for a D library
I figure it can go here :)

On Windows, I want to be able to spawn a console and then interact with
its stdin/out asynchronously, similar to how forkpty [1] works on linux.

I'm improving my dexpect library [2] to work with windows machines and
this bit has me stumped. There doesnt seem to be much info about it that
I can find (though my google-fu mightn't be good enough!!)

I'm sure theres someone here who knows something?

Thanks!

[1] http://linux.die.net/man/3/forkpty
[2] https://github.com/grogancolin/dexpect


Did you try creating a new process which is cmd?
Because std.process should be able to handle the IO part.


Spawning a console in Windows (similar to forkpty on linux)

2015-05-09 Thread wobbles via Digitalmars-d-learn
This isn't specifically a D question, but seeing as it's for a D 
library I figure it can go here :)


On Windows, I want to be able to spawn a console and then 
interact with its stdin/out asynchronously, similar to how 
forkpty [1] works on linux.


I'm improving my dexpect library [2] to work with windows 
machines and this bit has me stumped. There doesnt seem to be 
much info about it that I can find (though my google-fu mightn't 
be good enough!!)


I'm sure theres someone here who knows something?

Thanks!

[1] http://linux.die.net/man/3/forkpty
[2] https://github.com/grogancolin/dexpect


Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote:
assert((function int(int 
x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);


Thanks. Yes, it is similar to what I wanted :)


Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn

On 05/09/2015 01:20 PM, Dennis Ritchie wrote:

Hi,
Can lambda functions or delegates in D to call themselves?
Can I write something like this:

-
import std.stdio;

void main() {

 auto fact = function (int x) => x * { if (x) fact(x - 1); };

 assert(fact(10) == 3628800);
}


assert((function int(int x)=>x?x*__traits(parent,{})(x-1):1)(10)==3628800);


Re: Lambda functions in D

2015-05-09 Thread Manfred Nowak via Digitalmars-d-learn

Dennis Ritchie wrote:

auto fact = function (int x) => x * { if (x) fact(x - 1); };


int fact (int x) { return x * ( x>1 ? fact(x - 1): 1); };

-manfred


Re: Lambda functions in D

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

On Saturday, 9 May 2015 at 11:20:10 UTC, Dennis Ritchie wrote:

Hi,
Can lambda functions or delegates in D to call themselves?
Can I write something like this:

-
import std.stdio;

void main() {

auto fact = function (int x) => x * { if (x) fact(x - 1); };

assert(fact(10) == 3628800);
}


dmd says no.


Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn

Hi,
Can lambda functions or delegates in D to call themselves?
Can I write something like this:

-
import std.stdio;

void main() {

auto fact = function (int x) => x * { if (x) fact(x - 1); };

assert(fact(10) == 3628800);
}


Re: Multiple template alias parameters

2015-05-09 Thread Artur Skawina via Digitalmars-d-learn
On 05/08/15 23:56, Brian Schott via Digitalmars-d-learn wrote:
> On Friday, 8 May 2015 at 12:44:31 UTC, Artur Skawina wrote:
>> On 05/08/15 03:53, Brian Schott via Digitalmars-d-learn wrote:
>>> The problem occurs when I want to register multiple modules to scan for 
>>> functions. The grammar does not allow this syntax:
>>>
>>> ```
>>> template (alias Modules ...) {
>>> ...
>>> ```
>>
>> The grammar allows omitting the 'alias' keyword.
>>
>> artur
> 
> alias parameters are different from normal template parameters. They're not 
> necessary for this problem, but they are for others.

I was trying to hint at the fact that D's template tuple parameters
already have the required magic.
Hence, the trailing '...' makes that 'alias' unnecessary.

> As an example:
> 
> 
> void traceVar(alias var, size_t line = __LINE__, string file = __FILE__)()
> {
> import std.stdio: stderr;
> stderr.writeln(file, "(", line, ") ", var.stringof, ": ", var);
> }
> 
> This allows you to print a variable's name and value by only passing the 
> variable once as a template argument. Allowing "template Tem(alias Args ...)" 
> syntax would let me trace multiple variables at once.
> 
> If you omit "alias", "var.stringof" evaluates to "var" instead of its name in 
> the calling context.

   template traceVar(VARS...) {
  void traceVar(size_t line = __LINE__, string file = __FILE__)() {
 import std.stdio: stderr;
 foreach (I, ref var; VARS)
stderr.writeln(file, "(", line, ") ", VARS[I].stringof, ": ", var);
  }
   }

artur



Re: mscoff x86 invalid pointers

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

On Saturday, 9 May 2015 at 06:21:11 UTC, extrawurst wrote:

On Saturday, 9 May 2015 at 00:16:28 UTC, Etienne wrote:
I'm trying to compile a library that I think used to work with 
-m32mscoff flag before I reset my machine configurations.


https://github.com/etcimon/memutils

Whenever I run `dub test --config=32mscoff` it gives me an 
assertion failure, which is a global variable that already has 
a pointer value for some reason..


I'm wondering if someone here could test this out on their 
machine with v2.067.1? There's no reason why this shouldn't 
work, it runs fine in DMD32/optlink  and DMD64/mscoff, just 
not in DMD32/mscoff. Thanks!


you can always use travis-ci to do such a job for you ;)


doesn't -m32mscoff recquire phobos to be compiled as COFF too ? I 
think that travis uses the official releases (win32 releases have 
phobos as OMF) so he can't run the unittests like that...


The dark side of the story is that you have to recompile phobos 
by hand with -m32mscoff...I'm not even sure that there is a 
option for this in the win32.mak...




Re: DMD phobos built with contracts check for win?

2015-05-09 Thread Rainer Schuetze via Digitalmars-d-learn



On 05.05.2015 02:03, Dzugaru wrote:

I have to compile it myself from sources or is it available somewhere?
Was playing with fibers using VisualD + DMD and lack of contract
checking (for example call() on fiber in state TERM) leads to bizarre
crashes :(


The latest (beta) version of Visual D comes with a new linker option 
"build and use local phobos library" that builds phobos with the options 
of the current project and links against it.


https://github.com/D-Programming-Language/visuald/releases