Re: Is there an efficient byte buffer queue?

2018-10-10 Thread Dukc via Digitalmars-d-learn

On Monday, 8 October 2018 at 10:31:33 UTC, Nicholas Wilson wrote:


Try searching for "circular buffer". I'm sure 
http://code.dlang.org/packages/iopipe has them in some form but 
I can't find them with a cursory search.


https://github.com/dlang-community/containers/blob/master/src/containers/cyclicbuffer.d


Re: Why are 2-D arrays reversed?

2018-10-10 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, October 10, 2018 4:26:40 PM MDT Chris Katko via Digitalmars-d-
learn wrote:
> On Wednesday, 10 October 2018 at 16:00:42 UTC, Steven
>
> Schveighoffer wrote:
> > On 10/10/18 9:22 AM, Chris Katko wrote:
> >> int[][] data =
> >>
> >>  [
> >>  [1, 0, 1, 0, 0],
> >>  [1, 0, 1, 0, 0],
> >>  [1, 0, 1, 1, 1],
> >>  [1, 0, 0, 1, 0],
> >>  [5, 1, 1, 1, 0]
> >>  ];
> >>
> >> when drawn with data[i][j], prints the transpose of "data":
> >>
> >> [1, 1, 1, 1, 5]
> >> [0, 0, 0, 0, 1]
> >> [1, 1, 1, 0, 1]
> >> [0, 0, 1, 1, 1]
> >> [0, 0, 1, 0, 0]
> >>
> >> So, if I flip [i][j] and print a row of "j's", it'll be
> >> correct. It's very confusing and counter-intuitive to have to
> >> remember to swap i and j every time I use an array.
> >>
> >> I guess when I load data from files, the i/j are already
> >> swapped and stay consistent, but when using an array in source
> >> code, they have to be flipped.
> >
> > I'm not sure what code you are using, but it prints out just
> > fine for me:
> >
> > https://run.dlang.io/is/hrA0tj
> >
> > -Steve
>
> Ah, here's a simple example:
>
> int[][] data3 =
>   [
>   [1, 0, 1, 0, 0],
>   [1, 0, 1, 0, 0],
>   [1, 0, 1, 1, 1],
>   [1, 0, 0, 1, 0],
>   [5, 1, 1, 1, 0]
>   ];
>
>   for(int i = 0; i < 5; i++)
>   {
>   for(int j = 0; j < 5; j++)
>   {
>   write(data4[i][j]," ");
>   }
>   writeln();
>   }
>
>1 0 1 0 0
>1 0 1 0 0
>1 0 1 1 1
>1 0 0 1 0
>5 1 1 1 0
>
> I have to draw j's first. I have to iterate through the
> "y"/columns/j to get the the "x's" first.
>
> I mean, I guess it makes sense if the outer-most array indexer
> refers to the inner-most "element".
>
> Wait, this IS the same as C, isn't it? So maybe this is just a
> "new" problem for me since I rarely-if-ever use hardcoded
> arrays...
>
> Maybe my brain is just melting.

The situation is bascially the same as C, though trying to make the type
syntax more consistent by putting it all on the left-hand side of the
variable has made things more confusing. The types read basically the same
as in C. The primary difference is that in D, the static array sizes go with
the type and not the variable name, which means that they're on the
left-hand side rather than the right. So, you get

int[5][4] arr;

instead of

int arr[4][5];

