Re: Where can I find the DEFINITION of vibe IOMode ??

2020-09-05 Thread Andy Balba via Digitalmars-d-learn

On Saturday, 5 September 2020 at 19:22:17 UTC, drug wrote:

05.09.2020 22:17, Andy Balba пишет:
Where do I fine where IOMode is defined..  as in the last line 
of ...


module vibe.core.stream;

import vibe.internal.traits : checkInterfaceConformance, 
validateInterfaceConformance;

import vibe.internal.interfaceproxy;
import core.time;
import std.algorithm;
import std.conv;

public import eventcore.driver : IOMode;

After endless searches, I find many references to it, but 
never a DEFINITION of it





https://github.com/vibe-d/eventcore/blob/a027c233c2542de8372bbff25d0a4804f32a094e/source/eventcore/driver.d#L1087


@drug: thank you..I'm now left with resolving @blocking


Where can I find the DEFINITION of vibe IOMode ??

2020-09-05 Thread Andy Balba via Digitalmars-d-learn
Where do I fine where IOMode is defined..  as in the last line of 
...


module vibe.core.stream;

import vibe.internal.traits : checkInterfaceConformance, 
validateInterfaceConformance;

import vibe.internal.interfaceproxy;
import core.time;
import std.algorithm;
import std.conv;

public import eventcore.driver : IOMode;

After endless searches, I find many references to it, but never a 
DEFINITION of it




Re: D binary io functions

2020-08-30 Thread Andy Balba via Digitalmars-d-learn

On Sunday, 30 August 2020 at 10:02:09 UTC, Arjan wrote:

On Sunday, 30 August 2020 at 06:00:20 UTC, Andy Balba wrote:
going nuts trying to figure out which D functions will 
read/write binary files


see this blog:
http://nomad.uk.net/articles/working-with-files-in-the-d-programming-language.html


Yes, Great, many thanks! This is the kind of online docs Dlang 
needs more of.


Links for these "great docs" addressing various topics should be 
collected into a Dlang.org online file, where each "great doc" 
contains a link, followed by a brief description.


Getting good and useful, online file-io docs for C++ takes a few 
minutes, but getting equivalent useful information on Dlang 
functions took me hours, with no successful outcome.


IMHO Dlang.org online doc related to file io are too terse to be 
useful to anyone trying to learn Dlang, and are only useful to 
those that already familiar with them and who only need a "syntax 
reminder"


D binary io functions

2020-08-30 Thread Andy Balba via Digitalmars-d-learn
going nuts trying to figure out which D functions will read/write 
binary files





Re: generating random numbers

2020-08-13 Thread Andy Balba via Digitalmars-d-learn

On Monday, 10 August 2020 at 15:43:04 UTC, Andy Balba wrote:

On Monday, 10 August 2020 at 15:13:51 UTC, bachmeier wrote:

On Monday, 10 August 2020 at 14:20:23 UTC, bachmeier wrote:

On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
generating random numbers using 
https://dlang.org/library/std/random/uniform01.html


I find the example given in this section totally 
incomprehensible

.. Can any help me answer two simple questions:
How to generate a random floating number in range [0,1) ?
How to set a seed value, prior to generating random values ?


Strange example for sure. I'd recommend checking out the 
examples on the landing page for std.random: 
https://dlang.org/library/std/random.html


I created a PR with a hopefully clearer example:
https://github.com/dlang/phobos/pull/7588


Ahhh yes, yes .. this is the way to write Dlang example code :
https://dlang.org/library/std/random.html



... a very neat random byte generator is at
 https://github.com/LightBender/SecureD/tree/master/source/secured

here's the essential code :

import std.digest;
import std.stdio;
import std.exception;

@trusted ubyte[] random (uint bytes)
{
  if (bytes == 0)
 { printf("number of bytes must be > zero"); return null; }

  ubyte[] buffer = new ubyte[bytes];

  try
  { File urandom = File("/dev/urandom", "rb");
urandom.setvbuf (null, _IONBF);
scope(exit) urandom.close();

try
  { buffer= urandom.rawRead(buffer); }

catch(ErrnoException ex)
  { printf("Cant get next random bytes"); return null;}
catch(Exception ex)
  { printf("Cant get next random bytes"); return null; }
  }

  catch(ErrnoException ex)
{ printf("Cant initialize system RNG"); return null; }
  catch(Exception ex)
{ printf ("Cant initialize system RNG"); return null; }

return buffer;
}

