How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread vitalfadeev via Digitalmars-d-learn

How to?

I plan scan First array with checkers:
[checker, checker, checker]

...and store result to Second array:
[0, 0, 1]

In next code 'r' not reference. But expected ref.

import std.range : zip;

foreach(checker, ref r; zip(checkers, result.set)) {
   r = checker();
}

What is the best way for speed and beautyfull D code?




Re: How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread vitalfadeev via Digitalmars-d-learn

On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote:

How to?

I plan scan First array with checkers:
[checker, checker, checker]

...and store result to Second array:
[0, 0, 1]

In next code 'r' not reference. But expected ref.

import std.range : zip;

foreach(checker, ref r; zip(checkers, result.set)) {
   r = checker();
}

What is the best way for speed and beautyfull D code?

Detailed:

 import std.range : zip;

 struct Result {
 byte[] set;
 }
 Result result;

 alias Callback = bool delegate();
 Callback[] checkers;


 foreach(checker, ref r; zip(checkers, result.set)) {
r = checker();
 }



Re: How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread vitalfadeev via Digitalmars-d-learn

On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote:

On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote:

How to?

I plan scan First array with checkers:
[checker, checker, checker]

...and store result to Second array:
[0, 0, 1]

In next code 'r' not reference. But expected ref.

import std.range : zip;

foreach(checker, ref r; zip(checkers, result.set)) {
   r = checker();
}

What is the best way for speed and beautyfull D code?

Detailed:


import std.range : zip;
import std.stdio : writeln, write;


struct Result {
byte[] set;
}

alias Callback = bool function();


Callback[] checkers;
Result result;


bool false_cb() { return 0; }
bool true_cb() { return 1; }


void init() {
checkers ~= &false_cb;
checkers ~= &false_cb;
checkers ~= &true_cb;

result.set.length = checkers.length;
}


void check() {
foreach(checker, ref r; zip(checkers, result.set)) {
r = checker();
}
}


void print() {
foreach(r; result.set) {
write(r, " ");
}
writeln();
}


void main() {
init();
check();
print();
}



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



Re: How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread Anonymouse via Digitalmars-d-learn

On Monday, 10 June 2019 at 08:02:18 UTC, vitalfadeev wrote:

On Monday, 10 June 2019 at 07:45:42 UTC, vitalfadeev wrote:

On Monday, 10 June 2019 at 07:41:40 UTC, vitalfadeev wrote:

How to?

I plan scan First array with checkers:
[checker, checker, checker]

...and store result to Second array:
[0, 0, 1]

In next code 'r' not reference. But expected ref.

import std.range : zip;

foreach(checker, ref r; zip(checkers, result.set)) {
   r = checker();
}


Replace zip with lockstep and your example works.

"zip is similar to lockstep, but lockstep doesn't bundle its 
elements and uses the opApply protocol. lockstep allows reference 
access to the elements in foreach iterations."


https://dlang.org/library/std/range/zip.html

"Function lockstep

Iterate multiple ranges in lockstep using a foreach loop. In 
contrast to zip it allows reference access to its elements."


https://dlang.org/library/std/range/lockstep.html


Re: How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2019-06-10 at 08:02 +, vitalfadeev via Digitalmars-d-learn
wrote:

[…]

Perhaps I am missing something that is critical to the example, but I
rewrote the code as:

   import std.algorithm: map;
   import std.stdio: writeln;

   bool false_cb() { return false; }
   bool true_cb() { return true; }

   void main() {
   auto checkers = [&false_cb, &false_cb, &true_cb];
   auto result = map!(a => a())(checkers);
   writeln(result);
   }

This avoids the assumption that 0 is false and 1 is true. Also it makes
use of D map which seems to me what is happening at the core of the
code – using foreach and zip or lockstep just seems to be implementing
map. But as I say, I may be missing something.

-- 
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


Re: How to walk over two arrays by ref in beautyfull way?

2019-06-10 Thread vitalfadeev via Digitalmars-d-learn

Thanks!

I happy!

You are really powerfull!




Re: meson and d

2019-06-10 Thread Matthias Klumpp via Digitalmars-d-learn