In both cases, the type is read outward from the variable name (which is
basically how the compiler reads types in both C and D, though understanding
that is more critical in C because of C's function pointer syntax). It's
just that in the D case, this then seems backwards, because when you then
index the array, the left index has $ of 4 and the right has $ of 5, which
is the reverse of the order in the declaration, whereas in C, the order
matches, because the sizes went on the right-hand side. One sort of
consistency was traded for another. With dynamic arrays, the sizes are
always given on the right-hand side, so you don't get that reversal. So,
what D does is reasonably consistent and matches C in terms of how types are
read, but it does end up being annoyingly hard to wrap your head around,
because the result is that the sizes of multi-dimensional, static arrays are
then declared in the opposite order from the order that they're accessed in.
If it weren't for the fact that D does out-of-bounds checking for all array
accesses in @safe code or when -release is not used, it would likely to be a
huge problem with such arrays in practice. Fortunately, since the checks are
almost always in place, the inevitable mistakes are usually quickly caught.

- Jonathan M Davis





Re: static foreach

2018-10-10 Thread Neia Neutuladh via Digitalmars-d-learn
On 10/10/2018 04:03 PM, James Japherson wrote:> Says that it cannot 
interpret X(the class that contains the static> opApply).
It's a bit hard to diagnose the problem you're getting using that 
function when we don't have the code that uses it. Or the context that's 
referenced with the foreach loop there. You might want to post a larger 
segment of code and the entire error message.


The error I'm seeing that might be related is: opApply is a static 
member of the type. So part of the function (after mixins) reads:


dg(X.opApply);

This attempts to call X.opApply with no arguments and pass the result to 
dg. But X.opApply requires an argument.


You probably want to add a `is(typeof(mixin("X." ~ m)) == cPiece)` in there.



static foreach

2018-10-10 Thread James Japherson via Digitalmars-d-learn
I have a static opApply in a class and I need to iterate over all 
the members statically using foreach


// Iterates over all members
static int opApply(int delegate(cPiece) dg)
{
alias T = typeof(this);
foreach (m; __traits(derivedMembers, T))
{
import std.traits : hasStaticMember;
static if (hasStaticMember!(T, m))
{
mixin("dg(X."~m~");");

}
}
return 0;

}


This is not a problem using foreach.


But when I use static foreach it fails!!!

Says that it cannot interpret X(the class that contains the 
static opApply).


the opApply is static, it does everything at compile time, so why 
can't the foreach be statically used?


The whole point of using opApply was so that I wouldn't have to 
write all that code each time and I could just iterate over class 
members when I need to.




Using . notation abstractly

2018-10-10 Thread James Japherson via Digitalmars-d-learn



struct Dispatcher(Base...)
{
static auto opDispatch(string name, T...)(T vals)
{
pragma(msg, " - ", name);


return Dispatcher!(Base, name).init;

}

}
struct Dispatch
{
alias X = Dispatcher!void;
alias X this;
}

pragma(msg, Dispatch.A.B.C, " - ", typeof(Dispatch.A.B.C));


CT Output:

 - A
 - B
 - C
Dispatcher() - Dispatcher!(void, "A", "B", "C")



The idea is one can do

Dispatch.Left.Right.A.B.C;

and it will sequentially handle all the indirections regardless 
of how many and return an object that encodes the path. (which 
then can be collapsed down to a single string).


Ultimately some action can then be taken on the path.

The point of all this is because D does not allow nesting of enums

which allows for nice use of . to separate hiearchies:

enum A
{
   enum B
   {
  X,
   }
}

A.B.X, rather than having to have one large flat enum and do 
things like A_B_X.


I know one can use structs but it is messy and not general enough.

Dispatch.A.B.X

can be used such as

struct A
{
alias Dispatch this;
}

A.B.X

but it requires more machinery to setup.

The purpose is mainly to be able to hijack the `.` notation to 
create nice separations visually like we do for indirection but 
allow it to mean many different things.


One of the problems is connecting it with actual code that does 
something depending on the path in a way that is general enough 
to be used for a wide variety of problems.


Any ideas on how this could be done?









Re: Why are 2-D arrays reversed?

2018-10-10 Thread Chris Katko via Digitalmars-d-learn
On Wednesday, 10 October 2018 at 16:00:42 UTC, Steven 
Schveighoffer wrote:

On 10/10/18 9:22 AM, Chris Katko wrote:

int[][] data =
 [
     [1, 0, 1, 0, 0],
     [1, 0, 1, 0, 0],
     [1, 0, 1, 1, 1],
     [1, 0, 0, 1, 0],
     [5, 1, 1, 1, 0]
 ];

when drawn with data[i][j], prints the transpose of "data":

[1, 1, 1, 1, 5]
[0, 0, 0, 0, 1]
[1, 1, 1, 0, 1]
[0, 0, 1, 1, 1]
[0, 0, 1, 0, 0]

So, if I flip [i][j] and print a row of "j's", it'll be 
correct. It's very confusing and counter-intuitive to have to 
remember to swap i and j every time I use an array.


I guess when I load data from files, the i/j are already 
swapped and stay consistent, but when using an array in source 
code, they have to be flipped.


I'm not sure what code you are using, but it prints out just 
fine for me:


https://run.dlang.io/is/hrA0tj

-Steve


Ah, here's a simple example:

int[][] data3 =
[
[1, 0, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 1, 0],
[5, 1, 1, 1, 0]
];

for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
write(data4[i][j]," ");
}
writeln();
}

  1 0 1 0 0
  1 0 1 0 0
  1 0 1 1 1
  1 0 0 1 0
  5 1 1 1 0