void main()
{
  ubyte[] rnd1 = random(32);
  writeln("32Bytes: ", toHexString!(LetterCase.lower)(rnd1));

  ubyte[] rnd2 = random(128);
  writeln("128Bytes: ", toHexString!(LetterCase.lower)(rnd2));

  ubyte[] rnd3 = random(512);
  writeln("512Bytes:"); 
writeln(toHexString!(LetterCase.lower)(rnd3));


  ubyte[] rnd4 = random(2048);
  writeln("2048 Bytes:"); 
writeln(toHexString!(LetterCase.lower)(rnd4));


}


Re: generating random numbers

2020-08-10 Thread Andy Balba via Digitalmars-d-learn

On Monday, 10 August 2020 at 15:13:51 UTC, bachmeier wrote:

On Monday, 10 August 2020 at 14:20:23 UTC, bachmeier wrote:

On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
generating random numbers using 
https://dlang.org/library/std/random/uniform01.html


I find the example given in this section totally 
incomprehensible

.. Can any help me answer two simple questions:
How to generate a random floating number in range [0,1) ?
How to set a seed value, prior to generating random values ?


Strange example for sure. I'd recommend checking out the 
examples on the landing page for std.random: 
https://dlang.org/library/std/random.html


I created a PR with a hopefully clearer example:
https://github.com/dlang/phobos/pull/7588


Ahhh yes, yes .. this is the way to write Dlang example code :
https://dlang.org/library/std/random.html


generating random numbers

2020-08-09 Thread Andy Balba via Digitalmars-d-learn
generating random numbers using 
https://dlang.org/library/std/random/uniform01.html


I find the example given in this section totally incomprehensible
.. Can any help me answer two simple questions:
How to generate a random floating number in range [0,1) ?
How to set a seed value, prior to generating random values ?


How does one run a linux system command from a D main() fcn ?

2020-08-04 Thread Andy Balba via Digitalmars-d-learn

i.e.  D  equivalent to C++ command system("MyExe")


Re: safety and auto vectorization

2020-08-04 Thread Andy Balba via Digitalmars-d-learn
On Monday, 3 August 2020 at 19:42:51 UTC, Steven Schveighoffer 
wrote:

On 8/3/20 3:22 PM, Bruce Carneal wrote:


Thanks Steve (and Chad).  Summary: underspecified, varying 
behavior across versions, buggy.


Steve, what's the best way for me to report this?  Are spec 
issues lumped in with the other bugzilla reports?


Yep. You can file under dlang.org with the spec keyword.

Although this looks like it's not exactly a spec issue. I'd 
start it as a dmd bug (I don't know the exact interaction with 
the compiler and the library). It might also be a druntime bug.


-Steve


FWIW   ..using gdc Debian 8.3.0-6,   dst[]= generates a range 
error

auto a = [1, 2, 3];
auto b = [4, 5, 6];
int[] dst = new int[4]; // note the extra element
dst[] = a[] + b[];
writeln(dst[3]);


Re: 2-D array initialization

2020-08-02 Thread Andy Balba via Digitalmars-d-learn

On Sunday, 2 August 2020 at 06:37:06 UTC, tastyminerals wrote:

You haven't said anything about efficiency because if you care 
and your arrays are rather big, you better go with 
https://github.com/libmir/mir-algorithm as mentioned above. It 
might be a little finicky at the start but this post: 
https://tastyminerals.github.io/tasty-blog/dlang/2020/03/22/multidimensional_arrays_in_d.html should get you up to speed.



Keep in mind that std.array.staticArray is not efficient for  
large arrays.


If you want to stick to standard D, I would not initialize a 2D 
array because it is just cumbersome but rather use a 1D array 
and transform it into 2D view on demand via ".chunks" method. 
Here is an example.


import std.range;
import std.array;

void main() {
int[] arr = 20.iota.array;
auto arr2dView = arr.chunks(5);
}

Should give you

┌  ┐
│ 0  1  2  3  4│
│ 5  6  7  8  9│
│10 11 12 13 14│
│15 16 17 18 19│
└  ┘

whenever you need to access its elements as arr.chunks(5)[1][1 
.. 3] --> [6, 7].


@ tastyminerals  Thanks for your help on this. These comments, 
combined with the others, are making my climb of the D learning 
curve much quicker.


I'm not a gitHub fan, but I like the mir functions; and it looks 
like I have to download mir before using it.
mir has quite a few .d files..Is there a quick way to download it 
?


Re: 2-D array initialization

2020-08-01 Thread Andy Balba via Digitalmars-d-learn

On Saturday, 1 August 2020 at 22:00:43 UTC, Ali Çehreli wrote:

On 8/1/20 12:57 PM, Andy Balba wrote:

> On Saturday, 1 August 2020 at 00:08:33 UTC, MoonlightSentinel
wrote:
>> On Friday, 31 July 2020 at 23:42:45 UTC, Andy Balba wrote:
>>> How does one initialize c in D ?
>>
>> ubyte[3][4] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35,
35,35]  ];

