Re: Does GtkD work well on Windows?

2016-03-24 Thread Web Biz Owner via Digitalmars-d-learn

On Thursday, 24 March 2016 at 23:08:13 UTC, Zardoz wrote:

On Thursday, 24 March 2016 at 21:00:36 UTC, Web Biz Owner wrote:

[...]


Short answer : Yes, but you need to install (or package with) 
Gtk dlls.


Long answer :

Install following this instructions : 
https://github.com/gtkd-developers/GtkD/wiki/Installing-on-Windows
If you would use Dub and you need to be cross-platform, the 
dub.sdl/json would be a bit more tricky, but works. You would 
need something like this :


  libs "gtkd" platform="windows"

  configuration "nogtk" {
platforms "windows"
targetType "executable"
  }
  configuration "gtk" {
platforms "posix"
dependency "gtk-d:gtkd" version="~>3.2.0"
targetType "executable"
  }



Thank you. Will try those steps.


Re: Strange behavior in console with UTF-8

2016-03-24 Thread Jonathan Villa via Digitalmars-d-learn

On Friday, 25 March 2016 at 01:03:06 UTC, Ali Çehreli wrote:

>
Try char:

char[] readerBuffer;

Ali


Also tried with dchar ... there's no changes.


Re: Strange behavior in console with UTF-8

2016-03-24 Thread Jonathan Villa via Digitalmars-d-learn

On Friday, 25 March 2016 at 01:03:06 UTC, Ali Çehreli wrote:

On 03/24/2016 05:54 PM, Jonathan Villa wrote:

Try char:

char[] readerBuffer;




flush() has no effect on input streams.

Ali


Thankf fot he quick reply.
Unfortunately it behaves exactly as before with wchar.


Re: Strange behavior in console with UTF-8

2016-03-24 Thread Ali Çehreli via Digitalmars-d-learn

On 03/24/2016 05:54 PM, Jonathan Villa wrote:

> I'm using WCHAR instead of CHAR
> with the hope to get less problems in the future.

Try char:

char[] readerBuffer;

> Also I tried stdin.flush()

flush() has no effect on input streams.

Ali



Strange behavior in console with UTF-8

2016-03-24 Thread Jonathan Villa via Digitalmars-d-learn
I prefer to post this thing here because it could that I'm doing 
something wrong.


I'm using std.stdio -> readln() to read whatever I'm typing in 
the console.
BUT, if the line contains some UTF-8 characters, the data 
obtained is EMPTY and




module runnable;

import std.stdio;
import std.string : chomp;
import std.experimental.logger;

void doSomethingElse(wchar[] data)
{
writeln("hello!");
}

int main(string[] args)
{
/* Some fix I found to fix UTF-8 related problems, I'm using 
Windows 10 */

version(Windows)
{
import core.sys.windows.windows;
if (SetConsoleCP(65001) == 0)
throw new Exception("failure");
if (SetConsoleOutputCP(65001) == 0)
throw new Exception("failure");
}
FileLogger fl = new FileLogger("log.log");
wchar[] readerBuffer;

readln(readerBuffer);
readerBuffer = chomp(readerBuffer);

fl.info(readerBuffer.length); /* <- if the readed string 
contains at least one UTF-8
char this prints 0, else 
it prints its length

   */

if (readerBuffer != "exit"w)
doSomethingElse(readerBuffer);

/* Also, all the following code doesn't run as expected, the 
program doesn't wait for
   you, it executes readln() even without pressing/sending a 
key */

readln(readerBuffer);
fl.info(readerBuffer.length);
readln(readerBuffer);
fl.info(readerBuffer.length);
readln(readerBuffer);
fl.info(readerBuffer.length);
readln(readerBuffer);
fl.info(readerBuffer.length);
readln(readerBuffer);
fl.info(readerBuffer.length);

return 0;
}

The real code is bigger but this describes the bug. Also, if it 
needs to print UTF-8 there's no problem.