I have to draw j's first. I have to iterate through the 
"y"/columns/j to get the the "x's" first.


I mean, I guess it makes sense if the outer-most array indexer 
refers to the inner-most "element".


Wait, this IS the same as C, isn't it? So maybe this is just a 
"new" problem for me since I rarely-if-ever use hardcoded 
arrays...


Maybe my brain is just melting.


Re: Copy parameters from function passed as an alias

2018-10-10 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 12:10:06 UTC, bauss wrote:

On Wednesday, 10 October 2018 at 08:16:11 UTC, Kagamin wrote:
How do you want to use parameter names of an arbitrary 
function?


What I want to do is pass a function to a template and that 
template creates a function with the same parameters as the 
function passed to it, if it wasn't clear.


I can already do that if you see my examples, BUT it's not 
pretty.


I suppose the question is, why is your first example inadequate? 
In other words, why is it important that the parameters of the 
generated function have the same names as the original parameters?


Re: typo mapNode[6]* exits; instead of mapNode*[6] exits; but whats it mean ?

2018-10-10 Thread Codifies via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 13:36:20 UTC, Simen Kjærås wrote:

mapNode[6]* can be read right-to-left as 'a pointer to an array


right... hence the failed attempt at an array copy... now I 
understand...


Re: Why are 2-D arrays reversed?

2018-10-10 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/10/18 9:22 AM, Chris Katko wrote:

int[][] data =
 [
     [1, 0, 1, 0, 0],
     [1, 0, 1, 0, 0],
     [1, 0, 1, 1, 1],
     [1, 0, 0, 1, 0],
     [5, 1, 1, 1, 0]
 ];

when drawn with data[i][j], prints the transpose of "data":

[1, 1, 1, 1, 5]
[0, 0, 0, 0, 1]
[1, 1, 1, 0, 1]
[0, 0, 1, 1, 1]
[0, 0, 1, 0, 0]

So, if I flip [i][j] and print a row of "j's", it'll be correct. It's 
very confusing and counter-intuitive to have to remember to swap i and j 
every time I use an array.


I guess when I load data from files, the i/j are already swapped and 
stay consistent, but when using an array in source code, they have to be 
flipped.


I'm not sure what code you are using, but it prints out just fine for me:

https://run.dlang.io/is/hrA0tj

-Steve


Re: Home Brew dmd update 2.082

2018-10-10 Thread aliak via Digitalmars-d-learn

On Sunday, 7 October 2018 at 13:24:39 UTC, Joel wrote:
It's been a over a month, and there still isn't an update for 
dmd 2.082 on macOS using Home Brew?!


I get this (with 'brew upgrade dmd'):
Error: dmd 2.081.2 already installed


