Re: Yield from function?

2017-01-30 Thread Ali Çehreli via Digitalmars-d-learn

On 01/30/2017 08:12 PM, Profile Anaysis wrote:


import std.stdio, std.concurrency, core.thread;

class Search : Fiber
{
this() { super(); }
int res = 0;
void start()
{
Fiber.yield();
res = 1;
}
}

void main()
{

auto search = new Search();

search.call(); writeln(search.res);
search.call(); writeln(search.res);
search.call(); writeln(search.res); // crashes after 3rd call(first
two work fine)
}


That's because the fiber is not in a callable state. (You can check with 
search.state.) Here is one where the fiber function lives (too) long:


import std.stdio, std.concurrency, core.thread;

class Search : Fiber
{
this() { super(); }
int res = 0;
void start() {
while (true) {
Fiber.yield();
++res;
}
}
}

void main()
{
auto search = new Search();

foreach (i; 0 .. 5) {
search.call();
writeln(search.res);
}
}

Ali



Re: Yield from function?

2017-01-30 Thread Profile Anaysis via Digitalmars-d-learn

On Monday, 30 January 2017 at 18:48:10 UTC, Ali Çehreli wrote:

On 01/30/2017 03:03 AM, Profile Anaysis wrote:
> I need to yield from a complex recursive function too allow
visualizing
> what it is doing.
>
> e.g., if it is a tree searching algorithm, I'd like to yield
for each
> node so that the current state can be shown visually.

I used tree iteration as a Generator example here:

  http://ddili.org/ders/d.en/fibers.html

It's in the code where the function 'byNode' appears. (The 
example builds on an earlier tree iteration code in the same 
chapter.)


Ali


I tried your lambda based fib example and it works as 
expected(using a function)


But when I try the class version it crashes without a call stack 
or any info(somewhere in the fiber module and an access 
violation).


import std.stdio, std.concurrency, core.thread;

class Search : Fiber
{
this() { super(); }
int res = 0;
void start()
{   
Fiber.yield();
res = 1;
}
}

void main()
{

auto search = new Search();

search.call(); writeln(search.res);
search.call(); writeln(search.res);
	search.call(); writeln(search.res); // crashes after 3rd 
call(first two work fine)

}


Any ideas what why it is crashing like this?


Re: Yield from function?

2017-01-30 Thread Profile Anaysis via Digitalmars-d-learn

On Monday, 30 January 2017 at 22:34:11 UTC, TheFlyingFiddle wrote:
On Monday, 30 January 2017 at 11:03:52 UTC, Profile Anaysis 
wrote:
I need to yield from a complex recursive function too allow 
visualizing what it is doing.


e.g., if it is a tree searching algorithm, I'd like to yield 
for each node so that the current state can be shown visually.


I realize that there are several ways to do this but D a yield 
version without additional threads would be optimal. I don't 
need concurrency or speed, just simple.


If you don't want to use fibers then an alternative to yeilds 
is to use callbacks during iteration.


Example:
struct Tree(T)
{
   T value;
   Tree!(T)[] children;
}

void iterDepth(T)(Tree!(T) tree, void delegate(Tree!T) cb)
{
cb(tree);
foreach(child; tree.children)
{
iterDepth(child, cb);
}
}

unittest
{
auto tree = ... //Make the tree somehow
iterDepth(tree, (node)
{
writeln(node.value);
});
}

Callbacks have their set of problems but it's one of the 
simpler ways to do this.


This can't be easily because it requires the callback to contain 
the main program(if it depends on the results). Since I am 
already in a multi-threaded environment it would not be easy to 
marshal the data around.


If I run it in it's own thread then it won't block but seems like 
a lot of work for a simple thing.


Re: Yield from function?

2017-01-30 Thread TheFlyingFiddle via Digitalmars-d-learn

On Monday, 30 January 2017 at 11:03:52 UTC, Profile Anaysis wrote:
I need to yield from a complex recursive function too allow 
visualizing what it is doing.