On Sunday, 9 June 2019 at 13:13:28 UTC, dkd wrote:

hi, i need some meson configuration that works with dmd and ldc

my setting don't work. because meson complains it cannot find 
the d compiler.


d compiler can be invoked on command line.

i use :
set dmd_path="path/to/d/base"
set PATH=%dmd_path%\bin;%PATH%;

mingw can be detected by setting GCC_HOME

i use win7 and x86
meson 0.50.1


Are you using Windows? Meson's D support has never been tested 
there.
In theory though you can point it at any D compiler by setting 
the "DC" environment variable.


curl: can't send email?

2019-06-10 Thread Andrew via Digitalmars-d-learn

What wrong?

```
import std.net.curl;

void main() {
auto smtp = SMTP("smtps://smtp.gmail.com:465");
smtp.setAuthentication("qwe...@gmail.com", "password");
smtp.mailTo = ["std.net.curl.CurlException@std/net/curl.d(4364): Failed sending 
data to the peer on handle 55FACCB58AC0


??:? pure @safe void 
std.exception.bailOut!(std.net.curl.CurlException).bailOut(immutable(char)[], ulong, scope const(char)[]) [0x55facbd8db5e]
??:? pure @safe bool 
std.exception.enforce!(std.net.curl.CurlException).enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x55facbd8dad1]

??:? void std.net.curl.Curl._check(int) [0x55facbd7209b]
??:? int 
std.net.curl.Curl.perform(std.typecons.Flag!("throwOnError").Flag) [0x55facbd724c7]
??:? int 
std.net.curl.SMTP.perform(std.typecons.Flag!("throwOnError").Flag) [0x55facbd71a80]

??:? _Dmain [0x55facbd69b6b]


Re: meson and d

2019-06-10 Thread dkd via Digitalmars-d-learn

hi,

In theory though you can point it at any D compiler by setting 
the "DC" environment variable.


i found at early morning, that by setting variable at system 
level(through my computer->environment setting) meson can detect 
d.


it is the old usual way of setting variable that i almost forgot.

i'll try the DC variable

thanks.

# i use mason because i am stuck with dub, i got lost in 
documentation.


# reggae that i build from source, it works but at the end it 
throws exception.


Re: meson and d

2019-06-10 Thread Andre Pany via Digitalmars-d-learn

On Monday, 10 June 2019 at 16:51:15 UTC, dkd wrote:

hi,

In theory though you can point it at any D compiler by setting 
the "DC" environment variable.


i found at early morning, that by setting variable at system 
level(through my computer->environment setting) meson can 
detect d.


it is the old usual way of setting variable that i almost 
forgot.


i'll try the DC variable

thanks.

# i use mason because i am stuck with dub, i got lost in 
documentation.


# reggae that i build from source, it works but at the end it 
throws exception.


Where are you stuck within dub documentation? What do you want to 
achieve?


Kind regards
Andre


Re: curl: can't send email?

2019-06-10 Thread Andre Pany via Digitalmars-d-learn

On Monday, 10 June 2019 at 16:10:07 UTC, Andrew wrote:

What wrong?

```
import std.net.curl;

[...]


Curl smtp is a low level api. You need to know SMTP RFC.
Please have a look here, this is a high level API on top of curl 
smtp:

https://github.com/adamdruppe/arsd/blob/master/email.d

Kind regards
Andre


Re: if (X !is null && X.Y !is null) access crash

2019-06-10 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/9/19 1:25 AM, Amex wrote:

On Saturday, 8 June 2019 at 20:44:13 UTC, Steven Schveighoffer wrote:

Try GC.addRef on the X reference, and see if it helps.



This is during shutdown so I imagine simply turning off the GC should 
work fine? That way it prevents all cases rather than having to add a 
bunch of specific cases each time they crop up.


I'm not sure if you can shut off the final collection. All you can do is 
disable collections during allocations.


-Steve


Re: if (X !is null && X.Y !is null) access crash

2019-06-10 Thread Amex via Digitalmars-d-learn
On Monday, 10 June 2019 at 19:48:18 UTC, Steven Schveighoffer 
wrote:

On 6/9/19 1:25 AM, Amex wrote:
On Saturday, 8 June 2019 at 20:44:13 UTC, Steven Schveighoffer 
wrote:

Try GC.addRef on the X reference, and see if it helps.



This is during shutdown so I imagine simply turning off the GC 
should work fine? That way it prevents all cases rather than 
having to add a bunch of specific cases each time they crop up.


I'm not sure if you can shut off the final collection. All you 
can do is disable collections during allocations.


-Steve


So far it seems to be working. It may prevent a collection at the 
right time that causes the problem... it all may be coincidence.





Re: Function parameters UDAs

2019-06-10 Thread Johannes Loher via Digitalmars-d-learn
On Wednesday, 14 November 2018 at 18:05:55 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 14 November 2018 at 16:28:19 UTC, Radu wrote:
Looks like that there is no easy way to extract a function 
parameters UDA list.


Indeed, the only way I can find is kinda crazy:

---
void foo(int f, @("test") string s) {}

void main() {
static if(is(typeof(foo) Params == __parameters))
pragma(msg, __traits(getAttributes, 
Params[1..2])); // get the second param

}
---


So, the process is:

1) alias the params with the static if + is() statement (this 
is what std.traits.Parameters does internally)


2) slice the params! using params[0] will not work, but 
params[0 .. 1] will.


3) get the attributes using the language function


Has anybody come up with a better solution yet? And why does 
__traits(getAttributes, Parameters!foo[0]) not work in the first 
place?


I am asking because I ran into a problem which does not seem 
solvable to me using the above workaround:


I would like to iterate over all parameters of a function using 
static foreach and then process each parameter's UDAs. But by 
using static foreach on the parameter tuple, slicing it is not 
possible anymore, so the suggested workaround does not work in 
this case :(


The error does not appear if all parameters of the function are 
symbols (e.g. structs), but it still does not work as expected 
because the UDAs simply get dropped when iterating over the 
parameter tuple. Here is an example:


import std;

struct Test
{}

void foo(@(1) Test x)
{
}

void main()
{
alias Params = Parameters!foo;
pragma(msg, Params);
static foreach(P; Params)
{
pragma(msg, P);
static foreach(uda; __traits(getAttributes, P))
{
pragma(msg, uda);
}
}
}

This prints

(@(1) Test)
Test

but I would expect it to print something like

(@(1) Test)
@(1) Test
@(1)



Re: Function parameters UDAs

2019-06-10 Thread ag0aep6g via Digitalmars-d-learn

On 11.06.19 01:12, Johannes Loher wrote:
I would like to iterate over all parameters of a function using static 
foreach and then process each parameter's UDAs. But by using static 
foreach on the parameter tuple, slicing it is not possible anymore, so 
the suggested workaround does not work in this case :(


The workaround is to work with 1-length slices of your parameter 
sequence instead of elements. You can still do that. You just can't 
`foreach` over the parameters directly. Instead, iterate with an index 
and use it to get slices:


-
import std;

struct Test
{}

void foo(@(1) Test x, @(2) @(3) float y)
{
}

void main()
{
alias Params = Parameters!foo;
pragma(msg, Params);
static foreach(i; 0 .. Params.length)
{{
alias P = Params[i .. i + 1];
pragma(msg, P);
static foreach(uda; __traits(getAttributes, P))
{
pragma(msg, uda);
}
}}
}


Prints:


(@(1) Test, @(tuple(2), tuple(3)) float)
(@(1) Test)
1
(@(tuple(2), tuple(3)) float)
2
3



Re: Function parameters UDAs

2019-06-10 Thread Machine Code via Digitalmars-d-learn

On Wednesday, 14 November 2018 at 16:28:19 UTC, Radu wrote:
Looks like that there is no easy way to extract a function 
parameters UDA list.


The following:
```
import std.traits;

struct s { string foo; }
void foo(@s("aaa") int a, bool x);

void main()
{
alias P = Parameters!foo;
enum udas = __traits(getAttributes, P);
pragma(msg, udas);
}
```
will print `tuple(s("aaa"))`

but if you change `foo` to `void foo(int a, @s("aaa") bool x);`

you get `Error: first argument is not a symbol`

Is this a current limitation or I'm using the wrong approach?


I also, quite disappointed how UDAs doesn't work with enums. I 
end up using struct + enum to simulate that, sometimes it's quite 
a work.


Re: Function parameters UDAs

2019-06-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 11 June 2019 at 02:04:13 UTC, Machine Code wrote:
I also, quite disappointed how UDAs doesn't work with enums. I 
end up using struct + enum to simulate that, sometimes it's 
quite a work.


They do now...

struct foo {}

enum Foo {
@foo a
}

void main() {
foreach(item; __traits(allMembers, Foo))
pragma(msg, __traits(getAttributes, 
__traits(getMember, Foo,

item)));
}


Re: curl: can't send email?

2019-06-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 10 June 2019 at 16:10:07 UTC, Andrew wrote:

smtp.mailTo = ["

There's a missing > in that string.

idk if that is making a difference though, the error messages in 
this are awful.




Re: Function parameters UDAs

2019-06-10 Thread Machine Code via Digitalmars-d-learn

On Tuesday, 11 June 2019 at 02:24:49 UTC, Adam D. Ruppe wrote:

On Tuesday, 11 June 2019 at 02:04:13 UTC, Machine Code wrote:
I also, quite disappointed how UDAs doesn't work with enums. I 
end up using struct + enum to simulate that, sometimes it's 
quite a work.


They do now...

struct foo {}

enum Foo {
@foo a
}

void main() {
foreach(item; __traits(allMembers, Foo))
pragma(msg, __traits(getAttributes, 
__traits(getMember, Foo,

item)));
}


Awesome! It came out and I didn't notice.


I want an exception message but my threaded callback just dies or crashes

2019-06-10 Thread Markus via Digitalmars-d-learn
Hi, when you use core.windows and LoadLibrary to load a DLL, and 
that DLL will create a new thread and inside that it will call 
back into your D app, is there something to special to be aware 
of?


I've done it like with the CreateWindow and WndProc, but this 
time I'm loading a compiled portaudio DLL and I give it a call 
back. And I try to catch errors, but no matter what D Exception 
(I tried out of range and null dereferencing) that thread seems 
to either die silently (no hints in my console) or somehow fail 
to make portaudio go on calling (I don't think so).


I just wanted to ask if you know any pitfall with this... I'll go 
on using windbg and looking at things. I guess I should post a 
complete project, but I have to clean it up, and you maybe don't 
want to execute that portaudio32bit.dll from some unknown github 
account.


The function for Portaudio looks like that:

extern(C) nothrow int patestCallback(
const void* inputBuffer,
void* outputBuffer,
uint framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData)
{
auto data = cast(paTestData*)userData;
auto out_ = cast(float*)outputBuffer;

auto ce = collectException(
data.callback(
data.frames_per_buffer,
&data.left_buf[0],
&data.right_buf[0]));
if(ce !is null)
{
assumeWontThrow(writeln(ce.toString));
TerminateProcess(GetCurrentProcess(), 0);   
} else
{
assumeWontThrow(writeln("ce is null"));
}

for(uint i = 0; i < framesPerBuffer; ++i)
{
*out_++ = data.left_buf[i];
*out_++ = data.right_buf[i];
}

return PaStreamCallbackResult.paContinue;
}

...
...

I give it to Portaudio like that

{ auto err = Pa_OpenStream(
&stream,
null,
&outputParameters,
frames_per_second,
frames_per_buffer,
paClipOff,
&patestCallback,
&data);



Re: I want an exception message but my threaded callback just dies or crashes

2019-06-10 Thread Markus via Digitalmars-d-learn
I have cleaned it up, kind of. Just in case you want to compile 
it. The Zip contains some DLL from 
https://github.com/spatialaudio/portaudio-binaries where you can 
find the original if you want.


https://gofile.io/?c=NpUxrJ

I'm in general thinking of buying a Mac. I know it has not much 
to do with this, but debugging Swift code in Xcode must be so 
"gorgeous" as they say.