Yeah... :(

https://github.com/Homebrew/homebrew-core/blob/master/Formula/dmd.rb


Re: typo mapNode[6]* exits; instead of mapNode*[6] exits; but whats it mean ?

2018-10-10 Thread Simen Kjærås via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 13:24:42 UTC, Codifies wrote:
I'm not sure I understand what mapNode[6]* means! (the second 
version is what I wanted an array of 6 pointers)


oddly when assigning a null to one element of the array it 
cause an error as it was trying to do an array copy... so 
what's going on and what does that definition actually mean ?


mapNode[6]* can be read right-to-left as 'a pointer to an array 
of 6 mapNodes'.


For simplicity, let's use int instead of mapNode:

unittest {
int[6]* p;
int[6] arr;
p = 
(*p)[0] = 1;
(*p)[1] = 2;
(*p)[2] = 3;
(*p)[3] = 4;
(*p)[4] = 5;
(*p)[5] = 6;
assert(arr == [1,2,3,4,5,6]);
}

--
  Simen


Re: Why are 2-D arrays reversed?

2018-10-10 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 13:22:41 UTC, Chris Katko wrote:

int[][] data =
[
[1, 0, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 1, 0],
[5, 1, 1, 1, 0]
];

when drawn with data[i][j], prints the transpose of "data":

[1, 1, 1, 1, 5]
[0, 0, 0, 0, 1]
[1, 1, 1, 0, 1]
[0, 0, 1, 1, 1]
[0, 0, 1, 0, 0]

So, if I flip [i][j] and print a row of "j's", it'll be 
correct. It's very confusing and counter-intuitive to have to 
remember to swap i and j every time I use an array.


I guess when I load data from files, the i/j are already 
swapped and stay consistent, but when using an array in source 
code, they have to be flipped.


What you actually have there is an array of arrays, or a jagged 
array, and not a 2D, or multidimensional, array. If you dig into 
it, it's consistent with how array syntax works with all types. 
Consider the basics:


int i;
int[] intArray;

The type of `i` is `int`. If I want an array of ints, I put the 
brackets next to the type.


The type of `intArray` is `int[]`, but it contains elements of 
type `int`. We can visualize it with parens:


(int)[];

This indicates that `int` is the element type. We can apply the 
same rules to int[][]:


int[] singleArr;// type is int[], element type int
int[][] doubleArr;  // type is int[][], element type ?

Again, using parens we cans the element type of `doubleArr`:

(int[])[];

So singleArr[0] returns the first `int`, and doubleArr[][0] 
returns the first int[]. Consistent.


So the syntax you are used to in C or C++ is /backwards/ in D.








typo mapNode[6]* exits; instead of mapNode*[6] exits; but whats it mean ?

2018-10-10 Thread Codifies via Digitalmars-d-learn
I'm not sure I understand what mapNode[6]* means! (the second 
version is what I wanted an array of 6 pointers)


oddly when assigning a null to one element of the array it cause 
an error as it was trying to do an array copy... so what's going 
on and what does that definition actually mean ?


Why are 2-D arrays reversed?

2018-10-10 Thread Chris Katko via Digitalmars-d-learn

int[][] data =
[
[1, 0, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 0, 0, 1, 0],
[5, 1, 1, 1, 0]
];

when drawn with data[i][j], prints the transpose of "data":

[1, 1, 1, 1, 5]
[0, 0, 0, 0, 1]
[1, 1, 1, 0, 1]
[0, 0, 1, 1, 1]
[0, 0, 1, 0, 0]

So, if I flip [i][j] and print a row of "j's", it'll be correct. 
It's very confusing and counter-intuitive to have to remember to 
swap i and j every time I use an array.


I guess when I load data from files, the i/j are already swapped 
and stay consistent, but when using an array in source code, they 
have to be flipped.


Re: Copy parameters from function passed as an alias

2018-10-10 Thread bauss via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 08:16:11 UTC, Kagamin wrote:

How do you want to use parameter names of an arbitrary function?