> I'm a D newbie. moving over from C/C++, and I'm really
finding it hard
> to adjusting to D syntax, which I find somewhat cryptic
compared to C/C++.

That's surprising to me. I came from C++03 year ago but 
everything in D was much better for me. :)


I wanted to respond to your question yesterday and started 
typing some code but then I decided to ask first: Do you really 
need a static array? Otherwise, the following is a quite usable 
2D array:


  ubyte[][] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
35,35]  ];


However, that's quite different from a ubyte[3][4] static array 
because 'c' above can be represented like the following graph 
in memory. (Sorry if this is already known to you.)


c.ptr --> | .ptr | .ptr | .ptr | .ptr |
  |  |  |  |
  .  .  |   --> | 35 | 35 | 35 | 35 |
 etc.   etc. --> | 25 | 25 | 25 | 25 |

In other words, each element is reached through 2 dereferences 
in memory.


On the other hand, a static array consists of nothing but the 
elements in memory. So, a ubyte[3][4] would be the following 
elements in memory:


  | 5 | 5 | ... | 35 | 35 |

One big difference is that static arrays are value types, 
meaning that all elements are copied e.g. as arguments during 
function calls. On the other hand, slices are copied just as 
fat pointers (ptr+length pair), hence have reference semantics.


Here are some ways of initializing a static array. This one is 
the most natural one:


  ubyte[3][4] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
35,35]  ];


Yes, that works! :) Why did you need to cast to begin with? One 
reason may be you had a value that could not fit in a ubyte so 
the compiler did not agree. (?)


This one casts a 1D array as the desired type:

  ubyte[3][4] c = *cast(ubyte[3][4]*)(cast(ubyte[])[ 5, 5, 5, 
15, 15, 15, 25, 25, 25, 35, 35, 35 ]).ptr;


The inner cast is required because 5 etc. are ints by-default.

There is std.array.staticArray as well but I haven't used it.

Ali


Although not detailed in my original question, in my actual app
I have array ubyte [1000][3] Big which consists of research data 
I obtained,
 and from which I want to randomly select 4 observations to 
construct

ubyte c[ ][ ].

i.e. construct c= [ Big[r1][3], Big[r2][3], Big[r3][3], 
Big[r4][3] ]

where r1, r2, r3 and r4 are 4 random integers in 0..1001

Being a D newbie, my naive way of doing this was to declare c 
using:

ubyte[3][4] c= [ Big[r1][3], Big[r2][3], Big[r3][3], Big[r4][3] ]

Obviously, I want to learn how to this the smart D way, but I not 
smart enough at this point.





Re: 2-D array initialization

2020-08-01 Thread Andy Balba via Digitalmars-d-learn
On Saturday, 1 August 2020 at 00:08:33 UTC, MoonlightSentinel 
wrote:

On Friday, 31 July 2020 at 23:42:45 UTC, Andy Balba wrote:

How does one initialize c in D ?


ubyte[3][4] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
35,35]  ];



none of the statements below works

 c = cast(ubyte) [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
35,35]  ];


This is an invalid cast because it tries to coerce the entire 
literal into an ubyte. Also  it would be an assignment instead 
of an initialization because this is independent of c's 
declaration.



c[0] = ubyte[3] [5, 5, 5]   ;  c[1] = ubyte[3] [15, 15,15] ;
c[2] = ubyte[3] [25, 25,25] ;  c[3] = ubyte[3] [35, 35,35] ;


A cast is usually specified as `cast(TargetType) value` but not 
necesseray in this example. Use this instead:


c[0] = [5, 5, 5]   ;  c[1] = [15, 15,15] ;
c[2] = [25, 25,25] ;  c[3] = [35, 35,35] ;

for (int i= 0; i<3; i++) for (int j= 0; i<4; j++) c[i][j]= 
cast(ubyte)(10*i +j) ;


The array indices and the inner loop condition are wrong

for (int i= 0; i<3; i++) for (int j= 0; j<4; j++) c[j][i]= 
cast(ubyte)(10*i +j) ;


You could also use foreach-loops, e.g.

foreach (j, ref line; c)
foreach (i, ref cell; line)
cell = cast(ubyte) (10 * i + j);


@ MoonlightSentinel: sorry about the typo in for (int i= 0; i<3; 
i++) for (int j= 0; j<4; j++)


I'm a D newbie. moving over from C/C++, and I'm really finding it 
hard to adjusting to D syntax, which I find somewhat cryptic 
compared to C/C++.
After going thru a D on-line tutorial, I decided D was much 
better than C/C++, so I began by journey into D by converting one 
of my less complicated C++ apps.
I'm surprised at the enormous amount of time this D-conversion 
has taken compared to GO conversion of the very same app. 
Obviously, I need a faster way of climbing the D learning curve, 
so I would appreciate any suggestions.