My main problem is that the line is gonna be sended through a TCP 
socket and I wanna make it work with UTF-8. I'm using WCHAR 
instead of CHAR with the hope to get less problems in the future.


I you comment the fixed Windows code, the program crashes
http://prntscr.com/ajmy14

Also I tried stdin.flush() right after the first readln() but 
nothing seems to fix it.


I'm doing something wrong?
many thanks.


Re: Does GtkD work well on Windows?

2016-03-24 Thread Zardoz via Digitalmars-d-learn

On Thursday, 24 March 2016 at 21:00:36 UTC, Web Biz Owner wrote:

Hi group,

I'm interested in developing some GUI programs in D on Windows. 
Looked into the various D GUI toolkit options a bit, on the D 
sites and by googling a bit. Thinking of using GtkD. So would 
like to know whether people think it is a good option for GUI 
app dev on Windows.


Note: I have not ruled out other toolkits. If anyone has any 
thoughts on their use, please do comment.


Thanks,
W.B.O.


Short answer : Yes, but you need to install (or package with) Gtk 
dlls.


Long answer :

Install following this instructions : 
https://github.com/gtkd-developers/GtkD/wiki/Installing-on-Windows
If you would use Dub and you need to be cross-platform, the 
dub.sdl/json would be a bit more tricky, but works. You would 
need something like this :


  libs "gtkd" platform="windows"

  configuration "nogtk" {
platforms "windows"
targetType "executable"
  }
  configuration "gtk" {
platforms "posix"
dependency "gtk-d:gtkd" version="~>3.2.0"
targetType "executable"
  }



Re: Updating D-based apps without recompiling it

2016-03-24 Thread cym13 via Digitalmars-d-learn

On Thursday, 24 March 2016 at 18:46:43 UTC, Jesse Phillips wrote:
On Wednesday, 23 March 2016 at 17:29:50 UTC, Jacob Carlborg 
wrote:

On 2016-03-23 18:15, Jesse Phillips wrote:

Do you have an example of this being done in any other 
language?


In Erlang it's possible to hot swap code. I'm not sure how it 
works though. But if we're talking servers, the easiest is to 
have multiple instances and restart one at the time with the 
new code.


Looks like it is doable because it is byte code: 
http://erlang.org/doc/reference_manual/code_loading.html#id88478


My recommendation would have been using a DLL, but that was 
already excluded as an option.


It can be done in compiled languages too, here an example of a 
library that allows it in C http://kitsune-dsu.com/


Does GtkD work well on Windows?

2016-03-24 Thread Web Biz Owner via Digitalmars-d-learn

Hi group,

I'm interested in developing some GUI programs in D on Windows. 
Looked into the various D GUI toolkit options a bit, on the D 
sites and by googling a bit. Thinking of using GtkD. So would 
like to know whether people think it is a good option for GUI app 
dev on Windows.


Note: I have not ruled out other toolkits. If anyone has any 
thoughts on their use, please do comment.


Thanks,
W.B.O.



Re: Checking if a port is listening

2016-03-24 Thread Lucien via Digitalmars-d-learn

On Thursday, 24 March 2016 at 12:17:35 UTC, Marc Schütz wrote:

On Wednesday, 23 March 2016 at 21:37:09 UTC, Lucien wrote:
When I remove the Thread.sleep, it doesn't find all adresses. 
Why ?


Socket.select() will wait _at most_ 100 msecs. If a socket gets 
ready before that timeout, it will return immediately. 
Therefore, you might not get the full TIMES*100 msecs, and some 
hosts might not have responded in time.


If I change Thread.sleep(100.msecs) to Thread.sleep(6.seconds), 
all hosts respond. What am I doing false ? I don't understand..





Re: Updating D-based apps without recompiling it

2016-03-24 Thread Jesse Phillips via Digitalmars-d-learn

On Wednesday, 23 March 2016 at 17:29:50 UTC, Jacob Carlborg wrote:

On 2016-03-23 18:15, Jesse Phillips wrote:

Do you have an example of this being done in any other 
language?


In Erlang it's possible to hot swap code. I'm not sure how it 
works though. But if we're talking servers, the easiest is to 
have multiple instances and restart one at the time with the 
new code.


Looks like it is doable because it is byte code: 
http://erlang.org/doc/reference_manual/code_loading.html#id88478


My recommendation would have been using a DLL, but that was 
already excluded as an option.


Re: Struct array assignment behaviour using example from Programming in D, chapter 78

2016-03-24 Thread Ali Çehreli via Digitalmars-d-learn

On 03/24/2016 10:24 AM, data pulverizer wrote:
> I have been playing with the matrix example given at the end of chapter
> 78 of Ali Çehreli's

For reference, it's "Multi-dimensional operator overloading example" here:

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

>having problems with overloading the opAssign operator.
>
> rows is a private int[][] in a Matrix struct.
>
> I have added the following ...
>
> Matrix opAssign(int[][] arr)
> {
>  this.rows = arr;
>  // rows = arr // does not work either ...
>  return this;
> }
>
> However this does not work (no error occurs, it just doesn't do
> anything)

How are you testing it? The following worked for me:

1) Added that opAssign() to the struct. (Verified that it gets called.)

2) Tested with the following code:

auto m2 = Matrix();
auto rows = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ];
m2 = rows;

writeln(m2);