What I want to do is pass a function to a template and that 
template creates a function with the same parameters as the 
function passed to it, if it wasn't clear.


I can already do that if you see my examples, BUT it's not pretty.


Re: Process Pipes

2018-10-10 Thread Colin via Digitalmars-d-learn

On Tuesday, 9 October 2018 at 09:15:09 UTC, Gorker wrote:

Hi all,

I'm on macOS 10.11.6 with dmd 2.081.2 and I've a problem with 
std.process.


---
gork ():foo gorker$ gcc -c -Iinclude -o foo.cpp.o src/foo.cpp
In file included from src/foo.cpp:2:
include/foo/foo.hpp:22:10: warning: scoped enumerations are a 
C++11 extension [-Wc++11-extensions]

enum class foo_event_type_t
 ^


56 warnings and 9 errors generated.
---
No output, (zero bytes) in stdout.

If I use standard process to collect both stream with:
---
auto processPipes = pipeProcess(args, Redirect.all, null, 
Config.none, workDir);
foreach (c; pipes.stdout.byChunk(100)) writeln(cast(string) c); 
// <<< it halts here: stdout file is empty, but not EOF

foreach (c; pipes.stderr.byChunk(100)) writeln(cast(string) c);
---
Everything is fine if I don't redirect the stderr to a pipe.

Suggestions?


If you want to use asynch io - you'll have to fork and use 
NONBLOCK output on the file descriptor.


I wrote this years ago (not sure if it still compiles tbh, but it 
should)


https://github.com/grogancolin/dexpect

Specifically this block should help you: 
https://github.com/grogancolin/dexpect/blob/master/source/dexpect.d#L343-L352


Re: Process Pipes

2018-10-10 Thread Danny Arends via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 09:16:43 UTC, Gorker wrote:

On Wednesday, 10 October 2018 at 08:31:36 UTC, Kagamin wrote:
Maybe read them with parallelism? 
http://dpldocs.info/experimental-docs/std.parallelism.parallel.2.html


thanks, but I'd rather avoid having to use threads just for 
this reason.

some other suggestion?


This might be a way to do it under Linux / Posix systems (it 
reads byte by byte), adapted from my web server which uses it to 
read the output from php/perl/cgi scripts:


https://github.com/DannyArends/DaNode/blob/master/danode/process.d

It uses fcntl to make sure the pipe is non-blocking
you can read 1 byte from the stdout pipe, then 1 byte from stderr 
pipe


bool nonblocking(ref File file) {
  version(Posix) {
return(fcntl(fileno(file.getFP()), F_SETFL, O_NONBLOCK) != 
-1);

  }else{
return(false);
  }
}