2-D array initialization

2020-07-31 Thread Andy Balba via Digitalmars-d-learn

ubyte[3][4] c ;

How does one initialize c in D ?  none of the statements below 
works


 c = cast(ubyte) [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
35,35]  ];


c[0] = ubyte[3] [5, 5, 5]   ;  c[1] = ubyte[3] [15, 15,15] ;
c[2] = ubyte[3] [25, 25,25] ;  c[3] = ubyte[3] [35, 35,35] ;

for (int i= 0; i<3; i++) for (int j= 0; i<4; j++) c[i][j]= 
cast(ubyte)(10*i +j) ;


Re: arrays in srucs

2020-07-31 Thread Andy Balba via Digitalmars-d-learn

On Friday, 31 July 2020 at 17:26:17 UTC, Paul Backus wrote:

On Friday, 31 July 2020 at 17:02:46 UTC, Andy Balba wrote:
The above code, compiles and runs ok  .. but sometimes I get 
run runtime errors using the same paradym, which disappear 
when I substitute (img.p)[i]


Any explanation for this ?


Can you show an example where it doesn't work?


Copy the code block below into your editor then
Look down at second statement marked "// printf "

when I use () it works fine, but without () it gives run-time 
errors


import image_BMP ;
import colorMaps : hsv_colormap, jet_colormap, prism_colormap, 
vga_colormap ;


import std.math ;
import std.random ;
import std.stdio ;

int main()
{  int cmapM= 2, cmapJ= 3; // cmap:  0=hsv_colormap, 
1=jet_colormap, 2=prism_colormap, 3= vga_colormap
   // cmapM, cmapJ = colormap for 
Mendelbrot, Julia
   const uint W = 1200, H =  800, Bp=3, row_size= W*Bp ; // 
fractal width, height, Bytes per pixel

   IFImage img ;  // fractal image
   img.w = W; img.h = H; img.c = ColFmt.RGB; img.pixels = new 
ubyte[H*W*3];   // c= color format: 1=Gray 2=Gray+Alpha, 3=RGB  
4=RGB +Alpha

  // 1200 * 800 *3 = 2.8 MB per image 4 images= 11.2 MB
  int max_iterations= 1000;
  for (int Y=0; Y< H; ++Y)
  {  for (int X=0; X< W; ++X)
 {
double cr= 1.5 *(2.0*X /W-1.0) -0.5,  ci= (2.0*Y /H 
-1.0); // Mendelbrot Fractal constant= (cr, ci)

double nr=0, ni= 0,  pr=0, pi = 0;
for (int i= 0; i< max_iterations; i++)
{  pr= nr; pi = ni;  nr= pr*pr -pi*pi +cr;  ni= 
2*pr*pi +ci;  // z(real, imag)= (pr,pi),  z^2+c (real, imag)= 
(nr,ni)

   if (((nr*nr) +(ni*ni)) > 4)
   {  if (max_iterations != i)
  {  double z= sqrt(nr*nr +ni*ni);
 int cid = cast(int) (1000.0*log2(1.75 +i 
-log2(log2(z)))/ log2(max_iterations));
 ubyte[3][4] c= [ hsv_colormap[cid], 
jet_colormap[cid], prism_colormap[cid], vga_colormap[cid] ];

//printf("Y%i X%i :\n", Y, X);  stdout.flush();
 foreach ( p; 0..3 )
 {
//printf(">%i %i %i", p, cmapM, Y*row_size +X*Bp +p );   
stdout.flush();
 img.pixels [ Y*row_size +X*Bp +p ] = 
c[p][cmapM] ;

 }
  }
  break;
  }  }  }  }
  write_image ( "M_fractal.bmp", cast(long) img.w, cast(long) 
img.h, img.pixels, cast(long)img.c ) ;




arrays in srucs

2020-07-31 Thread Andy Balba via Digitalmars-d-learn

I frequently use this paradigm for my image processing apps :

import std.stdio ;
struct S { int a, b; ubyte[] p;
}
void main()
{ S img= S(11,22) ;  img.p = new ubyte[5];

foreach (i; 0 .. 5) {
  img.p[i]=cast(ubyte)(10+i) ; printf(" sa %d sb %d : %d\n", 
img.a, img.b, img.p[i] );

}
}

The above code, compiles and runs ok  .. but sometimes I get run 
runtime errors using the same paradym, which disappear when I 
substitute (img.p)[i]


Any explanation for this ?


is using predSwitch the only way to create to create recursive functions in D ?

2020-07-29 Thread Andy Balba via Digitalmars-d-learn

,,