(I've tested with a dynamically generated 'rows' as well.)

Ali



Re: Struct array assignment behaviour using example from Programming in D, chapter 78

2016-03-24 Thread data pulverizer via Digitalmars-d-learn

On Thursday, 24 March 2016 at 17:24:38 UTC, data pulverizer wrote:
I have been playing with the matrix example given at the end of 
chapter 78 of Ali Çehreli's fabulous book and am having 
problems with overloading the opAssign operator.


rows is a private int[][] in a Matrix struct.

I have added the following ...

Matrix opAssign(int[][] arr)
{
this.rows = arr;
// rows = arr // does not work either ...
return this;
}

However this does not work (no error occurs, it just doesn't do 
anything) but this does work ...


Matrix opAssign(int[][] arr)
{
foreach(i, row; rows){
row[] = arr[i];
}
return this;
}

The second is not efficient since it has to loop to assign the 
array. Is there a more efficient way of overwriting the array?


Sorry. Please disregard. I'll drive home and ask this question 
properly!


Re: Usage of custom class with JSONValue

2016-03-24 Thread Uldis via Digitalmars-d-learn

On Thursday, 24 March 2016 at 17:03:25 UTC, Andre wrote:
I hoped there is some operator overloading for implicit 
conversion of my

class to JSONValue.
I solved the issue with an toJSON method and a generic 
functionality which

checks for this method.

Kind regards
André



Vibe.d has some serialization with toJson(), fromJson() methods:
http://vibed.org/api/vibe.data.json/serializeToJson
Maybe you can work with that?


Re: Containers, Allocators and Purity

2016-03-24 Thread ZombineDev via Digitalmars-d-learn

On Thursday, 24 March 2016 at 11:18:06 UTC, Nordlöw wrote:
Could somebody briefly outline how the thread-locality 
(non-GC-locked) of allocators  relates to the purity of the 
containers using them?


This because I want to move forward with optimizations in my 
knowledge graph that requires GC-free array containers storing 
value typed elements (integers) which preferrably has pure API.


Specifically, I want to use something like

https://github.com/economicmodeling/containers/blob/master/src/containers/dynamicarray.d

that is `@safe pure` and uses lock-free allocations in a 
heavily multi-threaded application.


If I want purity aswell which `std.experimental.allocators` are 
possible?


After a quick glance at: 
https://github.com/economicmodeling/containers/blob/master/src/containers/dynamicarray.d


It looks like if `useGC == false`, all of the methods should be 
`pure`-ready, except for `remove(size_t)` and `~this()`. These 
two methods use `typeid.destroy`, which I don't think is `pure`.


Struct array assignment behaviour using example from Programming in D, chapter 78

2016-03-24 Thread data pulverizer via Digitalmars-d-learn
I have been playing with the matrix example given at the end of 
chapter 78 of Ali Çehreli's fabulous book and am having problems 
with overloading the opAssign operator.


rows is a private int[][] in a Matrix struct.

I have added the following ...

Matrix opAssign(int[][] arr)
{
this.rows = arr;
// rows = arr // does not work either ...
return this;
}

However this does not work (no error occurs, it just doesn't do 
anything) but this does work ...


Matrix opAssign(int[][] arr)
{
foreach(i, row; rows){
row[] = arr[i];
}
return this;
}

The second is not efficient since it has to loop to assign the 
array. Is there a more efficient way of overwriting the array?




Re: Containers, Allocators and Purity

2016-03-24 Thread ZombineDev via Digitalmars-d-learn

On Thursday, 24 March 2016 at 11:18:06 UTC, Nordlöw wrote:
Could somebody briefly outline how the thread-locality 
(non-GC-locked) of allocators  relates to the purity of the 
containers using them?


This because I want to move forward with optimizations in my 
knowledge graph that requires GC-free array containers storing 
value typed elements (integers) which preferrably has pure API.


Specifically, I want to use something like

https://github.com/economicmodeling/containers/blob/master/src/containers/dynamicarray.d

that is `@safe pure` and uses lock-free allocations in a 
heavily multi-threaded application.


If I want purity aswell which `std.experimental.allocators` are 
possible?


Currently almost nothing in `std.experimental.allocators` is 
explicitly marked as pure, except for some of the things in 
./common.d 
(https://github.com/D-Programming-Language/phobos/pull/3957 
*shameless plug*). Thankfully make, makeArray, dispose and some 
of the allocators are templates, so you can rely on attribute 
inference.


The most important thing is that you need to have pure `allocate` 
and `deallocate` methods. After this `make` and friends will work 
like magic (at least for types with pure `this` and `~this`). 
`pure` statefull allocator essentially means that it has to be 
thread-local, so there are no global side-effects. Essentially, 
to achieve this you can make a large initial allocation in each 
thread (even from a non-pure allocator) and then you can make 
`pure` sub-allocations out of it.


You can read more about my adventures with my `pure` smart 
pointer here:

http://forum.dlang.org/post/eegjluaiwvdxfnbxk...@forum.dlang.org
http://forum.dlang.org/post/bvgyrfvuqrqcyvhkq...@forum.dlang.org


Re: Usage of custom class with JSONValue

2016-03-24 Thread Andre via Digitalmars-d-learn
On Thursday, 24 March 2016 at 16:03:13 UTC, Edwin van Leeuwen 
wrote:

On Thursday, 24 March 2016 at 11:39:13 UTC, arturg wrote:

isnt alias this supposed to do this implicitly?

convert this
auto jsValue = JSONValue(new MyClass());

into this
auto jsValue = JSONValue((new MyClass())._data);


Good point, I did not catch that. That indeed should work and 
seems to be a bug. Does it work if _data is a base type (string 
or int, etc..)


Thanks for the answers. I also wonder why alias this does not the 
job.
Unfortunatelly even if it works, the _data is not the data I want 
to be used
as value for JSONValue. _data only contains keys, the values are 
stored

in another variable. JSONValue should contain both.

I hoped there is some operator overloading for implicit 
conversion of my

class to JSONValue.
I solved the issue with an toJSON method and a generic 
functionality which

checks for this method.

Kind regards
André


Re: Usage of custom class with JSONValue

2016-03-24 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Thursday, 24 March 2016 at 11:39:13 UTC, arturg wrote:

isnt alias this supposed to do this implicitly?

convert this
auto jsValue = JSONValue(new MyClass());

into this
auto jsValue = JSONValue((new MyClass())._data);


Good point, I did not catch that. That indeed should work and 
seems to be a bug. Does it work if _data is a base type (string 
or int, etc..)




Re: getOverloads, but also include all the imported members

2016-03-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 24 March 2016 at 15:07:09 UTC, Yuxuan Shui wrote:

Is there a way to do this automatically?


No. You have to decide to bring them together if you want them to 
overload.


Re: getOverloads, but also include all the imported members

2016-03-24 Thread Yuxuan Shui via Digitalmars-d-learn

On Thursday, 24 March 2016 at 13:55:31 UTC, Adam D. Ruppe wrote:

On Thursday, 24 March 2016 at 12:11:33 UTC, Marc Schütz wrote:

On Wednesday, 23 March 2016 at 20:54:20 UTC, Yuxuan Shui wrote:

module one;
void func(int a){}
/
module two;
import one;
void func(float a){}


Add in module two:

alias func = one.func;


Indeed, the two funcs are NOT overloaded right now unless you 
add that alias. See : http://dlang.org/hijack.html for details.


Is there a way to do this automatically?


Re: inout and templates don't mix...

2016-03-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/23/16 6:25 PM, Ali Çehreli wrote:

On 03/23/2016 02:31 PM, cy wrote:

 > struct Someop(Type) {
 >Type thing;
 >void foo() {
 >  thing.bar();
 >}
 > }
 >
 > struct Foo {
 >void bar() {
 >  import std.stdio: writeln;
 >  writeln("bar");
 >}
 > }
 >
 > struct Bar {
 >void thingy(inout(Foo) foo) inout {
 >  auto op = Someop(foo);

The following is a workaround for this example:

   auto op = Someop!Foo(foo);


I would be surprised if the original code worked regardless of inout 
status. Struct implicit ctors are not IFTI-compatible.


Note what is happening here in your update is likely not what the OP 
expects. Since Foo contains no indirections, it is a value type. So it 
can be implicitly cast from inout to mutable (same thing with Someop). 
As soon as you involve a pointer here, it would stop compiling.




I'm not sure whether Someop's implicit constructor should take part in
deducing Someop's Type template parameter.


There is no deduction.

However, you can specify that you are explicitly constructing an inout 
Someop:


auto op = inout(Someop!Foo)(foo);

However, this will not allow Bar to compile, since it calls op.foo, and 
Someop.foo is not inout, neither is Foo.bar, so neither can be called.


In this case, you need to mark Foo.bar and Someop.foo as inout. Marking 
Foo.bar as inout is probably acceptable, but marking Someop.foo as inout 
is tricky, since it's a wrapper, and can't predict what Type is going to 
allow.


This is one of the viral situations that frankly affects all the 
mutability modifiers.


-Steve


Re: parsing fastq files with D

2016-03-24 Thread eastanon via Digitalmars-d-learn

On Thursday, 24 March 2016 at 13:38:32 UTC, Marc Schütz wrote:
Yes, it's read into your processes memory. You can use 
std.mmfile [1] to make things a bit more efficient. It will, 
too, read the data into memory, but it will do so in a way 
(memory mapping) that only loads what is actually accessed 
(everything in your case), and that allows the operating system 
to efficiently release and reload parts of it if memory runs 
low.


Unfortunately there is no example in the documentation, but it 
works like this (untested):


import std.mmfile;
auto file = new MmFile(inputfile);
string text = cast(string) file[];
...

[1] http://dlang.org/phobos/std_mmfile.html


That is very clever. Thank you for the tip and I have implemented 
it and it works.  I feel like it is a safer way of reading a file.


Re: getOverloads, but also include all the imported members

2016-03-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 24 March 2016 at 12:11:33 UTC, Marc Schütz wrote:

On Wednesday, 23 March 2016 at 20:54:20 UTC, Yuxuan Shui wrote:

module one;
void func(int a){}
/
module two;
import one;
void func(float a){}


Add in module two:

alias func = one.func;


Indeed, the two funcs are NOT overloaded right now unless you add 
that alias. See : http://dlang.org/hijack.html for details.


Re: parsing fastq files with D

2016-03-24 Thread Marc Schütz via Digitalmars-d-learn

On Thursday, 24 March 2016 at 08:24:15 UTC, eastanon wrote:
On Thursday, 24 March 2016 at 06:34:51 UTC, rikki cattermole 
wrote:

As a little fun thing to do I implemented it for you.

It won't allocate. Making this perfect for you.
With a bit of work you could make Result have buffers for 
result instead of using the input array allow for the source 
to be an input range itself.


I made this up on dpaste and single quotes were not playing 
nicely there. So you'll see "\r"[0] as a workaround.


Thank you very much. I think you have exposed me to  a number 
of new concepts that I will go through and annotate the code 
with.  I read all input from file as follows.


string text = cast(string)std.file.read(inputfile);
foreach(record;FastQRecord.parse(text)){
   writeln(record);
}

Does this mean that text is allocated to 
memory? and is there a better way to read and process the 
inputfile? 


Yes, it's read into your processes memory. You can use std.mmfile 
[1] to make things a bit more efficient. It will, too, read the 
data into memory, but it will do so in a way (memory mapping) 
that only loads what is actually accessed (everything in your 
case), and that allows the operating system to efficiently 
release and reload parts of it if memory runs low.


Unfortunately there is no example in the documentation, but it 
works like this (untested):


import std.mmfile;
auto file = new MmFile(inputfile);
string text = cast(string) file[];
...

[1] http://dlang.org/phobos/std_mmfile.html


Re: Checking if a port is listening

2016-03-24 Thread Marc Schütz via Digitalmars-d-learn

On Wednesday, 23 March 2016 at 21:37:09 UTC, Lucien wrote:
When I remove the Thread.sleep, it doesn't find all adresses. 
Why ?


Socket.select() will wait _at most_ 100 msecs. If a socket gets 
ready before that timeout, it will return immediately. Therefore, 
you might not get the full TIMES*100 msecs, and some hosts might 
not have responded in time.


Re: getOverloads, but also include all the imported members

2016-03-24 Thread Marc Schütz via Digitalmars-d-learn

On Wednesday, 23 March 2016 at 20:54:20 UTC, Yuxuan Shui wrote:

Say:

module one;
void func(int a){}

/

module two;
import one;
void func(float a){}

Is there a way to get both func() in module two?


Add in module two:

alias func = one.func;


Re: Usage of custom class with JSONValue

2016-03-24 Thread arturg via Digitalmars-d-learn
On Thursday, 24 March 2016 at 08:24:46 UTC, Edwin van Leeuwen 
wrote:
JSONValue only works with the build in types, not with user 
defined types. Either you define a specific function for the 
class that returns a JSONValue. Easiest way to do that would be 
to build an associative array with strings as keys with the 
names, and JSONValues as values and turn that into JSONValue, 
i.e. (untested):

class MyClass
{
string[] _data;
alias _data this;
// ...
   JSONValue toJSON()
  {
JSONValue[string] aa;
JSONValue[] dataJSON = _data.map!((a) => 
JSONValue(a)).array;

aa["data"] = JSONValue(dataJSON);
return JSONValue(aa);
  }
}



isnt alias this supposed to do this implicitly?

convert this
auto jsValue = JSONValue(new MyClass());

into this
auto jsValue = JSONValue((new MyClass())._data);


Containers, Allocators and Purity

2016-03-24 Thread Nordlöw via Digitalmars-d-learn
Could somebody briefly outline how the thread-locality 
(non-GC-locked) of allocators  relates to the purity of the 
containers using them?


This because I want to move forward with optimizations in my 
knowledge graph that requires GC-free array containers storing 
value typed elements (integers) which preferrably has pure API.


Specifically, I want to use something like

https://github.com/economicmodeling/containers/blob/master/src/containers/dynamicarray.d

that is `@safe pure` and uses lock-free allocations in a heavily 
multi-threaded application.


If I want purity aswell which `std.experimental.allocators` are 
possible?


Re: Usage of custom class with JSONValue

2016-03-24 Thread Rene Zwanenburg via Digitalmars-d-learn
On Thursday, 24 March 2016 at 08:24:46 UTC, Edwin van Leeuwen 
wrote:
Alternatively there are multiple serialization libraries that 
will allow you to turn any user defined type from and to 
JSONValues.


https://code.dlang.org/packages/painlessjson
https://code.dlang.org/packages/jsonizer

Cheers, Edwin


I don't know the above libraries so I can't comment on their 
quality, but I've used vibe.d's json module extensively and it's 
pretty solid. Vibe has also recently been split up into multiple 
subpackages, so you only have to depend on vibe-d:data and don't 
have to pull in the rest.


Re: iota result as member variable

2016-03-24 Thread Alex via Digitalmars-d-learn
On Thursday, 24 March 2016 at 08:13:27 UTC, Edwin van Leeuwen 
wrote:
Yeah this is one of the downsides of voldermort types. In these 
cases typeof and ReturnType are your friend. It often takes me 
a couple of tries to get it right, but the following seems to 
work:


import std.traits : ReturnType;
import std.range : iota;
class A
{
ReturnType!(A.testIter4) member;
auto testIter4()
{
return iota(0,5);
}
}

void main()
{

 A a = new A();
 a.member = a.testIter4();

}


Ah... thanks! The "ReturnType" is what I looked for. This also 
makes some kind of semantic statement about "how slim should be 
the interface of my class members"...


Re: parsing fastq files with D

2016-03-24 Thread rikki cattermole via Digitalmars-d-learn

On 24/03/16 9:24 PM, eastanon wrote:

On Thursday, 24 March 2016 at 06:34:51 UTC, rikki cattermole wrote:

As a little fun thing to do I implemented it for you.

It won't allocate. Making this perfect for you.
With a bit of work you could make Result have buffers for result
instead of using the input array allow for the source to be an input
range itself.

I made this up on dpaste and single quotes were not playing nicely
there. So you'll see "\r"[0] as a workaround.


Thank you very much. I think you have exposed me to  a number of new
concepts that I will go through and annotate the code with. I read all
input from file as follows.

string text = cast(string)std.file.read(inputfile);
foreach(record;FastQRecord.parse(text)){
writeln(record);
}

Does this mean that text is allocated to memory? and is
there a better way to read and process the inputfile? 


Yup, any string usage gets allocated in memory. Since the way I designed 
parse there, it won't allocate during returning of a record. Just don't 
go around modifying that memory or keeping copies (not critical but I 
wouldn't).


There are better ways to read the input file. For example byChunk would 
read it in smaller groups of say 4096 chars. But you would need to 
overload that with support for e.g. lines parsing.


The way I'd go is memory mapped files as an input range.
After all, let the OS help you out with deciding when to put the file 
and parts of it into memory.


After all, if you have 1gb files you want to parse. Could you just as 
easily have 2tb worth? Probably since it is DNA after all.

You kinda don't want that all in memory at once if you know what I mean.

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



Re: Usage of custom class with JSONValue

2016-03-24 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Thursday, 24 March 2016 at 08:15:12 UTC, Andre wrote:

Hi,

I have a class which has already an alias this to a string 
array,

so I can use it in a foreach loop.

class MyClass
{
string[] _data;
alias _data this;
// ...
}

void main()
{
import std.json;
auto jsValue = JSONValue(new MyClass());
}

For some generic code I need an implicit conversion of MyClass 
so I can
use it for a JSONValue. For the coding above I receive a 
compiler error:

static assert  "unable to convert type "MyClass" to json"



JSONValue only works with the build in types, not with user 
defined types. Either you define a specific function for the 
class that returns a JSONValue. Easiest way to do that would be 
to build an associative array with strings as keys with the 
names, and JSONValues as values and turn that into JSONValue, 
i.e. (untested):

class MyClass
{
string[] _data;
alias _data this;
// ...
   JSONValue toJSON()
  {
JSONValue[string] aa;
JSONValue[] dataJSON = _data.map!((a) => JSONValue(a)).array;
aa["data"] = JSONValue(dataJSON);
return JSONValue(aa);
  }
}

Alternatively there are multiple serialization libraries that 
will allow you to turn any user defined type from and to 
JSONValues.


https://code.dlang.org/packages/painlessjson
https://code.dlang.org/packages/jsonizer

Cheers, Edwin


Re: parsing fastq files with D

2016-03-24 Thread eastanon via Digitalmars-d-learn
On Thursday, 24 March 2016 at 06:34:51 UTC, rikki cattermole 
wrote:

As a little fun thing to do I implemented it for you.

It won't allocate. Making this perfect for you.
With a bit of work you could make Result have buffers for 
result instead of using the input array allow for the source to 
be an input range itself.


I made this up on dpaste and single quotes were not playing 
nicely there. So you'll see "\r"[0] as a workaround.


Thank you very much. I think you have exposed me to  a number of 
new concepts that I will go through and annotate the code with.  
I read all input from file as follows.


string text = cast(string)std.file.read(inputfile);
foreach(record;FastQRecord.parse(text)){
   writeln(record);
}

Does this mean that text is allocated to memory? 
and is there a better way to read and process the inputfile? 







Usage of custom class with JSONValue

2016-03-24 Thread Andre via Digitalmars-d-learn

Hi,

I have a class which has already an alias this to a string array,
so I can use it in a foreach loop.

class MyClass
{
string[] _data;
alias _data this;
// ...
}

void main()
{
import std.json;
auto jsValue = JSONValue(new MyClass());
}

For some generic code I need an implicit conversion of MyClass so 
I can
use it for a JSONValue. For the coding above I receive a compiler 
error:

static assert  "unable to convert type "MyClass" to json"

Is there anything I can do?

Kind regards
André



Re: iota result as member variable

2016-03-24 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Thursday, 24 March 2016 at 06:54:25 UTC, Alex wrote:

Hi everybody,
doing some optimization on my code, I faced some strange 
question:

how to save a iota result in a class member?

Say I have
class A
{
??? member;

auto testIter4()
{
return iota(0,5);
}
}

void main()
{
A a = new A();
a.member = testIter4();
}

how would I declare the member?



Yeah this is one of the downsides of voldermort types. In these 
cases typeof and ReturnType are your friend. It often takes me a 
couple of tries to get it right, but the following seems to work:


import std.traits : ReturnType;
import std.range : iota;
class A
{
ReturnType!(A.testIter4) member;
auto testIter4()
{
return iota(0,5);
}
}

void main()
{

 A a = new A();
 a.member = a.testIter4();

}


Re: byChunk odd behavior?

2016-03-24 Thread Hanh via Digitalmars-d-learn

On Wednesday, 23 March 2016 at 19:07:34 UTC, cym13 wrote:

In Scala, 'take' consumes bytes from the iterator. So the same 
code would be

buffer = range.take(N).toArray


Then just do that!

import std.range, std.array;
auto buffer = range.take(N).array;

auto example = iota(0, 200, 5).take(5).array;
assert(example == [0, 5, 10, 15, 20]);


Well, that's what I do in the first post but you can't call it 
twice with an InputRange.


auto buffer1 = range.take(4).array; // ok
range.popFrontN(4); // not ok
auto buffer2 = range.take(4).array; // not ok



Re: iota result as member variable

2016-03-24 Thread Alex via Digitalmars-d-learn

As a comment on my own post:
I’m aware, that there are some different return types from 
functions like iota. And I’m also aware, that there are much less 
different range types. I can, maybe, define what kind of range 
type I want to have, the question is, how to map all the 
different function results to this one interface I need with as 
less performance penalty as possible