int readpipe(ref Pipe pipe, int verbose = NORMAL){
  File fp = pipe.readEnd;
  try{
if(fp.isOpen()){
  if(!nonblocking(fp) && verbose >= DEBUG) writeln("[WARN]   
unable to create nonblocking pipe for command");

  return(fgetc(fp.getFP()));
}
  }catch(Exception e){
writefln("[WARN]   Exception during readpipe command: %s", 
e.msg);

fp.close();
  }
  return(EOF);
}


pStdIn = File(inputfile, "r");
pStdOut = pipe();
pStdErr = pipe();
auto cpid = spawnShell(command, pStdIn, pStdOut.writeEnd, 
pStdErr.writeEnd, null);

while(true){
  ch = readpipe(pStdOut);
  outbuffer.put(cast(char)ch);
  ch = readpipe(pStdErr);
  errbuffer.put(cast(char)ch);
}

Hope this works for your usecase


Re: Process Pipes

2018-10-10 Thread Gorker via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 08:31:36 UTC, Kagamin wrote:
Maybe read them with parallelism? 
http://dpldocs.info/experimental-docs/std.parallelism.parallel.2.html


thanks, but I'd rather avoid having to use threads just for this 
reason.

some other suggestion?



Re: Process Pipes

2018-10-10 Thread Kagamin via Digitalmars-d-learn
Maybe read them with parallelism? 
http://dpldocs.info/experimental-docs/std.parallelism.parallel.2.html


Re: Can i watch folders/files for changes with D language?

2018-10-10 Thread Jacob Carlborg via Digitalmars-d-learn

On 2018-10-09 19:20, Ephrahim wrote:

I intend to make this a command line tool for a start, but i'll need a 
GUI sooner or later. So i also need help pointing me to the best 
available GUI library D has.


It's difficult to specifying a single best GUI library. But I'll mention 
DWT [1] [2]. It has a native look and feel and works on Windows and 
Linux (GTK).


[1] http://code.dlang.org/packages/dwt
[2] https://github.com/d-widget-toolkit/dwt

--
/Jacob Carlborg


Re: Copy parameters from function passed as an alias

2018-10-10 Thread Kagamin via Digitalmars-d-learn

How do you want to use parameter names of an arbitrary function?


Re: Process Pipes

2018-10-10 Thread Gorker via Digitalmars-d-learn

On Wednesday, 10 October 2018 at 08:02:29 UTC, Kagamin wrote:
stderr buffer is full (it's about 8kb or so) and gcc waits when 
you read from it.


Thank you for your kind reply,

How to just try to read from stdout (not blocking), and then try 
to read from stderr (not blocking)?

I mean, how to check both pipes for data?

As an alternative, how to raise the 8kb limit?




Re: Process Pipes

2018-10-10 Thread Kagamin via Digitalmars-d-learn
stderr buffer is full (it's about 8kb or so) and gcc waits when 
you read from it.


Re: Can i watch folders/files for changes with D language?

2018-10-10 Thread Russel Winder via Digitalmars-d-learn
On Tue, 2018-10-09 at 17:20 +, Ephrahim via Digitalmars-d-learn
wrote:
> Hi Everyone, i'm planning on developing a software for 
> synchronizing folder contents across multiple computers. The 
> software will evolve very quickly into virtual Remote Desktop 
> Access system.
> So i've been considering C++ for this, its the best in the market 
> of all things fast at the moment (Considering the availability of 
> tools and docs). But i hate just two things in the language, 
> (.cpp + .h files) and no "easy" centralized package manager.
> So i'm definitely considering D and Python now, but D seems to 
> satisfy my needs as per this software. I've developed very good 
> custom server for hardware before, and it performed very well.
> So i need your help guys, can you point me to any documentation 
> or libraries i can use to watch files/folders for changes 
> (delete, edit, new content)?

Conan is the up and coming centralised package management system for
C++.

C++ may, or may not, get modules (and so remove the .cpp/.h split)
sometime before the heat death of the universe.

You should add Rust to the list of options I believe.

Not sure whether Go should be added as well, probably Yes.

Python, D, Rust, and Go, all have bindings the the various file
watching facilities in the various operating systems.
 
> I intend to make this a command line tool for a start, but i'll 
> need a GUI sooner or later. So i also need help pointing me to 
> the best available GUI library D has.

It is really a question of which GUI framework you want to use. This is
a situation where a bit of forward planning will help avoid having to
reimplement or hack in silly ways.

The two obvious candidates are Qt and GTK if you want to be cross-
platform. This then leads to the question which programming languages
have good bindings to the frameworks. I am more or less in the GTK camp
now having more or less given up on Qt, however Qt remains a very good
choice.

D, Python, and Rust have excellent bindings to GTK. Go has a good
binding. C++ binding to GTK is not bad, but I prefer the D and Rust
ones.

Python has an excellent binding to Qt (use PySide2). Obviously C++ has
an excellent binding to Qt since Qt is a C++ library. I have not
properly investigated D and Rust bindings to Qt. The Big Question™ is
whether to use QML or not. I get the feeling the answer is yes you
probably should. The Python and Go binding for using QML are good, I
haven't tried D, Rust, or C++.

[…]

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



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