e.g., if it is a tree searching algorithm, I'd like to yield 
for each node so that the current state can be shown visually.


I realize that there are several ways to do this but D a yield 
version without additional threads would be optimal. I don't 
need concurrency or speed, just simple.


If you don't want to use fibers then an alternative to yeilds is 
to use callbacks during iteration.


Example:
struct Tree(T)
{
   T value;
   Tree!(T)[] children;
}

void iterDepth(T)(Tree!(T) tree, void delegate(Tree!T) cb)
{
cb(tree);
foreach(child; tree.children)
{
iterDepth(child, cb);
}
}

unittest
{
auto tree = ... //Make the tree somehow
iterDepth(tree, (node)
{
writeln(node.value);
});
}

Callbacks have their set of problems but it's one of the simpler 
ways to do this.















Re: Yield from function?

2017-01-30 Thread Ali Çehreli via Digitalmars-d-learn

On 01/30/2017 02:04 PM, cym13 wrote:

On Monday, 30 January 2017 at 18:48:10 UTC, Ali Çehreli wrote:



  http://ddili.org/ders/d.en/fibers.html



BTW the alias to avoid a name conflic on "Generator" isn't necessary
anymore [...] Otherwise I believe you'll agree
that removing it would make it easier for the beginners.


Thanks for letting me know. I can't update the book to 2.073 for other 
reasons like some changes made to ddoc anyway. :-/ (And 'scope' function 
parameter, etc.)


Ali



Re: Yield from function?

2017-01-30 Thread cym13 via Digitalmars-d-learn

On Monday, 30 January 2017 at 18:48:10 UTC, Ali Çehreli wrote:

On 01/30/2017 03:03 AM, Profile Anaysis wrote:
> I need to yield from a complex recursive function too allow
visualizing
> what it is doing.
>
> e.g., if it is a tree searching algorithm, I'd like to yield
for each
> node so that the current state can be shown visually.

I used tree iteration as a Generator example here:

  http://ddili.org/ders/d.en/fibers.html

It's in the code where the function 'byNode' appears. (The 
example builds on an earlier tree iteration code in the same 
chapter.)


Ali


BTW the alias to avoid a name conflic on "Generator" isn't 
necessary anymore with the last version of phobos (as shipped 
with DMDv2.073.0). However it's still conflicting with the latest 
version of LDC available on my system (1.0.0, based on 
DMDv2.070.2) so maybe letting it be in the example a bit longer 
is appropriate. Otherwise I believe you'll agree that removing it 
would make it easier for the beginners.


Re: Why fatal log throw object.Error@(0) on console?

2017-01-30 Thread Ali Çehreli via Digitalmars-d-learn

On 01/30/2017 05:21 AM, Suliman wrote:

I found in the docs mention "If data is logged with LogLevel fatal by
default an Error will be thrown.". But what the reason of such behavior?


That likely comes from the fact that 'fatal' describes states that you 
can't trust the program to do the right thing. For example, you may not 
be able to write to a log file. Fatal means fatal. :)


Ali



Re: Yield from function?

2017-01-30 Thread Ali Çehreli via Digitalmars-d-learn

On 01/30/2017 03:03 AM, Profile Anaysis wrote:
> I need to yield from a complex recursive function too allow visualizing
> what it is doing.
>
> e.g., if it is a tree searching algorithm, I'd like to yield for each
> node so that the current state can be shown visually.

I used tree iteration as a Generator example here:

  http://ddili.org/ders/d.en/fibers.html

It's in the code where the function 'byNode' appears. (The example 
builds on an earlier tree iteration code in the same chapter.)


Ali



Re: embedding a library in Windows

2017-01-30 Thread biozic via Digitalmars-d-learn

On Monday, 30 January 2017 at 17:25:13 UTC, Nestor wrote:

On Monday, 30 January 2017 at 16:40:47 UTC, biozic wrote:
As an alternative, you could build an object file from 
Sqlite's source code (e.g. the amalgamation file from Sqlite's 
website) with a C compiler. Then you just build your D 
application with:


dmd app.d sqlite3.d sqlite3.o[bj]

No dll. Sqlite statically linked.

You could also try https://code.dlang.org/packages/d2sqlite3 
with option "--all-included". This wasn't tested much though.


I tried to compile a static library with MinGW (which is the 
one I have at hand, v4.8.1) with this command:


gcc -static -c sqlite3.c

However:

D:\prj\sqltest2\source>dmd app.d database.d sqlite.d sqlite3.o
Error: unrecognized file extension o


Sorry, I never used Mingw32. It's either an incompatible object 
file format, or dmd only accepts .obj extensions, or both...


You could also use the dmc compiler 
(http://ftp.digitalmars.com/dmc.zip). Or see kinke's answer about 
using the MS compiler.




Re: embedding a library in Windows

2017-01-30 Thread Nestor via Digitalmars-d-learn

On Monday, 30 January 2017 at 16:40:47 UTC, biozic wrote:
As an alternative, you could build an object file from Sqlite's 
source code (e.g. the amalgamation file from Sqlite's website) 
with a C compiler. Then you just build your D application with:


dmd app.d sqlite3.d sqlite3.o[bj]

No dll. Sqlite statically linked.

You could also try https://code.dlang.org/packages/d2sqlite3 
with option "--all-included". This wasn't tested much though.


I tried to compile a static library with MinGW (which is the one 
I have at hand, v4.8.1) with this command:


gcc -static -c sqlite3.c

However:

D:\prj\sqltest2\source>dmd app.d database.d sqlite.d sqlite3.o
Error: unrecognized file extension o


Re: embedding a library in Windows

2017-01-30 Thread biozic via Digitalmars-d-learn

On Monday, 30 January 2017 at 16:40:47 UTC, biozic wrote:
You could also try https://code.dlang.org/packages/d2sqlite3 
with option "--all-included". This wasn't tested much though.


Sorry, this uses a dll on Windows.



Re: embedding a library in Windows

2017-01-30 Thread biozic via Digitalmars-d-learn

On Monday, 30 January 2017 at 13:00:15 UTC, Nestor wrote:

Hi,

In Windows, is it possible embed a dll library into an 
application (in this particular case, sqlite3.dll)? Notice I 
don't mean storing the resource in the application to extract 
it at runtime, but rather to produce a static self-contained 
application.


If it's possible, please provide a brief howto.

Thanks in advance.


As an alternative, you could build an object file from Sqlite's 
source code (e.g. the amalgamation file from Sqlite's website) 
with a C compiler. Then you just build your D application with:


dmd app.d sqlite3.d sqlite3.o[bj]

No dll. Sqlite statically linked.

You could also try https://code.dlang.org/packages/d2sqlite3 with 
option "--all-included". This wasn't tested much though.


Re: embedding a library in Windows

2017-01-30 Thread kinke via Digitalmars-d-learn
You'll probably be more successful with a static .lib library 
generated with the MS compiler (Visual Studio). I'd suggest 
compiling sqlite yourself and then using DMD with the 
`-m32mscoff` switch (32-bit) or `-m64` for 64-bit. Google is your 
friend in case you don't know how to build a static C library.


Oh apparently sqlite is a single C module, in that case it's even 
easier. Just compile the .c file with the MS compiler (cl.exe 
sqlite3.c) and specify the resulting sqlite3.obj file on the DMD 
command line (with `-m32mscoff` or `-m64`), so that it's linked 
in.


Re: embedding a library in Windows

2017-01-30 Thread Kagamin via Digitalmars-d-learn

That looks compiled for linux.


Re: embedding a library in Windows

2017-01-30 Thread Nestor via Digitalmars-d-learn

On Monday, 30 January 2017 at 13:58:45 UTC, Adam D. Ruppe wrote:

On Monday, 30 January 2017 at 13:29:20 UTC, Nestor wrote:

OK, and in case I have a sqlite3.a file


Just pass the sqlite3.a file instead of sqlite3.lib and the 
compiler should do the rest... worst case is you might need to 
edit the source of my sqlite.d to comment out the pragma(lib) 
line to explicitly deny the dependency, but I think it will 
just work with the .a since it will find the functions in there.


d:\prj\sqltest2\source>dmd app.d database.d sqlite.d sqlite3.a
Error: unrecognized file extension a

I took the file from 
http://math.seattleacademy.org/andersgibbons/fall17/node_modules/sqlite3/build/Release/ so I am not 100% sure it's compatible (I don't know how to build it myself), but in any case dmd doesn't recognize the extension.


If I delete the sqlite3.lib or remove the pragma from sqlite.d 
(or both), I get this instead:


d:\prj\sqltest2\source>dmd app.d database.d sqlite.d
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_open
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_finalize
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_prepare_v2
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_free
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_mprintf
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_exec
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_last_insert_rowid
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_changes
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_errmsg
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_close
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_reset
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_blob
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_bytes
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_text
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_step
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_double
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_count
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_type
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_int
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_name
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_blob
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_null
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_double
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_int
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_text
Error: linker exited with status 214890840


Re: embedding a library in Windows

2017-01-30 Thread Kagamin via Digitalmars-d-learn

On Monday, 30 January 2017 at 13:29:20 UTC, Nestor wrote:
OK, and in case I have a sqlite3.a file, what parameters should 
I pass to dmd to build a static application?


If it's an import library, you will link against the dll 
dynamically (the library only contains bindings to dll). If it's 
a static library with sqlite code, you will link that code 
statically.


Re: embedding a library in Windows

2017-01-30 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 30 January 2017 at 13:29:20 UTC, Nestor wrote:

OK, and in case I have a sqlite3.a file


Just pass the sqlite3.a file instead of sqlite3.lib and the 
compiler should do the rest... worst case is you might need to 
edit the source of my sqlite.d to comment out the pragma(lib) 
line to explicitly deny the dependency, but I think it will just 
work with the .a since it will find the functions in there.


Re: embedding a library in Windows

2017-01-30 Thread Nestor via Digitalmars-d-learn

On Monday, 30 January 2017 at 13:22:45 UTC, Kagamin wrote:
In general case the library can depend on it being a dll, then 
it can't be linked statically.


OK, and in case I have a sqlite3.a file, what parameters should I 
pass to dmd to build a static application?


Re: embedding a library in Windows

2017-01-30 Thread Kagamin via Digitalmars-d-learn
In general case the library can depend on it being a dll, then it 
can't be linked statically.


Re: Why fatal log throw object.Error@(0) on console?

2017-01-30 Thread Suliman via Digitalmars-d-learn
I found in the docs mention "If data is logged with LogLevel 
fatal by default an Error will be thrown.". But what the reason 
of such behavior?


Re: Another bug?

2017-01-30 Thread Yuxuan Shui via Digitalmars-d-learn

On Monday, 30 January 2017 at 12:40:44 UTC, Jack Applegame wrote:

bug report: https://issues.dlang.org/show_bug.cgi?id=17128


LDC (2.070.2) has a different problem: the dtor is never called.


embedding a library in Windows

2017-01-30 Thread Nestor via Digitalmars-d-learn

Hi,

In Windows, is it possible embed a dll library into an 
application (in this particular case, sqlite3.dll)? Notice I 
don't mean storing the resource in the application to extract it 
at runtime, but rather to produce a static self-contained 
application.


If it's possible, please provide a brief howto.

Thanks in advance.


Re: D idom for removing array elements

2017-01-30 Thread ag0aep6g via Digitalmars-d-learn

On 01/30/2017 01:33 PM, albert-j wrote:

On Monday, 30 January 2017 at 12:31:33 UTC, albert-j wrote:



OK, got it. Can you do removal without reallocation with
std.container.array?

Array!int arr;
foreach (i; 0..10) arr ~= i;


Sorry, sent too early.

arr = arr[].remove!(x=> x > 5); //Doesn't work withouth calling
.Array!int


Looks like it can't be done in that straight-forward manner. But you can 
do the `remove` and then update just `arr`'s length:



void main()
{
import std.container: Array;
import std.algorithm: equal, remove;

Array!int arr;
foreach (i; 0..10) arr ~= i;
const oldAddress = [0];

arr.length = arr[].remove!(x=> x > 5).length;

assert(equal(arr[], [0, 1, 2, 3, 4, 5])); /* holds */
assert([0] is oldAddress); /* holds */
}


Maybe std.container.Array can be improved somehow to simplify this. But 
I'm not sure about the details.


Re: Another bug?

2017-01-30 Thread Jack Applegame via Digitalmars-d-learn

bug report: https://issues.dlang.org/show_bug.cgi?id=17128


Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn

On Monday, 30 January 2017 at 12:31:33 UTC, albert-j wrote:



OK, got it. Can you do removal without reallocation with 
std.container.array?


Array!int arr;
foreach (i; 0..10) arr ~= i;


Sorry, sent too early.

arr = arr[].remove!(x=> x > 5); //Doesn't work withouth 
calling .Array!int


Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn

On Monday, 30 January 2017 at 10:45:03 UTC, cym13 wrote:


Meh.

Forget that, bad memory. remove isn't working in-place. However 
slapping ".array" is still asking explicitely for reallocation, 
so just forget it. Here is a code that works:


import std.conv;
import std.stdio;
import std.format;
import std.algorithm;

void main(string[] args) {
int [] arr = [8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

auto before = arr.ptr;
arr = arr.remove!(x => x>5);
auto after  = arr.ptr;

assert(arr == [0, 1, 2, 3, 4, 5], arr.to!string);
assert(before == after, "%s %s".format(before, after));
}


OK, got it. Can you do removal without reallocation with 
std.container.array?


Array!int arr;
foreach (i; 0..10) arr ~= i;






Re: Another bug?

2017-01-30 Thread ag0aep6g via Digitalmars-d-learn

On 01/30/2017 12:55 PM, Jack Applegame wrote:

Code:

import std.stdio;
struct Foo {
int val = 0;
~this() {
writefln("destruct %s", val);
}
}

void bar(ARGS...)() {
ARGS args;
args[0].val = 1;
writefln("val = %s", args[0].val);
}

void main() {
bar!Foo();
}

Excpected output:
val = 1
destruct 1

But got:
destruct 0
val = 1

It seems that the compiler destructs 'args' immediately after
definition, not at the end of the function.


Yup, looks like a (pretty bad) bug to me.


Re: Problems compiling sqlite-d

2017-01-30 Thread Nestor via Digitalmars-d-learn

On Monday, 30 January 2017 at 03:07:22 UTC, Adam D. Ruppe wrote:

If I specify all source files, there are even more problems:
 Error 42: Symbol Undefined _sqlite3_open


It apparently couldn't find sqlite3.lib.

Files sqlite3.{def|dll|lib} are on both source/ and 
source/arsd/ (just in case)


Try specifying it on the command line too:

dmd app.d database.d sqlite.d sqlite3.lib

Though this may still require sqlite3.dll there too, unless it 
was built statically.


I found out the cause of the problem.

First I tried to verify if the linker was able to find 
sqlite3.lib using Process Monitor by Mark Russinovich, and at 
least there were IRP_MJ_CREATE, FASTIO_QUERY_INFORMATION, 
IRP_MJ_READ and FASTIO_READ operations with the correct path to 
sqlite3.lib where the result was SUCCESS, so apparently the 
linker could find the file.


So I opened sqlite3.bn in notepad++ just to see the name of the 
symbols and not even one started with an underscore, so I created 
sqlite3.lib again with these arguments and this time it compiled:


implib /system sqlite3.lib sqlite3.dll


Why fatal log throw object.Error@(0) on console?

2017-01-30 Thread Suliman via Digitalmars-d-learn

import std.stdio;
import std.experimental.logger;

void main()
{
sharedLog = new FileLogger("New_Default_Log_File.log");

fatal("Fatal error: ");
}

All other log-levels write as expected log files, but `fatal` 
throw on console:



app.exe


object.Error@(0): A fatal log message was logged

0x0041291E
0x004138E7
0x0040206C
0x0040B277
0x0040B178
0x00407AB3
0x73FC62C4 in BaseThreadInitThunk
0x77440FD9 in RtlSubscribeWnfStateChangeNotification
0x77440FA4 in RtlSubscribeWnfStateChangeNotification

It's bug normal behavoiur?




Re: Another bug?

2017-01-30 Thread Jack Applegame via Digitalmars-d-learn

WORKAROUND:

import std.stdio;

struct Foo {
int val = 0;
~this() {
writefln("destruct %s", val);
}
}

void bar(ARGS...)() {
struct Tuple {
ARGS args;
alias args this;
}
Tuple args;
args[0].val = 1;
writefln("val = %s", args[0].val);
}


void main() {
bar!Foo();
}



Another bug?

2017-01-30 Thread Jack Applegame via Digitalmars-d-learn

Code:

import std.stdio;
struct Foo {
int val = 0;
~this() {
writefln("destruct %s", val);
}
}

void bar(ARGS...)() {
ARGS args;
args[0].val = 1;
writefln("val = %s", args[0].val);
}

void main() {
bar!Foo();
}

Excpected output:
val = 1
destruct 1

But got:
destruct 0
val = 1

It seems that the compiler destructs 'args' immediately after 
definition, not at the end of the function.


Re: Bug in generator

2017-01-30 Thread ag0aep6g via Digitalmars-d-learn

On 01/30/2017 11:58 AM, Profile Anaysis wrote:

Also, if one tries to create a global generator an error about PAGESIZE
not being a compile time value is given.


That means you can't initialize it statically, because PAGESIZE is not 
known statically. But you can have a global (module scope, thread local) 
Generator. You just have to initialize it dynamically.



import std.concurrency: Generator, yield;

/* No: */
/* Generator!int g = new Generator!int({ yield(42); }); */

/* Yes: */
Generator!int g1;
void main()
{
g1 = new Generator!int({ yield(42); });
}

/* Also yes: */
Generator!int g2;
static this()
{
g2 = new Generator!int({ yield(42); });
}



Re: Bug in generator

2017-01-30 Thread ag0aep6g via Digitalmars-d-learn

On 01/30/2017 11:58 AM, Profile Anaysis wrote:

the code from https://dlang.org/library/std/concurrency/generator.html

gives a seg fault at the end.

import std.concurrency;
import std.stdio;


void main()
{
auto tid = spawn(
{
while (true)
{
writeln(receiveOnly!int());
}
});

auto r = new Generator!int(
{
foreach (i; 1 .. 10)
yield(i);
});

foreach (e; r)
{
tid.send(e);
}
}

[...]

I don't see a segfault, but an exception: 
"std.concurrency.OwnerTerminated@std/concurrency.d(241): Owner terminated".


Which makes sense because the spawned thread still tries to receive ints 
after the main thread has finished. So, the example is bad and should be 
fixed. I've filed an issue:


https://issues.dlang.org/show_bug.cgi?id=17127


Re: Yield from function?

2017-01-30 Thread pineapple via Digitalmars-d-learn

On Monday, 30 January 2017 at 11:03:52 UTC, Profile Anaysis wrote:
I need to yield from a complex recursive function too allow 
visualizing what it is doing.


e.g., if it is a tree searching algorithm, I'd like to yield 
for each node so that the current state can be shown visually.


I realize that there are several ways to do this but D a yield 
version without additional threads would be optimal. I don't 
need concurrency or speed, just simple.


You may be able to use Generator in std.concurrency for this: 
https://dlang.org/phobos/std_concurrency.html#.Generator


You could also rephrase your algorithm as a range, which would 
not involve concurrency but may be unintuitive to implement. In 
this case you would likely have to use a stack or queue rather 
than a recursive function.


Is it an option to just accumulate the information regarding your 
algorithm in a separate data structure and then analyze it after 
the algorithm is complete, or has recorded some number of entries?





Yield from function?

2017-01-30 Thread Profile Anaysis via Digitalmars-d-learn
I need to yield from a complex recursive function too allow 
visualizing what it is doing.


e.g., if it is a tree searching algorithm, I'd like to yield for 
each node so that the current state can be shown visually.


I realize that there are several ways to do this but D a yield 
version without additional threads would be optimal. I don't need 
concurrency or speed, just simple.


Bug in generator

2017-01-30 Thread Profile Anaysis via Digitalmars-d-learn
the code from 
https://dlang.org/library/std/concurrency/generator.html


gives a seg fault at the end.

import std.concurrency;
import std.stdio;


void main()
{
auto tid = spawn(
{
while (true)
{
writeln(receiveOnly!int());
}
});

auto r = new Generator!int(
{
foreach (i; 1 .. 10)
yield(i);
});

foreach (e; r)
{
tid.send(e);
}
}


0x7FF7BDDA05E5 in 
std.concurrency.receiveOnly!int.receiveOnly.__lambda3 at 
\..\..\src\phobos\std\concurrency.d(805)
0x7FF7BDDA1997 in void std.concurrency.Message.map!(pure 
@nogc @safe void 
function(std.concurrency.OwnerTerminated)*).map(pure @nogc @safe 
void function(std.concurrency.OwnerTerminated)*) at 
\..\..\src\phobos\std\concurrency.d(163)
0x7FF7BDDA0B04 in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ13onStandardMsgMFKS3std11concurrency7MessageZb
0x7FF7BDDA0EA0 in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ13onLinkDeadMsgMFKS3std11concurrency7MessageZb at \..\..\src\phobos\std\concurrency.d(1988)
0x7FF7BDDA0F4A in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ12onControlMsgMFKS3std11concurrency7MessageZb at \..\..\src\phobos\std\concurrency.d(2000)
0x7FF7BDDA0FFA in 
D3std11concurrency10MessageBox160__T3getTDFNaNbNiNfiZvTPFNaNiNfC3std11concurrency14LinkTerminatedZvTPFNaNiNfC3std11concurrency15OwnerTerminatedZvTPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ3getMFDFNaNbNiNfiZvPFNaNiNfC3std11concurrency14LinkTerminatedZvPFNaNiNfC3std11concurrency15OwnerTerminatedZvPFS3std7variant18__T8VariantNVmi20Z8VariantNZvZ4scanMFKS3std11concurrency36__T4ListTS3std11concurrency7MessageZ4ListZb at \..\..\src\phobos\std\concurrency.d(2016)
0x7FF7BDDA094C in bool std.concurrency.MessageBox.get!(pure 
nothrow @nogc @safe void delegate(int), pure @nogc @safe void 
function(std.concurrency.LinkTerminated)*, pure @nogc @safe void 
function(std.concurrency.OwnerTerminated)*, void 
function(std.variant.VariantN!(20uL).VariantN)*).get(pure nothrow 
@nogc @safe void delegate(int), pure @nogc @safe void 
function(std.concurrency.LinkTerminated)*, pure @nogc @safe void 
function(std.concurrency.OwnerTerminated)*, void 
function(std.variant.VariantN!(20uL).VariantN)*) at 
\..\..\src\phobos\std\concurrency.d(2115)
0x7FF7BDDA0579 in std.concurrency.receiveOnly!int.receiveOnly 
at \..\..\src\phobos\std\concurrency.d(806)

0x7FF7BDD9F50D in main.main.__lambda1 at main.d(82)
0x7FF7BDDA46C2 in std.concurrency._spawn!(void 
function())._spawn.exec at 
\..\..\src\phobos\std\concurrency.d(538)

0x7FF7BDDE9302 in void core.thread.Thread.run()
0x7FF7BDDD5CBD in thread_entryPoint
0x7FF7BDE5D96D in thread_start__ptr64)> at 
d:\th\minkernel\crts\ucrt\src\appcrt\startup\thread.cpp(115)

0x7FF9EB178364 in BaseThreadInitThunk
0x7FF9EBD570D1 in RtlUserThreadStart


Also, if one tries to create a global generator an error about 
PAGESIZE not being a compile time value is given.




Re: D idom for removing array elements

2017-01-30 Thread cym13 via Digitalmars-d-learn

On Monday, 30 January 2017 at 10:30:22 UTC, cym13 wrote:

On Monday, 30 January 2017 at 08:50:14 UTC, albert-j wrote:

On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote:

Removing works by overwriting the array with only the wanted 
values and discarding the rest.


But then why do I get this:

import std.stdio, std.algorithm, std.array;

int[] arr;
foreach (i; 0..10) arr ~= i; // [0, 1, 2, 3, 4, 5, 6, 7, 
8, 9]


writeln("[0]:", [0]); // prints "7F56FAE93000"

arr = arr.remove!(x => x > 5).array;

writeln("[0]:", [0]); // prints "7F56FAE92020"

writeln("arr:",arr); // prints: "[0, 1, 2, 3, 4, 5]"


It looks like arr.remove allocates new memory and copies the 
data there.


No, remove works in-place, you are the one specifically asking 
for a reallocation here: instead of


arr = arr.remove!(x => x>5).array;

write

arr.remove!(x => x>5);


Complete example:

import std.stdio;
import std.format;
import std.algorithm;

void main(string[] args) {
int [] arr = [0, 1, 2, 3, 4, 5];

auto before = arr.ptr;
arr.remove!(x => x>5);
auto after  = arr.ptr;

assert(before == after, "%s %s".format(before, after));
}


Meh.

Forget that, bad memory. remove isn't working in-place. However 
slapping ".array" is still asking explicitely for reallocation, 
so just forget it. Here is a code that works:


import std.conv;
import std.stdio;
import std.format;
import std.algorithm;

void main(string[] args) {
int [] arr = [8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

auto before = arr.ptr;
arr = arr.remove!(x => x>5);
auto after  = arr.ptr;

assert(arr == [0, 1, 2, 3, 4, 5], arr.to!string);
assert(before == after, "%s %s".format(before, after));
}



Re: D idom for removing array elements

2017-01-30 Thread cym13 via Digitalmars-d-learn

On Monday, 30 January 2017 at 08:50:14 UTC, albert-j wrote:

On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote:

Removing works by overwriting the array with only the wanted 
values and discarding the rest.


But then why do I get this:

import std.stdio, std.algorithm, std.array;

int[] arr;
foreach (i; 0..10) arr ~= i; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 
9]


writeln("[0]:", [0]); // prints "7F56FAE93000"

arr = arr.remove!(x => x > 5).array;

writeln("[0]:", [0]); // prints "7F56FAE92020"

writeln("arr:",arr); // prints: "[0, 1, 2, 3, 4, 5]"


It looks like arr.remove allocates new memory and copies the 
data there.


No, remove works in-place, you are the one specifically asking 
for a reallocation here: instead of


arr = arr.remove!(x => x>5).array;

write

arr.remove!(x => x>5);


Complete example:

import std.stdio;
import std.format;
import std.algorithm;

void main(string[] args) {
int [] arr = [0, 1, 2, 3, 4, 5];

auto before = arr.ptr;
arr.remove!(x => x>5);
auto after  = arr.ptr;

assert(before == after, "%s %s".format(before, after));
}



Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn

On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote:

Removing works by overwriting the array with only the wanted 
values and discarding the rest.


But then why do I get this:

import std.stdio, std.algorithm, std.array;

int[] arr;
foreach (i; 0..10) arr ~= i; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

writeln("[0]:", [0]); // prints "7F56FAE93000"

arr = arr.remove!(x => x > 5).array;

writeln("[0]:", [0]); // prints "7F56FAE92020"

writeln("arr:",arr); // prints: "[0, 1, 2, 3, 4, 5]"


It looks like arr.remove allocates new memory and copies the data 
there.