Re: how to do this meta-programming? print the address of random element's address of a variable length of arrays?

2020-09-11 Thread mw via Digitalmars-d-learn

On Saturday, 12 September 2020 at 03:11:09 UTC, Ali Çehreli wrote:

On 9/11/20 6:44 PM, mw wrote:> e.g.
>
> int[] a = new int[la];
> int[] b = new int[lb];
> int[] c = new int[lc];
> int[] d = new int[ld];
>
>
> the func I want to write, e.g. for 2 arrays (instantiation)
is like this:
>
> void print_random_elem_addr(int[] x, int[] y) {
>auto i = random_int_between(0, x.length);
>auto j = random_int_between(0, y.length);
>print(&(x[i], &(y[j]));  // only single print() func call
allowed!
> }
>
>
> But I want one generic function, which can be called as:
>
> print_random_elem_addr(a, b);
> print_random_elem_addr(a, b, c);
> print_random_elem_addr(a, b, c, d);


Thanks for the reply.


If they are all of same type like int[] in this case, then you


but, this is not the intention, we should suppose the array's are 
heterogeneous type ...


can variable number of parameters, which means "any number of 
int[] arrays" below, elements of which can be called either as 
separate arguments or as a single array argument:


import std.stdio;
import std.random;

void print_random_elem_addr(int[][] arrays...) {


... to prevent passing in parameters as array of array like this.


  foreach (i, array; arrays) {
const chosen = uniform(0, array.length);
writefln!"Array %s, element %s: %s"(i, chosen, 
[chosen]);


actually this writefln will be called n times.

I intentionally require:

  print(&(x[i], &(y[j]));  // only single print() func call 
allowed!



I.e. I want to learn the generic meta-programming way to assemble 
such parameter list (&(x[i], &(y[j])) at compile time, it is 
possible?





Re: how to do this meta-programming? print the address of random element's address of a variable length of arrays?

2020-09-11 Thread Ali Çehreli via Digitalmars-d-learn

On 9/11/20 6:44 PM, mw wrote:> e.g.
>
> int[] a = new int[la];
> int[] b = new int[lb];
> int[] c = new int[lc];
> int[] d = new int[ld];
>
>
> the func I want to write, e.g. for 2 arrays (instantiation) is like this:
>
> void print_random_elem_addr(int[] x, int[] y) {
>auto i = random_int_between(0, x.length);
>auto j = random_int_between(0, y.length);
>print(&(x[i], &(y[j]));  // only single print() func call allowed!
> }
>
>
> But I want one generic function, which can be called as:
>
> print_random_elem_addr(a, b);
> print_random_elem_addr(a, b, c);
> print_random_elem_addr(a, b, c, d);

If they are all of same type like int[] in this case, then you can 
variable number of parameters, which means "any number of int[] arrays" 
below, elements of which can be called either as separate arguments or 
as a single array argument:


import std.stdio;
import std.random;

void print_random_elem_addr(int[][] arrays...) {
  foreach (i, array; arrays) {
const chosen = uniform(0, array.length);
writefln!"Array %s, element %s: %s"(i, chosen, [chosen]);
  }
}

void main() {
  auto randomLengthArray() {
return new int[uniform(1, 101)];
  }

  auto a = randomLengthArray();
  auto b = randomLengthArray();
  auto c = randomLengthArray();

  writeln("As independent arguments:");
  print_random_elem_addr(a, b, c);

  writeln("As a single argument:");
  print_random_elem_addr([a, b, c]);
}

Warning: The array that is automatically generated by the first 
print_random_elem_addr() call in main() is short-lived: You cannot store 
a slice of it because the array that contains the arguments may be 
destroyed upon leaving the function (e.g. in the "independent" case above).


Here is some more information:


http://ddili.org/ders/d.en/parameter_flexibility.html#ix_parameter_flexibility.variadic%20function

There are other ways of doing the same thing. For example, if you want 
to work with different ranges, you can use tuple template parameters:



http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter

Ali




how to do this meta-programming? print the address of random element's address of a variable length of arrays?

2020-09-11 Thread mw via Digitalmars-d-learn

e.g.

int[] a = new int[la];
int[] b = new int[lb];
int[] c = new int[lc];
int[] d = new int[ld];


the func I want to write, e.g. for 2 arrays (instantiation) is 
like this:


void print_random_elem_addr(int[] x, int[] y) {
  auto i = random_int_between(0, x.length);
  auto j = random_int_between(0, y.length);
  print(&(x[i], &(y[j]));  // only single print() func call 
allowed!

}


But I want one generic function, which can be called as:

print_random_elem_addr(a, b);
print_random_elem_addr(a, b, c);
print_random_elem_addr(a, b, c, d);
...


My main question is how to meta-program (generate the parameter 
list) this line:
  print(&(x[i], &(y[j]));  // only single print() func call 
allowed!



Thanks!


Re: Vibe-D File Question

2020-09-11 Thread James Blachly via Digitalmars-d-learn

On 9/11/20 7:28 AM, Daniel Kozak wrote:

void fun(HTTPServerRequest req, HTTPServerResponse res) nothrow
{
try
{
res.headers["Content-Disposition"] = "filename=\"muj.csv\"";
res.writeBody("some;csv;data", "text/csv");
}
catch (Exception e)
{}
}


Selim, note the Content-Disposition header in particular.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition


Re: Vibe-D File Question

2020-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Fri, Sep 11, 2020 at 1:15 PM Daniel Kozak  wrote:

> On Fri, Sep 11, 2020 at 1:10 PM Selim Ozel via Digitalmars-d-learn <
> digitalmars-d-learn@puremagic.com> wrote:
>
>> It seems like rejected-software forum is flooded with spam, so I
>> decided to ask it here. Is there a way to generate a file -csv
>> for example- on the back-end and serve it to the front-end as a
>> file.
>>
>> Serve static file [1] function does this for files saved on the
>> disk. I want to be able to generate a file during runtime and
>> serve it to the client. Is this possible?
>>
>> Thanks,
>> S
>>
>>
>> [1] https://vibed.org/api/vibe.http.fileserver/serveStaticFile
>>
>>
>>
> https://vibed.org/api/vibe.http.server/HTTPServerResponse.writeBody
>

import vibe.core.core;
import vibe.http.server;


void main()
{
runWorkerTaskDist();
runApplication();
}

void wrap()
{
auto settings = new HTTPServerSettings(":8080");
settings.options |= HTTPServerOption.reusePort;
listenHTTP(settings, );
}

void fun(HTTPServerRequest req, HTTPServerResponse res) nothrow
{
try
{
res.headers["Content-Disposition"] = "filename=\"muj.csv\"";
res.writeBody("some;csv;data", "text/csv");
}
catch (Exception e)
{}
}


Re: Vibe-D File Question

2020-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Fri, Sep 11, 2020 at 1:10 PM Selim Ozel via Digitalmars-d-learn <
digitalmars-d-learn@puremagic.com> wrote:

> It seems like rejected-software forum is flooded with spam, so I
> decided to ask it here. Is there a way to generate a file -csv
> for example- on the back-end and serve it to the front-end as a
> file.
>
> Serve static file [1] function does this for files saved on the
> disk. I want to be able to generate a file during runtime and
> serve it to the client. Is this possible?
>
> Thanks,
> S
>
>
> [1] https://vibed.org/api/vibe.http.fileserver/serveStaticFile
>
>
>
https://vibed.org/api/vibe.http.server/HTTPServerResponse.writeBody


Vibe-D File Question

2020-09-11 Thread Selim Ozel via Digitalmars-d-learn
It seems like rejected-software forum is flooded with spam, so I 
decided to ask it here. Is there a way to generate a file -csv 
for example- on the back-end and serve it to the front-end as a 
file.


Serve static file [1] function does this for files saved on the 
disk. I want to be able to generate a file during runtime and 
serve it to the client. Is this possible?


Thanks,
S


[1] https://vibed.org/api/vibe.http.fileserver/serveStaticFile