Re: Profiling the memory in D

2019-12-07 Thread kerdemdemir via Digitalmars-d-learn
On Wednesday, 4 December 2019 at 22:51:45 UTC, Steven 
Schveighoffer wrote:


I localized that the leak was actually being caused by 
websockets. I want to write down my experience because I did some 
weird stuff which seems to be working but I want to learn how it 
actually make sense and works.


I had a fancy work flow which caused by that bug 
https://github.com/vibe-d/vibe.d/issues/2169 which disallowed to 
open multiple sockets from my main process.


I solved that by lunching multiple process and using one web 
socket by one process. And communicated this process and my main 
process via zmqd.


My suggestion is; don't do that. Don't be super creative with 
current Vibe websockets. I had this unidentifable leak which took 
to much time to localize.


The bug I created around one year ago is solved now so I left 
creating processes approach and put web sockets in to a list.


Next problem I had while listening 300 websocket was I got some 
crushes within
webSocket.dataAvailableForRead() function, I am not sure if it is 
a bug or my misusage some how so I haven't created a bug yet. But 
my question to vibe forum can be seen 
https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/112309/


I solved that ( I hope ) by using something like:

if ( webSocket.waitForData(0.msecs) && 
webSocket.dataAvailableForRead() )


I know it looks so wierd to wait for 0.msecs but things seems to 
be working :/ .


The last problem I had was with closing the socket because this 
sockets are getting disconnected time to time. I need to close 
and reopen. When I call
webSocket.close() directly, after my program runs about 1 day it 
was freezing in a function called epoll while calling 
webSocket.close().


I also find another weird solution to that problem :

while ( true )
{
auto future = vibe.core.concurrency.async( { 
socket.socket.close(); return true;} );

vibe.core.core.sleep(100.msecs);
if ( future.ready() )
break;
writeln( " Couldn't close the socket retring ");
}
sockets.remove(uniqStreamName);// BTW order of this removal 
matters if you remove

   //from your list before closing the ticket you are screwed.


Which seems to be working with 300 websockets around 2 days 
without any leak nor crush neither freeze.


As I pointed in the beginning I don't have any question or 
problems now but I am very open to feedback if you guys have any.


Erdemdem








Re: const and immutable values, D vs C++?

2019-12-04 Thread kerdemdemir via Digitalmars-d-learn
On Wednesday, 4 December 2019 at 14:44:43 UTC, Ola Fosheim 
Grøstad wrote:
When is there a noticable difference when using const values 
instead of immutable values in a function body? And when should 
immutable be used instead of const?


f(){
  const x = g();
  immutable y = g();
  ... do stuff with x and y …
}

I'm comparing D to C++ and I get the following mapping:

D:
enum constant = number

C++:
enum : decltype(number) { constant = number }

D:
auto p =  g()

C++:
auto p = g()

D:
const p = g()

C++:
const auto p = g()

D:
immutable p = g()

C++:
hmmm...

Has anyone done a feature by feature comparison with C++? It 
would be interesting to see what it looks like.


Unfortunately I am not yet good with D to answer your question .
But Ali Çehreli made some comparesions with C++.
https://dconf.org/2013/talks/cehreli.pdf
And I think you will find the answers of your questions in it 
also.


Re: Profiling the memory in D

2019-12-04 Thread kerdemdemir via Digitalmars-d-learn
On Wednesday, 4 December 2019 at 15:38:36 UTC, Steven 
Schveighoffer wrote:

On 12/4/19 3:10 AM, Erdem wrote:
I am used to have cool tools like valgrid massif to visualize 
the memory usage from C++ but in D it seems I am blind folded 
looking for the problem.


Until now I tried:

--vgc option which show million things which makes it not 
useful

--build=profile-gc
which seems to slow down my program like *200 times and I have 
some operation depending on time which makes creating the real 
case of the application impossible. The observation itself 
effect the experiment very badly.


Since this options are not useful for me, I tried to put debug 
logs in critical places to estimate where I am losing memory. 
I couldn't find a D native call to get the memory usage of the 
program. I had to call a shell command which also slows down 
the app. This slow down causes me to not be able to put this 
debug log too many places or in critical loops.


I manage to narrow it down a bit. Than I wanted to check the 
object instances to find out which objects are getting bigger. 
I tried
GC.sizeOf() which always prints 0. GC.sizeOf also does 
not works with associative arrays , I don't see any usefull 
case than GC.sizeOf(array.ptr).


That is the background of my questions and my questions are:

Is there a better way to visualize the memory in D something 
like valgrid massif?


profile-gc literally makes to program not do anything due to 
slow down is there any way to profile-gc in a faster fashion?


Is there a native function which will return the memory usage 
of my program?


If it's total GC memory only you are interested in, then try 
the D runtime switch for the GC: --DRT-gcopt=profile:1


This will print out a summary of GC usage at the end of your 
program, and shouldn't significantly affect runtime.




Is there a function to return sizes of AAs and even class 
instances ?




Class instances have a compiler-defined size. Try 
__traits(classInstanceSize, SomeClass)


This is the compile-time size of that specific type. If you 
have a class instance, and you want the actual class size, in 
the case of a derived instance, you may retrieve that using 
typeid(classInstance).initializer.length.


Note also that this is not necessarily the size consumed from 
the GC! If you want *that* size, you need to use 
GC.sizeof(cast(void*)object) (you were almost correct, what you 
did was get the GC size of the *class reference* which is 
really a pointer, and really lives on the stack, hence the 0).


-Steve


GC.sizeof(cast(void*)object) will be super useful. I will use 
that.


I also tried GC: --DRT-gcopt=profile:1 already. It provides so 
little information.
I need to find out which member AA or array of which object is 
causing this memory problem of mine.I am ending up around 2GB of 
ram usage in a single day.


Is there any way to manipulate profile-gc flag on run time? Like 
I will start my program without it somehow and after the my 
program initializes I will turn it on.


One last thing in my program I am getting a message from vibe 
sometimes like
"leaking eventcore driver because there are still active 
handles". I use websockets and do web requests. I wanted to add 
that because I saw you fixed something with Redis about that in 
https://github.com/vibe-d/vibe.d/issues/2245.


Thanks for your help and replies Steve.




Referance usage in async function

2019-11-30 Thread kerdemdemir via Digitalmars-d-learn

I have simplified my problem which can be seen below.

import std.stdio;
import vibe.core.core;
import vibe.core.concurrency;
import vibe.data.json;

void main()
{
int[] list;

bool ListManipulator(ref int[] list)
{
list ~= 2;
list ~= 4;
return true;
}

bool ListManipulatorPointer( int[]* list)
{
*list ~= 2;
*list ~= 4;
return true;
}


auto future = vibe.core.concurrency.async(, 
list);

future.getResult();

writeln(list); > prints empty list

future = vibe.core.concurrency.async(, 
);

future.getResult();

writeln(list); > prints [2,4]
}


Why passing the pointer works meanwhile passing as reference does 
nothing? I feel that is more D issue than vibe.d which I can 
learn something I hope.


Erdem





Re: What is the replacement for deprecated array removal

2019-11-18 Thread kerdemdemir via Digitalmars-d-learn
On Monday, 18 November 2019 at 21:14:53 UTC, Steven Schveighoffer 
wrote:

On 11/18/19 3:53 PM, kerdemdemir wrote:

Is there any way to remove list of elements efficiently with a 
dynamical array?


It seems kind of silly that it's not allowed, but maybe it will 
be possible after the deprecation is removed.


But you could do something like this:

list = list.remove!(a => removeList.canFind(a));

This is going to suck. Because it's O(n^2). Should be O(n + m) 
way to do it (assuming you have a sorted index list).




Or is there anyway converting a dynamical array into a form 
which is like a AliasSeq?


An AliasSeq is a compile-time construct, and cannot be created 
from a runtime array.


-Steve


Thanks for awesome answers man ,

Yes that sucks real hard because unlike that dummy example in my 
real case == compression will be too expensive I can't simply use 
canFind on every index.


I guess I need to recreate a new dynamical array each time which 
is also horrible.


It is a bit weird that such a general case like removing list of 
elements does not work.


And I really do not know the real use of [0,1,2,3].remove(1,2,3). 
I mean in unit test it looks cool but in real life you will have 
dynamically calculated indexes.


Erdem








Re: What is the replacement for deprecated array removal

2019-11-18 Thread kerdemdemir via Digitalmars-d-learn

On Monday, 18 November 2019 at 20:48:40 UTC, kerdemdemir wrote:
On Monday, 18 November 2019 at 20:37:50 UTC, Steven 
Schveighoffer wrote:
If I follow the code correctly, it's treating your array as a 
tuple of pos/len to remove.


So it looks like your code is equivalent to

remove(tuple(0, 2));

Which is probably not what you want.

This probably explains why it's being deprecated, it's too 
confusing to the compiler.


And looking at git blame goes back to this PR: 
https://github.com/dlang/phobos/pull/6154


-Steve


Sorry to be horrible at explaining but remove(tuple(0, 2)); is 
not what I want.


I have an array which goes from 0 to 5. And I want to remove 
odd numbers.


remove(tuple(0, 2)); defines a starting index and a ending 
index but I need to delete not consecutive elements. In the 
case of odd number from 0 to 5 that will be 1, 3 and 5 .


It is so weird when you type it is allowed like remove(1, 3, 5) 
but unexpectedly remove([1,3,5]) does not work the same way.


I think if I can convert [1,3,5] to a AliasSeq that could be ok 
also.


Erdem


I read your message again now and I see that you weren't 
suggesting remove(tuple(0, 2)) but you were pointing out it is 
not what I want as well.


Is there any way to remove list of elements efficiently with a 
dynamical array?


Or is there anyway converting a dynamical array into a form which 
is like a AliasSeq?



Erdem



Re: What is the replacement for deprecated array removal

2019-11-18 Thread kerdemdemir via Digitalmars-d-learn
On Monday, 18 November 2019 at 20:37:50 UTC, Steven Schveighoffer 
wrote:
If I follow the code correctly, it's treating your array as a 
tuple of pos/len to remove.


So it looks like your code is equivalent to

remove(tuple(0, 2));

Which is probably not what you want.

This probably explains why it's being deprecated, it's too 
confusing to the compiler.


And looking at git blame goes back to this PR: 
https://github.com/dlang/phobos/pull/6154


-Steve


Sorry to be horrible at explaining but remove(tuple(0, 2)); is 
not what I want.


I have an array which goes from 0 to 5. And I want to remove odd 
numbers.


remove(tuple(0, 2)); defines a starting index and a ending index 
but I need to delete not consecutive elements. In the case of odd 
number from 0 to 5 that will be 1, 3 and 5 .


It is so weird when you type it is allowed like remove(1, 3, 5) 
but unexpectedly remove([1,3,5]) does not work the same way.


I think if I can convert [1,3,5] to a AliasSeq that could be ok 
also.


Erdem


Re: What is the replacement for deprecated array removal

2019-11-18 Thread kerdemdemir via Digitalmars-d-learn

int[] removeList;
for ( int i = 0; i < tempMap[0].length; i++ )
{
if ( i%2 == 0 )
removeList ~=i;
}
writeln(removeList); (prints 0,2,4)
tempMap[1].remove(0,2,4);
tempMap[2].remove(removeList);
tempMap[3].remove(tuple(0,1),tuple(2,3),tuple(4,5) );

Even weirder(at least for me)

int[] removeList is [0,2,4]

And .remove returns different results for .remove(0,2,4); and 
.remove(removeList)


Is there anyway to convert int[] to a sequence like 0,2,4

Erdem


What is the replacement for deprecated array removal

2019-11-18 Thread kerdemdemir via Digitalmars-d-learn
I know my example can be shortened but please excuse me that I am 
pasting directly


import std.stdio;
import std.math;
import std.range;
import std.algorithm;
import std.typecons;

int[][4] tempMap;

void main()
{
int[] temp  = [ 1, 2, 3 , 4 ,5 ];
tempMap[0] =  temp.dup;
tempMap[1] = temp.dup;
tempMap[2] = temp.dup;
tempMap[3] = temp.dup;

int[] removeList;
for ( int i = 0; i < tempMap.length; i++ )
{
if ( i%2)
removeList ~=i;
}
tempMap[1].remove(removeList);
writeln(tempMap);

}

Results with that warning:

onlineapp.d(24): Deprecation: function 
std.algorithm.mutation.remove!(cast(SwapStrategy)2, int[], 
int[]).remove is deprecated


How can I remove a list of indexes with remove ?

Erdem









ggplotd Fixed ratio between x and y axes

2019-03-11 Thread kerdemdemir via Digitalmars-d-learn
How can I configure a fixed ratio between x and y axes in ggplotd 
?


I easily found what I am looking for in ggplot which ggplotd 
inspires a lot.

http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/

But ggplotd documentation does not have any info about that. Even 
I go through the source code spend around half a hour I couldn't 
figure out how to achieve:


sp <- ggplot(dat, aes(xval, yval)) + geom_point()
sp + coord_fixed(ratio=1/3)

Erdem


Re: Applying a function each respective 2 elements of an array

2019-03-10 Thread kerdemdemir via Digitalmars-d-learn

On Sunday, 10 March 2019 at 09:43:59 UTC, Dennis wrote:

On Sunday, 10 March 2019 at 08:59:59 UTC, kerdemdemir wrote:
Can I avoid for loops and solve my problem with std algorithms 
or ranges ?


Try slide: https://dlang.org/phobos/std_range.html#slide
And then use map.


I think that will work that is exactly what I am looking for 
thanks


Erdem


Applying a function each respective 2 elements of an array

2019-03-10 Thread kerdemdemir via Digitalmars-d-learn
I have an array like(In my real problem my data structs is more 
complex ) :


auto a = [2,3,4,5,6,7];


I want to apply a operation in a fashion like :

 [ 2 , 3 ]  --> apply foo and get return result -1
 [ 3 , 4 ]  ---> -1
 [ 4 , 5 ] ---> -1
  and so on...

operation might be :

int foo ( int a, int b)
{
   return a - b;
}

So my expected result is [-1,-1,-1,-1,-1]

I tried fold but fold add up the result of the last operation.
Range utilities like "chunks" does not help because "chunks" 
transforms the array into a range like [2,3],[4,5],[6,7].


Can I avoid for loops and solve my problem with std algorithms or 
ranges ?


Removing the precision from double

2018-11-01 Thread kerdemdemir via Digitalmars-d-learn

I have two numbers

First The price  = 0.0016123
Second Maximum allowed precision = 0.0001(it can be only 
0.001, 0.0001, 0.1, ..., 0.01 bunch of zeros and than 
a one that is it)


Anything more precise than the allow precision should truncated.
So in this case 0.0016123 should turn into 0.0016.

I coded this which in my tests have no problem :

double NormalizeThePrice( double price, double tickSize)
{
import std.math : floor;
double inverseTickSize = 1.0/tickSize;
return floor(price * inverseTickSize) *  tickSize;
}

writeln(NormalizeThePrice(0.0016123, 0.0001));
Returns 1.6e-07 as excepted.

I am doing trading and super scared of suprices like mathematical 
errors during the multiplications(or division 1/tickSize) since 
market will reject my orders even if there is a small mistake.


Is this safe? Or is there a better way of doing that ?

Erdemdem





Re: Is there websocket client implementation for D

2018-06-11 Thread kerdemdemir via Digitalmars-d-learn

Hi

vibe.d has a client implementation.

http://vibed.org/api/vibe.http.websockets/WebSocket

It is as simple as :


auto ws_url = 
URL("wss://stream.binance.com:9443/ws/ethbtc@aggTrade");

auto ws = connectWebSocket(ws_url);
if ( !ws.connected )
return;

while ( true )
{
if  (ws && ws.dataAvailableForRead())
{
writeln("Recieve", ws.receiveText());
}

sleep(100.msecs);   
}   


Connecting two web sockets at the same time with vibe.d

2018-06-02 Thread kerdemdemir via Digitalmars-d-learn
I am blocked in my project because of an issue while using 
websockets.


I can simplify my problem like :

	auto ws_url = 
URL("wss://stream.binance.com:9443/ws/ethbtc@aggTrade");

auto ws = connectWebSocket(ws_url);
if ( !ws.connected )
return;
sleep(2.seconds);
ws_url = URL("wss://stream.binance.com:9443/ws/iotabtc@depth");
auto ws2 = connectWebSocket(ws_url);
if ( !ws2.connected )
return; 

If I don't close the first socket before opening the second one I 
am getting two different exceptions which can be seen in my 
question which I already ask in vibe.d's forum :


https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/52273/

Since I am blocked I am open to any suggestions. Or if some one 
knows an alternative socket implementation I may have to switch 
to another library.


So do you guys have any suggestions or an alternative library for 
socket handling?


Erdem


Finding the last executed line by checking dmd core

2018-05-03 Thread kerdemdemir via Digitalmars-d-learn

After a big refactor my code crushes I have no idea where.

I am only getting :

Program exited with code -11

And a core file.

I used to use gdb for c++ coredumps. With what program I 
can check dmd core file?


Erdemdem


Re: How to customize vibe.data.json

2018-03-30 Thread kerdemdemir via Digitalmars-d-learn

On Friday, 30 March 2018 at 17:58:23 UTC, Seb wrote:

On Friday, 30 March 2018 at 16:47:52 UTC, kerdemdemir wrote:

Hi,

In vibe's web 
page(http://vibed.org/api/vibe.data.json/serializeToJson) it 
is told that I should implement


[...]


I think you are looking for this - 
https://github.com/vibe-d/vibe.d/pull/2088


Feel free to ping the people there ;-)


struct RESTTime {
SysTime time;
alias time this;
static RESTTime fromString(string v) { return
RESTTime(SysTime.fromSimpleString(v)); }
string toString() const { return time.toSimpleString(); }
}

struct TradeData
{
UUID   sellOrderID;
UUID   buyOrderID;  

SysTime buyOrderTime;
SysTime sellOrderTime;  
}

void main() {

TradeData t;
Json jsonResult = t.serializeToJson();
writeln(jsonResult.toString());

}

Unfortunately what is shown in the forum is not working with 
SysTime.
My program is being terminated in 
std.datetime.SysTime.toISOExtString() function by code -11.


I hope it does not sound like spoon feeding but do you know any 
solution for SysTime. I am working on it in parallel.


Erdem




Re: How to customize vibe.data.json

2018-03-30 Thread kerdemdemir via Digitalmars-d-learn

On Friday, 30 March 2018 at 17:58:23 UTC, Seb wrote:

On Friday, 30 March 2018 at 16:47:52 UTC, kerdemdemir wrote:

Hi,

In vibe's web 
page(http://vibed.org/api/vibe.data.json/serializeToJson) it 
is told that I should implement


[...]


I think you are looking for this - 
https://github.com/vibe-d/vibe.d/pull/2088


Feel free to ping the people there ;-)


Exactly :)


How to customize vibe.data.json

2018-03-30 Thread kerdemdemir via Digitalmars-d-learn

Hi,

In vibe's web 
page(http://vibed.org/api/vibe.data.json/serializeToJson) it is 
told that I should implement


Json toJson() const;
static T fromJson(Json src);

string toString() const;
static T fromString(string src);

I think I should implement those as member functions(I am not 
sure).


I have a struct like:

struct TradeData
{
import std.uuid : UUID;
import std.datetime : SysTime;

UUID   sellOrderID;
UUID   buyOrderID;
SysTime buyOrderTime;
SysTime sellOrderTime;  
}

What I want is automatically json conversion of UUID and SysTime 
classes by returning UUID.toString() and SysTime.toSimpleString() 
methods when serializeToJson() is being called. Since these are 
std classes I don't know how can I manipulate them.


Can you please tell me how should I use toJson(), fromJson() 
functions for customizing user defined objects.


Erdem



Making mir.random.ndvariable.multivariateNormalVar create bigger data sets than 2

2018-02-27 Thread kerdemdemir via Digitalmars-d-learn

I need a classifier in my project.
Since it is I believe most easy to implement I am trying to 
implement logistic regression.


I am trying to do the same as the python example:  
https://beckernick.github.io/logistic-regression-from-scratch/


I need to data sets with which I will test.

This works(https://run.dlang.io/is/yGa4a0) :

double[2] x1;
Random* gen = threadLocalPtr!Random;

auto mu = [0.0, 0.0].sliced;
auto sigma = [1.0, 0.75, 0.75, 1].sliced(2,2);
auto rv = multivariateNormalVar(mu, sigma);
rv(gen, x1[]);
writeln(x1);

But when I increase my data set size from double[2] to 
double[100] I am getting an assert :


mir-random-0.4.3/mir-random/source/mir/random/ndvariable.d(378): 
Assertion failure


which is:
assert(result.length == n);

How can I have a result vector which has size like 5000 something?

Erdemdem



opCmp with double values

2017-12-24 Thread kerdemdemir via Digitalmars-d-learn
In documentation and forums I found some example for overloading 
opCmp for int values. But I couldn't see any examples for double 
values.


That is what I come up with my own:

struct AdjustableVal ( T = double )
{
this ( T initVal )
{
curVal = initVal;
initialVal = initVal;
}


int opCmp( T rhs ) const {
auto diff = curVal - rhs;
if (  fabs(diff) < 0.0001 )
return 0;
else if ( diff < 0 )
return -1;
else
return 1;
 }

T curVal;
T initialVal;
}

Do you guys see any problem with it or any suggestions?
Secondly if the value type(T) does not have "-" operator, is it 
possible to still get my code compiled somehow with static_if?



Erdem


Re: Finding equivalent of C++ feature in D documentation.

2017-12-23 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 23 December 2017 at 15:58:27 UTC, Seb wrote:
On Saturday, 23 December 2017 at 15:45:33 UTC, Mike Franklin 
wrote:
On Saturday, 23 December 2017 at 15:04:30 UTC, kerdemdemir 
wrote:


Is there any better way for me to search C/C++ equivalent 
features? As a humble suggestion would it make sense to make 
a table with feature names and corresponding name of the same 
feature in other languages ?



Try this: 
https://wiki.dlang.org/Programming_in_D_for_C%2B%2B_Programmers


... and potentially this: https://wiki.dlang.org/Coming_From

Those resources are community maintained.  When I was first 
learning D, if I encountered a problem and struggled to a 
solution, I would make an attempt to documented it on the D 
wiki to try and save others from having to go through the same 
difficulty.  I encourage you to do the same.


Mike


There are these two pages:

https://dlang.org/ctod.html
https://dlang.org/cpptod.html


Thanks guys both pages are good.

@Mike I really want to add this case about M.I.L syntax in to 
wiki page. But since I am not a D pro; I am shy and also I am not 
sure if I have the permission to modify this page. Do you suggest 
me to try adding this spesific to the wiki page?


Finding equivalent of C++ feature in D documentation.

2017-12-23 Thread kerdemdemir via Digitalmars-d-learn

I needed to find equivalent of member initialization list in D.
After searching ~10 minutes I found D has very elegant solution 
(https://dlang.org/spec/class.html#field-init) after reading 
through whole constructor page. My question is not technical this 
time I have my solution. But I think when I google "Member 
inilization in D" I am expecting to be directed to correct 
documentation.


This unfortunately happens a lot to me. And this minutes sum into 
hours and inefficiency.


Is there any better way for me to search C/C++ equivalent 
features? As a humble suggestion would it make sense to make a 
table with feature names and corresponding name of the same 
feature in other languages ?


Erdem


Re: One liner for creating an array filled by a factory method

2017-12-23 Thread kerdemdemir via Digitalmars-d-learn

On Friday, 22 December 2017 at 23:33:55 UTC, Mengu wrote:
On Thursday, 21 December 2017 at 21:11:58 UTC, Steven 
Schveighoffer wrote:

On 12/21/17 4:00 PM, kerdemdemir wrote:

I have a case like :

http://rextester.com/NFS28102

I have a factory method, I am creating some instances given 
some enums.

My question is about :


void PushIntoVector( BaseEnum[] baseEnumList )
{
     Base[] baseList;
     foreach ( tempEnum; baseEnumList )
     {
    baseList ~= Factory(tempEnum);
     }
}

I don't want to use "foreach" loop. Is there any cool std 
function that I can call ?


Something like baseEnumList.CoolStdFunc!( a=> Factory(a) 
)(baseList);




https://dlang.org/phobos/std_algorithm_iteration.html#map

-Steve


so basically it becomes:

Base[] baseList = baseEnumList.map!(el => Factory(el));

there's also a parallel version of map [0] if you ever need to 
map the list concurrently.


[0] https://dlang.org/phobos/std_parallelism.html#.TaskPool.map


Yeah that was very easy and I used to use map for this purposed a 
lot already. I don't know why I get confused. Thanks guys for 
correction. I began to think like map could transform but it 
can't create vector of elements and this confused me.


Converting member variables to strings with using reflection from base class

2017-12-22 Thread kerdemdemir via Digitalmars-d-learn
I want to make a logging function for member variables by using 
reflection.


import std.stdio;

class D : B
{
override void foo() {
a  = 4.0;
b  = 3.0;
}
double a;
double b;
}

class B
{
void Log()
{
auto a = [__traits(derivedMembers, D)];
foreach(memberName; a) {
// Somehow write only member variables with their 
names

// Result should be : a = 4.0, b = 3.0
}
}

void foo()
{
}
}

void main()
{
 auto b = new D;
 b.Log();
}

As I wrote in the comments I want to see member variable's name 
and its value.

What is the best way to achieve that?

Erdem


One liner for creating an array filled by a factory method

2017-12-21 Thread kerdemdemir via Digitalmars-d-learn

I have a case like :

http://rextester.com/NFS28102

I have a factory method, I am creating some instances given some 
enums.

My question is about :


void PushIntoVector( BaseEnum[] baseEnumList )
{
Base[] baseList;
foreach ( tempEnum; baseEnumList )
{
   baseList ~= Factory(tempEnum);
}
}

I don't want to use "foreach" loop. Is there any cool std 
function that I can call ?


Something like baseEnumList.CoolStdFunc!( a=> Factory(a)  
)(baseList);


Erdem


Re: Passing anonymous enums as function parameters

2017-12-20 Thread kerdemdemir via Digitalmars-d-learn

enum
{
a = "foo",
b = "bar",
c = "baz";
}

is identical to

enum a = "foo";
enum b = "bar";
enum c = "baz";



Thanks Jonathan I think that changes my point of perspective.

And Jacob Carlborg I like the third option a lot with aliases 
good to know that


enum Foo : string
{
KErdem
Ali
Zafer
Salih
//etc...
}

alias KErdem = Foo.KErdem
alias Ali = Foo.Ali
// etc...

Erdem


Re: Passing anonymous enums as function parameters

2017-12-17 Thread kerdemdemir via Digitalmars-d-learn

What I meant with anonymous enums was:
https://dlang.org/spec/enum.html#anonymous_enums. Maybe I 
couldn't explain well but I believe D have anonymous enums. I am 
sorry I have forgotten to remove " :string" in my example from 
the "enum : string". Please stretch out ": string" part my 
problem is not related with that.


I am not sure if it is a good one but I found a solution for my 
problem


double ReturnCoolNess(U)( U enumVal )
{
  switch (enumVal)
  {
case KErdem:
{
return 0.0
}
case Ali:
{
return 100.0;   
}
case Salih:
{
return 100.0;   
}
// etc..
  }
}

ReturnCoolNess(KErdem); ---> Compiles


D does not have anonymous enums. Either you're declaring an 
enum which creates a new type and therefore has a name and a 
base type, or you're just creating manifest constants that 
don't create a new type. e.g.


enum MyEnum : string
{
a = "hello",
b = "foo",
c = "dog",
d = "cat"
}

vs

enum string a = "hello";
enum string b = "foo";
enum string c = "dog";
enum string d = "cat";

or

enum a = "hello";
enum b = "foo";
enum c = "dog";
enum d = "cat";

If you want a function to accept values that aren't tied to a 
specific enum, then just have the function take the base type.


Now, within sections of code, you can use the with statement to 
reduce how often you have to use the enum type's name, e.g.


with(MyEnum) switch(enumVal)
{
case a: { .. }
case b: { .. }
case c: { .. }
case d: { .. }
}

but you can't have an enum without a name or just choose not to 
use an enum's name. With how enums work in D, there's really no 
point in having them if you're not going to treat them as their 
own type or refer to them via the enum type's name. If that's 
what you want to do, then just create a bunch of manifest 
constants.


- Jonathan M Davis





Passing anonymous enums as function parameters

2017-12-17 Thread kerdemdemir via Digitalmars-d-learn

I have an enum statement :

enum : string
{
KErdem
Ali
Zafer
Salih
//etc...
}


I don't want to give a name to my enum class since I am accessing 
this variables very often.


But I also have a function like:

double ReturnCoolNess( /* Is there any way? */ enumVal )
{
  switch (enumVal)
  {
case KErdem:
{
return 0.0
}
case Ali:
{
return 100.0;   
}
case Salih:
{
return 100.0;   
}
// etc..
  }
}

Is there any way I still keep my enum anonymous and be able to 
call functions with different enumarations. Or is there any other 
way to call named enums without type name ?


Re: Scope failure is not preventing application crush in case of uncaught exceptions

2017-12-16 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 16 December 2017 at 20:56:26 UTC, ketmar wrote:

kerdemdemir wrote:

As far as I know scope(failure) should be collecting all 
failure cases.


nope. `failure` scope won't stop exception propagation, it is 
just called before exception leaves your function, to give you 
a last chance to do some immediate cleanup.


Than do I still need to catch'em all? For me it doesn't matter 
the which exception it is. It is more important for me to not 
pollute my code with so many exception related lines. What is the 
most compact way for me to catch'em all?


Scope failure is not preventing application crush in case of uncaught exceptions

2017-12-16 Thread kerdemdemir via Digitalmars-d-learn

While calling this function :

bool PublicMarketCall( ref Json result )
{
  string fullUrl = 
"https://bittrex.com/api/v1.1/public/getmarketsummaries;;

  Json data;
  try
  {
requestHTTP(fullUrl,
(scope req) {
req.method = HTTPMethod.GET;
},
(scope res) {
data = parseJsonString(res.bodyReader.readAllUTF8());   
}
);
  }
  catch ( std.json.JSONException e )
  {
  	writeln("Exception was caught while making the public call: ", 
fullUrl);

return false;
  }

  scope(failure)
writeln("Exception was caught while public data: ", fullUrl);
  return false;
}

Very rarely (I am calling this function thousands of times in a 
day) like once in a day I am getting a crush while calling 
requestHTTP function :


object.Exception@../../../.dub/packages/vibe-d-0.8.1/vibe-d/core/vibe/core/drivers/libevent2.d(375):
 Failed to connect to host 104.17.154.108:443: 7FF703F4F617

??:? [0x606cee]
??:? [0x60ceb2]
exception.d:421 [0x4644a3]
exception.d:388 [0x41396d]
libevent2.d:375 [0x59002b]
net.d:129 [0x5be726]
client.d:610 [0x4a8ed4]
client.d:528 [0x4a7224]
client.d:462 [0x4a5a3f]
client.d:157 [0x4a5819]
client.d:133 [0x4a52bf]
 --> Some files belonging my function
app.d:41 [0x43b947]
??:? [0x612f3e]
??:? [0x612efd]
??:? [0x612df8]
__entrypoint.d:8 [0x43be54]
??:? __libc_start_main [0x3de282f]
??:? [0x406fb8]
Program exited with code 1

As far as I know scope(failure) should be collecting all failure 
cases. And my application shouldn't crush with uncaught 
exceptions. What I am missing?







Problem with digest Hmac sha256 method and another problem while using openssl hmac

2017-12-08 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I need to have the same result while using :

 openssl dgst -sha256 -hmac "somestring"

But the server rejecting my generated hmac with the code below .

auto hmac = HMAC!SHA256("somestring".representation);
hmac.put(url.representation);
auto generatedHmac = hmac.finish();
string generatedHmacStr = 
std.digest.digest.toHexString(generatedHmac);


Saying generatedHmac is not correct.

Than I wanted to give a try with deimos-openssl like:

ubyte[32] hmac_sha256( string key, string data) {

   HMAC_CTX *ctx = new HMAC_CTX;
   HMAC_CTX_init(ctx);

   auto digest = HMAC(EVP_sha256(),
  cast(void *) key,
  cast(int) key.length,
  cast(ubyte*) data,
  cast(int) data.length,
  null,null);
   ubyte[32] array = digest[0..32];
   return array;
}

But I am getting a linking error :

undefined reference to 
`_D6deimos7openssl3evp13env_md_ctx_st6__initZ'



My dependencies in dub.json file is like:

"dependencies": {
"openssl": "~>1.1.6+1.0.1g",
"vibe-d": "~>0.8.1",
  }


I really want to solve linking error first. I tried installing 
openssl it didn't help. Any suggestions?


Regards
Erdem






One liner alternatives for initing and pushing some values into array

2017-11-20 Thread kerdemdemir via Digitalmars-d-learn

I need to init and push some values into a array something like:

//DMD64 D Compiler 2.072.2
import std.stdio;
import std.array;

void main()
{
bool a = true;
bool b = false;
bool c = false;

bool[] boolList;
auto boolListAppender = boolList.appender();
boolListAppender ~= [ a, b, c];
}

Live example : http://rextester.com/TJLIXU71426

My question is about

bool[] boolList;
auto boolListAppender = boolList.appender();
boolListAppender ~= [ a, b, c];


I could do the same in one line with C++:

bool boolList[] = { a, b, c };

D alternative(at least my D alternative) seem long. Is there any 
better way for

initialing  and pushing values at the same time.

Erdem



Re: Lambda cannot access frame of function

2017-11-18 Thread kerdemdemir via Digitalmars-d-learn
On Saturday, 18 November 2017 at 14:30:29 UTC, Adam D. Ruppe 
wrote:
On Saturday, 18 November 2017 at 14:22:19 UTC, kerdemdemir 
wrote:

bool foo(  bool function( double ) controlFoo )


Change that `function` to `delegate` and it should work.

Function pointers aren't allowed to access other locals, but 
delegates are.


Yes it worked as you suggested.

Thanks.


Re: Json

2017-11-18 Thread kerdemdemir via Digitalmars-d-learn
I am using vibe.d's json(http://vibed.org/api/vibe.data.json/) 
module without a problem and really happy with it. There are also 
some 
examples(https://github.com/vibe-d/vibe.d/tree/master/examples/json). If you are using "dub" package manager it is also very easy to integrate vibe.d.


Lambda cannot access frame of function

2017-11-18 Thread kerdemdemir via Digitalmars-d-learn

//DMD64 D Compiler 2.072.2
import std.stdio;

bool foo(  bool function( double ) controlFoo )
{
return controlFoo(5.0);
}

void foo2( double val )
{
writeln  ( foo( a => a > val ) );
}

void main()
{
foo2(20);
writeln("Hello, World!");
}

Does not compile and gives this errors:
source_file.d(12): Error: function source.foo2.__lambda2 cannot 
access frame of function source.foo2
source_file.d(12): Error: function source.foo (bool 
function(double) controlFoo) is not callable using argument types 
(void)


Live example:
http://rextester.com/WRKCWR55408

Is there any workaround for that?

Regards
Erdem



Re: Removing some of the elements from vibe.core.concurrency.Future[] futurelist

2017-10-31 Thread kerdemdemir via Digitalmars-d-learn

I'd take a look at why the error message says
`Future!(UserData)[]) to Future!(AnalyzeData)[]`
is AnalyzeData  the type returned by ProcessResponceData?

Alternatively you could use a singly linked list and splice out 
elements that pass the filter predicate. I think you'd have to 
roll your own though.


I am sorry Wilson I normally in my code UserData is AnalyzeData = 
UserData but I replace the name to make it more understandable.


For now I solved my problem like

vibe.core.concurrency.Future!(UserData)[] futurelist;
foreach( elem; elemList )
{
auto future = vibe.core.concurrency.async( 
 );

futurelist ~= future;
}

int maximumSleepTime = 0;
while(futurelist.any!(a => !a.ready())) //--> Blocking until all 
of them ready

{
maximumSleepTime++;
sleep(10.msecs);
if ( maximumSleepTime  > 2000 )
  return;
}

futurelist.each!(a=> Process(a.getResult()));

Unfortunately that is really bad when I make 250 web requests and 
only one of them is bad(I mean not ready). And 249 request have 
to wait for the bad one.


I will take a deeper look to data structure you suggested.

Thanks


Removing some of the elements from vibe.core.concurrency.Future[] futurelist

2017-10-28 Thread kerdemdemir via Digitalmars-d-learn

I am trying to make non blocking web requests to a web service.

vibe.core.concurrency.Future!(UserData)[] futurelist;

// I will make http requests in for loop and push them to 
futureList

foreach( elem; elemList )
{
// In makeWebRequest I make the httprequest, parse json 
and push to my UserData
auto future = vibe.core.concurrency.async( 
 );

futurelist ~= future;
}


// I want to do call ProcessResponceData for each future which is 
ready.

while ( futurelist.length > 0 )
{
futurelist.filter!(a => a.ready()).each!(a=> 
ProcessResponceData(a.getResult()));


//!  Here is my problem
//!  I want to remove the futures which are already 
processed.

futurelist = futurelist.filter!(a => !a.ready()).array;  
sleep(10.msecs);

}

I am having troubles while trying to remove the future which I 
already processed.


futurelist = futurelist.filter!(a => !a.ready()).array;	 results 
with:


/usr/local/bin/../import/std/array.d(2716,20): Error: can't have 
array of nothrow @safe void()
/usr/local/bin/../import/std/array.d(2782,46): Error: can't have 
array of inout nothrow @safe void()
/usr/local/bin/../import/std/array.d(3158,1): Error: template 
instance std.array.Appender!(Future!(UserData)[]) error 
instantiating
/usr/local/bin/../import/std/array.d(133,32):instantiated 
from here: appender!(Future!(UserData)[])


futurelist = futurelist.filter!(a => !a.ready()); results with:

 Error: cannot implicitly convert expression (filter(futurelist)) 
of type FilterResult!(__lambda5, Future!(UserData)[]) to 
Future!(AnalyzeData)[]


Do you have any suggestion or workaround for my case?
What lesson I should learn from this case? Do you guys have any 
idea why I am getting those compile errors?



Thanks
Erdem




Re: debugging in vs code on Windows

2017-10-13 Thread kerdemdemir via Digitalmars-d-learn

On Friday, 13 October 2017 at 12:55:09 UTC, piotrklos wrote:
I have windows 10, VS Code with code-d and C/C++ language 
extensions. I try to debug but it doesn't work. In particular, 
the debugging doesn't stop on breakpoints. It exits 
immediately. I recompile with -m64 and -g. I use dub to build 
the project. I use unit-threaded and I'm trying to debug a 
unittest build.


Has anyone been able to debug in VS code on Windows? What am I 
doing wrong?


(Rhetorical) Why is dlang community provide so many options 
(see https://wiki.dlang.org/Debuggers) and **every single one** 
of them is faulty in some way? I tried windbg and mago-mi but 
didn't gen anywhere.


I am using VisualD(https://github.com/dlang/visuald/releases) 
with vs2015 community version(free) and I can debug. I highly 
recommend it if you haven't tried yet. Those options like "-g"  
is presented to you with interfaces. Forexample "-g" is 
automatically is being added if you are selecting debug 
builds(like Debug DMD or Debug LDC).





Re: Need importing dcompute.lib into my project

2017-10-13 Thread kerdemdemir via Digitalmars-d-learn

On Friday, 13 October 2017 at 16:36:06 UTC, Ali Çehreli wrote:

On 10/13/2017 08:47 AM, kerdemdemir wrote:

> I changed my dependency setting in dub file to "dcompute":
"~>0.0.0" but
> still getting the same error message. By the way I am sure my
LDC
> version is good because I can build DCompute source code with
same LDC
> version.

Could this be related to difference in platform like Windows 
vs. Linux? If so, may be it makes sense to work in a VM that 
has Nicholas's exact setup.


Ali


It make sense for me to switch back to linux.  I ruined my 
multiboot linux system which has only 20gb root partition while 
trying to install CUDA libraries(which takes a lot of space.). 
Maybe as you suggest instead of trying with windows I need to 
reinstall my linux.


Re: Need importing dcompute.lib into my project

2017-10-13 Thread kerdemdemir via Digitalmars-d-learn

On Sunday, 8 October 2017 at 07:51:12 UTC, Nicholas Wilson wrote:

On Saturday, 7 October 2017 at 10:34:15 UTC, kerdemdemir wrote:
do you set "-mdcompute-targets=cuda-xxx" in the dflags for 
your dub.json for your project?


I have added now after your comment.

But it seems it didn't changed anything.

Here is the dub.json file I have:
{
"name": "dsharpear",
"authors": [
"Erdem"
],
"dflags" : ["-mdcompute-targets=cuda-210" ,"-oq", "-betterC"],
"dependencies": {
"dcompute": ">=0.0.0-alpha0 <0.1.0"
},
"description": "Beamforming with D ",
"copyright": "Copyright © 2017, Erdem",
"license": "proprietary"
}

And running "dub build --compiler=D:\LDCDownload\bin\ldc2.exe 
--force"  fails with:


Performing "debug" build using D:\LDCDownload\bin\ldc2.exe for 
x86.

derelict-util 2.1.0: building configuration "library"...
derelict-cl 2.0.0: building configuration "library"...
derelict-cuda 2.0.1: building configuration "library"...
..\..\..\..\..\AppData\Roaming\dub\packages\derelict-cuda-2.0.1\derelict-cuda\source\derelict\cuda\runtimeapi.d(816,5):
 Deprecation: constructor derelict.cuda.runtimeapi.dim3.this
all parameters have default arguments, but structs cannot have 
default constructors.

dcompute 0.0.0-alpha0: building configuration "library"...
Targeting 'i686-pc-windows-msvc' (CPU 'pentium4' with features 
'')

Building type: real
Building type: uint
Building type: char
Building type: ubyte
Building type: ulong
Building type: int
Building type: double
Building type: long
Building type: ushort
Building type: wchar
Building type: byte
Building type: short
Building type: float
Building type: dchar
..\..\..\..\..\AppData\Roaming\dub\packages\dcompute-0.0.0-alpha0\dcompute\source\dcompute\std\package.d(6,5):
 Error: static assert  "Need to use a DCompute enabled compiler
See https://github.com/thewilsonator/ldc/tree/dcompute;
D:\LDCDownload\bin\ldc2.exe failed with exit code 1.


You are using an old DCompute: that message was changed in 
https://github.com/libmir/dcompute/commit/cf1420d5377c48f9132f1a9a0f8f06ebb46a6433#diff-b0637c2cde07f2ec8f77f9e3d379fff7


I'm not quite sure how the dub version specs work but I believe 
"dcompute": "~>0.0.0" should work, or you can force it to use 
master.
I'm still not sure why it fails with that error though, are you 
using an old LDC as well? 1.4.0 should work.


I changed my dependency setting in dub file to "dcompute": 
"~>0.0.0" but still getting the same error message. By the way I 
am sure my LDC version is good because I can build DCompute 
source code with same LDC version.


Re: Need importing dcompute.lib into my project

2017-10-07 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 7 October 2017 at 12:12:10 UTC, kinke wrote:

On Saturday, 7 October 2017 at 09:04:26 UTC, kerdemdemir wrote:

Error: static assert  "Need to use a DCompute enabled compiler"


Are you using latest LDC 1.4? The CUDA backend wasn't enabled 
for earlier versions.


Yes I am. Actually I can build dcompute's source code so I am 
sure LDC version is good. Now I am trying to use dcompute within 
my project.


Re: Need importing dcompute.lib into my project

2017-10-07 Thread kerdemdemir via Digitalmars-d-learn
do you set "-mdcompute-targets=cuda-xxx" in the dflags for your 
dub.json for your project?


I have added now after your comment.

But it seems it didn't changed anything.

Here is the dub.json file I have:
{
"name": "dsharpear",
"authors": [
"Erdem"
],
"dflags" : ["-mdcompute-targets=cuda-210" ,"-oq", "-betterC"],
"dependencies": {
"dcompute": ">=0.0.0-alpha0 <0.1.0"
},
"description": "Beamforming with D ",
"copyright": "Copyright © 2017, Erdem",
"license": "proprietary"
}

And running "dub build --compiler=D:\LDCDownload\bin\ldc2.exe 
--force"  fails with:


Performing "debug" build using D:\LDCDownload\bin\ldc2.exe for 
x86.

derelict-util 2.1.0: building configuration "library"...
derelict-cl 2.0.0: building configuration "library"...
derelict-cuda 2.0.1: building configuration "library"...
..\..\..\..\..\AppData\Roaming\dub\packages\derelict-cuda-2.0.1\derelict-cuda\source\derelict\cuda\runtimeapi.d(816,5):
 Deprecation: constructor derelict.cuda.runtimeapi.dim3.this
all parameters have default arguments, but structs cannot have 
default constructors.

dcompute 0.0.0-alpha0: building configuration "library"...
Targeting 'i686-pc-windows-msvc' (CPU 'pentium4' with features '')
Building type: real
Building type: uint
Building type: char
Building type: ubyte
Building type: ulong
Building type: int
Building type: double
Building type: long
Building type: ushort
Building type: wchar
Building type: byte
Building type: short
Building type: float
Building type: dchar
..\..\..\..\..\AppData\Roaming\dub\packages\dcompute-0.0.0-alpha0\dcompute\source\dcompute\std\package.d(6,5):
 Error: static assert  "Need to use a DCompute enabled compiler
See https://github.com/thewilsonator/ldc/tree/dcompute;
D:\LDCDownload\bin\ldc2.exe failed with exit code 1.


Re: Need importing dcompute.lib into my project

2017-10-07 Thread kerdemdemir via Digitalmars-d-learn

You should add DCompute as a DUB dependancy.


Hi,

I inited my project with Dub by unsing  "dub init DSharpEar" and 
I added dependency "dcompute".


Even I give extra parameters for using ldc.

 dub build --compiler=D:\LDCDownload\bin\ldc2.exe --force

I am getting :

Error: static assert  "Need to use a DCompute enabled compiler"

Any suggestions?

Regards
Erdem


Re: For fun: Expressive C++ 17 Coding Challenge in D

2017-10-06 Thread kerdemdemir via Digitalmars-d-learn

I am a total beginner but I want to post that a lot.

auto autoCorrelation(R)(R range)
if (isRandomAccessRange!R)
{

import std.numeric : fft, inverseFft;
import std.range : chain, repeat, zip, dropBack;
import std.algorithm : map;
import std.complex;

auto residual = residualPowerOf2(range.length);
auto fftResult = range.chain(repeat(0, residual)).fft();
	auto autoCorrResult = fftResult.zip(fftResult.map!(a => 
a.conj())).

map!( a=> a[0] * a[1] ).
inverseFft().
dropBack(residual).
map!( a => a.re );

return autoCorrResult;
}   

I implemented auto correlation in C++ before which is I believe 
2~3 time bigger(also I needed to compile fftw, lapack etc.. ) :

https://forum.kde.org/viewtopic.php?f=74=118619 .

That was the moment I feel in love with D.



Need importing dcompute.lib into my project

2017-10-06 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I have a cuda kernel already working in my cpp 
project(https://github.com/kerdemdemir/CUDABeamformer/blob/master/CudaBeamformer/kernel.cu)


I am trying to convert this to D with using DCompute. I already 
compiled the DCompute source code and have dcompute.lib. But I am 
really not good with using libraries and don't know how to use 
DCompute in my D 
project(https://github.com/kerdemdemir/DSharpEar/tree/master/DSharpEar). I am not using DUB and compiling with VisualD.


Can you guys please help with importing DCompute into my project?

Regards
Erdem



Error 16: Index Range error while building examples from D Web development

2017-09-17 Thread kerdemdemir via Digitalmars-d-learn

Hi,

Thanks its price dropped to 10 Euros I bought the the D Web 
Development book and I were trying to build some examples.

The example in Chapter3 called noteapp4 is giving me this error :

Performing "debug" build using dmd for x86.
noteapp4 ~master: building configuration "application"...
C:\Users\Erdem\AppData\Roaming\dub\packages\vibe-d-0.7.32\vibe-d\source\vibe\http\server.d(286,33):
 Deprecation: alias diet.traits.FilterCallback is deprecated - Use 
SafeFilterCallback instead.
Compiling Diet HTML template create.dt...
Compiling Diet HTML template listnotes.dt...
Linking...
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
.dub\build\application-debug-windows-x86-dmd_2075-102ED5AC10E8F023CF2D8D690787ADD9\noteapp4.obj(noteapp4)
  Offset 23F7EH Record Type 009D
 Error 16: Index Range
Error: linker exited with status 1
dmd failed with exit code 1.

Meanwhile hello world example compiling. I am on windows 10 and

C:\D\dmd2\windows\bin>dmd.exe --version
DMD32 D Compiler v2.075.0


Do you guys have any idea about what I am doing wrong?

Thanks


Re: Efficiently streaming data to associative array

2017-08-09 Thread kerdemdemir via Digitalmars-d-learn
I haven't yet dug into formattedRead but thx for letting me 
know : )
I was mostly speaking about the pattern with the AA. I guess 
the best I can do is a templated function to hide the ugliness.



ref Value GetWithDefault(Value)(ref Value[string] map, const 
(char[]) key) {

  auto pValue = key in map;
  if(pValue) return *pValue;
  return map[key.idup] = Value.init;
}

void main() {

  size_t[string][string] indexed_map;

  foreach(char[] line ; stdin.byLine) {
char[] a;
char[] b;
size_t value;
line.formattedRead!"%s,%s,%d"(a,b,value);

indexed_map.GetWithDefault(a).GetWithDefault(b) = value;
  }

  indexed_map.writeln;
}


Not too bad actually !


As a total beginner I am feeling a bit not comfortable with basic 
operations in AA.


First even I am very happy we have pointers but using pointers in 
a common operation like this IMHO makes the language a bit not 
safe.


Second "in" keyword always seemed so specific to me.

I think I will use your solution "ref Value 
GetWithDefault(Value)" very often since it hides the two things 
above.






Best syntax for a diagonal and vertical slice

2017-07-22 Thread kerdemdemir via Digitalmars-d-learn

We have awesome way for creating slices like:

a = new int[5];
int[] b = a[0..2];

But what about if I have 2D array and I don't want to go 
vertical. Something like :


int[3][3] matrix = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
];

I believe I can use std.range function "RoundRobin"(or maybe it 
won't work with 2D array directly) for having a good looking 
vertical slice which will have 1,4,7 or 2,5,8 or 3,6,9 in my 
example above.


And what if I want to go diagonal like 1,5,9 or 3,5,7 in the 
example above. Is there a good solution in std without using for 
loops?


I have one more requirement for fulfilling the task that I 
working on. This slices do not have to be the same size as the 
array. For example in the example above slice size could have 2 
instead of 3. In this case I need to have slices like 
1,5;2,6;4,8;5,9 ... and so on for diagonal case.


Erdem

Ps: Converting the 2D array to 1D array is possible in my case.


Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-17 Thread kerdemdemir via Digitalmars-d-learn
As for the problem itself, it can be solved without finding 
connected components.  I won't post the solution right away 
because it is potentially a spoiler.  See 
http://codeforces.com/blog/entry/53268 for problem analysis 
(828B) and 
http://codeforces.com/contest/828/submission/28637184 for an 
example implementation in D.


Ivan Kazmenko.


Wow I understand the question wrongly than,

B
W
WWWBB
WWBWW

I excepted there should be two B squares here.

One 3*3 and another 1*1.
And my answer would be 6 instead 12 here.

Yes if there can be only one black square than my solution would 
be much more simple.


One good thing about D community is when I ask a question here 
you guys are really nice that I get inspired.


Thanks Ivan




Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread kerdemdemir via Digitalmars-d-learn
Of course now I will try to have it work first. Than replace for 
loops with Cartesian product calls. Than I will make 2D array 
template and even maybe with random access range. And finally for 
being able to use this class later in the some other coding 
challenge I will make Searching( == 'W' part) and Process 
functions passed by parameter.


Thanks a lot for help.
Erdem


Re: Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread kerdemdemir via Digitalmars-d-learn

Hi Guys,

@Nicholas , thanks a lot for cool solution but actually I weren't 
working on image processing. I was trying to solve 
"http://codeforces.com/contest/828/problem/B;. I really needed 
finding connected components this time.


@Ivan, your solution is much more elegant than what I did. But I 
find @Timon's solution with cartesian product a bit nicer in this 
case since I love to see std function more and more.


Thanks guys for all your advises. D community is really the best.

Here is my solution to question. It seems I didn't get it working 
completely yet. In my debugger(Msvc MonoD) even there are many 
rows it seems Recurse function only loops the columns in the 
first row. And debugger is jumping so strangely I couldn't tag 
the problem.
But I don't expect a big change there should be a small bug that 
is it.


Sorry if code contains some foreign words I just replaced many 
variable names from my native language I might be missing some.


import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
import std.range;
import std.math;

int totalrow;
int totalcolumn;
dchar[][] twoDimensionArray;

struct ConnectedElementsSolver
{
this(  dchar[][] twoDimArray )
{
m_twoDimStruct = twoDimArray;
Recurse(0, 0);
}

void Recurse ( int row, int column )
{
if( row < 0 || column < 0  )
return;

for ( ; row <  m_twoDimStruct.length ; row++  )
{
for ( ; column <  m_twoDimStruct[row].length; column++  
)
{
Process( row, column, m_twoDimStruct.length, 
m_twoDimStruct[row].length );

}
}
}

	void Process( int row, int column, ulong maxrow, ulong maxcolumn 
)

{
		if( row < 0 || column < 0 || row >= maxrow || column >= 
maxcolumn  )

return;

if (  m_twoDimStruct[row][column] == 'B' )
{
m_twoDimStruct[row][column] = 'W';
m_tempResult.Process(row, column );
Process(row-1,column-1, maxrow, maxcolumn);
Process(row,column-1, maxrow, maxcolumn);
Process(row+1,column-1, maxrow, maxcolumn); 

Process(row-1,column, maxrow, maxcolumn);   

Process(row+1,column, maxrow, maxcolumn);   

Process(row-1,column+1, maxrow, maxcolumn); 

Process(row,column+1, maxrow, maxcolumn);   
Process(row-1,column+1, maxrow, maxcolumn);
}
else
{
if ( m_tempResult.HowManyFilled )
m_results ~= m_tempResult;
m_tempResult.Resetle();
}   
}

SquareCandidate   m_tempResult;
SquareCandidate[] m_results;
dchar[][] m_twoDimStruct;
}

struct SquareCandidate
{
int MaxY;
int MinY;
int MaxX;
int MinX;
int HowManyFilled;

this( int howManyFilled )
{
HowManyFilled = howManyFilled;
}

void Resetle()
{
this = SquareCandidate();
}


void Process( int row, int column )
{
HowManyFilled++;
MaxY = max( column, MaxY);
MinY = min( column, MinY);
MaxX = max( row, MaxX);
MinX = min( row, MinX);
}

int FindEmptySlots()
{
int kareKenarUzunlugu = max(MaxX-MinX, MaxY-MinY);
int kareAlani = kareKenarUzunlugu*kareKenarUzunlugu;
return kareAlani - HowManyFilled;
}

bool CanCreateSquare( int xMax, int yMax )
{
int xUzunlugu = MaxX-MinX;
int yUzunlugu = MaxY-MinY;
if ( xUzunlugu > yUzunlugu )
{
return yMax >= xUzunlugu;
}
else
{
return xMax >= yUzunlugu;
}
}
}


void main()
{
auto dimensions = stdin.readln.strip.split().map!(a => 
to!int(a)).array();

totalrow = dimensions[0];
totalcolumn = dimensions[1];
twoDimensionArray = stdin
.byLine()
.take(totalrow)
.map!(line => line
  .map!(a => to!dchar(a))
  .array())
.array;

	ConnectedElementsSolver baglantiliElemCozucu = 
ConnectedElementsSolver(twoDimensionArray);
	bool isAnyProblemMakingSquare = 
baglantiliElemCozucu.m_results.any!(a => 

Avoid if statements for checking neighboring indexes in a 2D array

2017-07-16 Thread kerdemdemir via Digitalmars-d-learn
My goal is to find connected components in a 2D array for example 
finding connected '*'

chars below.

  x x x x x x
  x x x x x x
  x x * * x x
  x x * * x x
  x x x * * x
  * x x x x x


There are two connected '*' group in this example. First group is 
composes of six '*' located closer to middle and the second group 
composes only one '*' char located in the left bottom corner.


Do to this I generally implement a recursive algorithm which 
repeat calling the same function by checking all neighbors around 
the current index. I generally end up with something like :


void foo( int row, int col)
{
//Do something here like caching the index

if ( twoDimensionData[row - 1][col] == '*')
   foo(row- 1, col);
else if ( twoDimensionData[row + 1][col] == '*')
   foo(row+ 1, col);
else if ( twoDimensionData[row - 1 ][col - 1] == '*')
   foo(row - 1, col - 1);

//. I need 5 more of this bad boys I mean if checks
}

Is there any better way to achieve this with cool std functions 
like enumerate or iota without needing to write eight if checks?


VisualD building with GDC setting library path

2015-07-18 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I am tring to build Cristi Cobzarenco's fork of Scid which has 
LAPACK,BLAS dependency.


I add all modules of Scid to my project and I am tring to build 
it within my project.


I add LibraryFiles: liblapack.a libblas.a libtmglib.a 
libgfortran.a etc.. via menu
configuration properties--Linker -- General --- LibraryFiles . 
Even though  I set C:\Qt\Tools\mingw491_32\i686-w64-mingw32\lib 
path which has libgfortran.a from Visual D Settings --Library 
Paths;


I am getting error :

gdc: error: libgfortran.a: No such file or directory.

Libraries only works if I copy and paste them to my project 
folder. And coping all libraries fortran.a , pthread.a etc... 
seems not logical to me.


I read there is a known isssue with sc.ini but it should only be 
with DMD not with GDC. How can I set library path with visualD 
while building with GDC?


Getting Nth Row and Column from MatrixView, Scid library

2015-07-17 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I want to use Scid  matrixes for implementing GMM algorithm. I 
will start by writing Naive Bayes with linear and quadratic 
decision boundaries .


I reliaze Scid does not provides any functions for getting 
spesific row or coloumn. Matrixwview class only supplies opIndex 
function.


Is there anything that I am missing or what is the best to get 
nth column or row with Scid library.


Link to Scid lib: 
http://www.kyllingen.net/code/scid/doc/scid_matrix.html


Re: Getting Nth Row and Column from MatrixView, Scid library

2015-07-17 Thread kerdemdemir via Digitalmars-d-learn

Sad times with linear algebra libraries for me,

Since I can't get  rows and columns easily with Scid, It seems 
not flexible for me. And also there are other issues for example 
I can't set matrix to row or column major.


I begin to check alternatives first I begin to investigate Dlib. 
D lib matrixes are unfortunately static once created their size 
can't be expanded  and must be square matrix. This restrictions 
seem too much to me .


The best alternative seems to me scid fork of Cristi Cobzarenco 
link: https://github.com/cristicbz/scid. It has the basic 
features I need. But it seems last commit made 3 years ago. I 
couldn't compile it. I am getting error:


scid\internal\regionallocator.d(364): Error: safe function 
'scid.internal.region
allocator.threadLocalSegmentSize' cannot call system function 
'scid.internal.reg

ionallocator.RegionAllocatorException.this'
Error building library

Any advice about another lib or fix compile error will be welcome.


Linear algebra library with no dependency Why every linear algebra library has LAPACK dependency

2015-07-03 Thread kerdemdemir via Digitalmars-d-learn
This question is not only about D linear algebra libraries but 
also for other linear algebra libraries in other languages.


I am working with some scientific developers in my current 
project.


When we were talking I said I know a great linear algebra 
library LAPACK but my friend who is very experienced about 
numeric told me LAPACK isn't the best library for performance 
especially if matrix is sparse. In fact he said every numeric 
developer will try to avoid LAPACK.


Now I want to implement some statistical methods like Bayes, GMM. 
And I need a linear algebra library. I am looking for a native 
module code which I can directly include my project without dll 
or I am looking for a library without any dependency. But I see 
all linear algebra libraries has dependency to LAPACK.


I am sure I am asking this question because I am lacking domain 
information about maths and linear algebra. But why all libraries 
has dependency to LAPACK. What make LAPACK irreplaceable ?


Ps: I am not sure if asking questions related to D but not %100 
about D is a bad habit. If it is please warn me.


Autocorrelation function with ranges

2015-06-27 Thread kerdemdemir via Digitalmars-d-learn

Hi

My question is more about Maths than D lang,
I am hoping, maybe somebody worked with AutoCorrelation function 
before.



auto autoCorrelation(R)(R range)
if (isRandomAccessRange!R)
{
auto residual = residualPowerOf2(range.length); // Find how 
many zeros to add
auto fftResult = range.chain(repeat(0, residual)).fft(); // 
Takes FFT


//First zip each element of fft with conjagute of fft
auto autoCorrResult = fftResult.zip(fftResult.map!(a = a * 
a.conj())).

map!( a= a[0] * a[1] ). // Than multiple them
inverseFft(). // Than take inverse
dropBack(residual);//Drop the additional zeros

auto finalResult = autoCorrResult.take(1). // Take DC element
		chain(autoCorrResult[$/2..$]).//Take last half to 
beginning
		chain(autoCorrResult[1..$/2]). // First(negative lags) 
to end

map!(a = a.re); // I just need real part

return finalResult ;
}


My autocorrelation method return some crazy values(finalResult[0] 
= -101652). I am mostly suspicious about calculations to set 
finalResult  variable.


Also is there any performance issues? can I make this faster?




Re: Autocorrelation function with ranges

2015-06-27 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 27 June 2015 at 10:37:08 UTC, Rikki Cattermole wrote:

No idea about the maths behind it are but:


Thanks a lot for your answer anyway. I am hoping even not related 
with D directly, this discussions may atract people from other 
languages to D while looking for Domain information.




chain(autoCorrResult[1..$/2])

Should that one be a zero?


I found this link below.
http://www.aip.de/groups/soe/local/numres/bookcpdf/c13-2.pdf;
Which says :
The correlation at zero lag is in r0, the first component; the 
correlation at lag 1 is in r1, the second component; the 
correlation at lag −1 is in rN−1, the last component; etc.


Correlation result after IFFT is like :

0 1 2 3 . T -1 -2 -3  -T

How I wanted to be :

-T -T+1 .-1 0 1  T-1 T

After reading the link I think you are right,

auto finalResult =  chain(autoCorrResult[$/2..$]).
chain(autoCorrResult[0..$/2]).
 map!(a = a.re);

Example above should be the way how I transform. Also now I see
inverseFft().
dropBack(residual); == DropBack may not be a good idea here. 
I will think about it


Re: Autocorrelation function with ranges

2015-06-27 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 27 June 2015 at 12:17:31 UTC, Timon Gehr wrote:



This computes a²·a̅ instead of a·a̅.

What is the source code for residualPowerOf2?


Also is there any performance issues? can I make this faster?



Probably you should use 
http://dlang.org/phobos/std_complex.html#.sqAbs instead. You 
then also don't need the final map to extract the real part.


You are absulately right instead
fftResult.zip(fftResult.map!(a = a * a.conj()))
I corrected it to
fftResult.zip(fftResult.map!(a = a.conj()))

Thanks a lot


Using C library libsndfile with D

2015-06-25 Thread kerdemdemir via Digitalmars-d-learn

Hi

I want to read wav files. I thought I can use libsndfile since I 
am quite familiar with it.


I downloaded the project from.
https://github.com/D-Programming-Deimos/libsndfile

I add the sndfile.di file to my project.

I thought I could directly use  libsndfile.dll since D directly 
supports C. But I am getting link errors.


Can I use a C DLL directly ?
Do I have to add anything to my dub.json file if dll is my 
project file ?
What is Deimos stands for ?  Is it collections of bindings ? 
Does community supports Deimos projects?


Re: Passing functions as template parameter and assigning default values to them

2015-06-21 Thread kerdemdemir via Digitalmars-d-learn

On Sunday, 21 June 2015 at 10:06:15 UTC, biozic wrote:
You can use a template alias parameter with a default value 
that is your default lambda:


int indexOfMax(alias fun = a = a, R)(R range)
{
// Use `fun` here like a function.
}

-- Nico


Thanks a lot, it works !!


Passing functions as template parameter and assigning default values to them

2015-06-21 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I need to find the index of maximum element so my code:

int indexOfMax(R)(R range)
{   
alias Type = typeof(range.front().re);   I don't like 
.re here

Type max = 0;
size_t maxIndex = 0;
foreach ( index,elem; range )
{
if ( elem.re  max )- And also here
{
max = elem.re;
maxIndex = index;
}
}
return maxIndex;
}

Since my range contains Complex!double types I have to add .re 
for current implementation. But I want to be more generic. Like 
how std.algorithm.map is.

Like: range.map!(a = a.re).

So what I want to achive is :

indexOfMax!(a = a.re)(complexRange);  implement algorithm 
on a.re
indexOfMax(intRange); - if a function is not given act like 
(a = a)


Any advice with template function parameters or mixins will make 
me happy.


Regards
Erdem




Re: appender!(dchar[]) put fail

2015-06-13 Thread kerdemdemir via Digitalmars-d-learn

On Saturday, 13 June 2015 at 13:09:20 UTC, Dennis Ritchie wrote:


auto stringB = readln.chomp.map!(to!dchar).array;
auto stringC = readln.chomp.map!(to!dchar).array;

auto charAppender = appender!(dchar[][]);

auto totalStr = stringB.repeat(3).chain(stringC.repeat(5));

charAppender.put(totalStr);

writeln(charAppender);

charAppender.put(cd.dup);
charAppender.put(testd.dup);

writeln(charAppender);


Thanks lot that is really good.

One more question I am asking those kind of questions to 
understand and not ask same stuff over and over, :


auto totalStr = chain(stringB.replicate(bCount), 
stringC.replicate(cCount));

writeln(typeof(totalStr.array()).stringof);
dchar[]

But
auto totalStr = chain(stringB.repeat(bCount), 
stringC.repeat(cCount));

writeln(typeof(totalStr.array()).stringof);
dchar[][]

It seems to me a little inconsistent. range.repeat and 
array.replicate gives result in difference dimension.


Is there any explanation or logic that I am missing which results 
this behaviour?


Re: appender!(dchar[]) put fail

2015-06-13 Thread kerdemdemir via Digitalmars-d-learn
It is the same, but totalStr is not a dchar[]. It's a Result (a 
type internal to the chain function ) which is a range. The 
foreach loop iterates over Result, which returns dchar[].


So if you try to do something like that :

charAppender.put(totalStr.array), it won't work because 
totalStr.array is a dchar[][].


Sorry to making the discussion longer and wasting your times.

But I am looking for a way without for loops. Also looping every 
element one by one does not seems very efficient to me. Any 
advices for that?


Re: appender!(dchar[]) put fail

2015-06-13 Thread kerdemdemir via Digitalmars-d-learn


The problem is that your appender is a char appender, and you 
try to put a dstring into it. Replace :


charAppender.put(totalStr);

by :

foreach(elem; totalStr){
   charAppender.put(elem);
}

elem will be a dchar, so it will work.



But I can see in the example of 
array.appander(http://dlang.org/phobos/std_array.html#appender)


int[] a = [ 1, 2 ];
auto app2 = appender(a);
app2.put(3);
app2.put([ 4, 5, 6 ]);  --- appender accepts another array.

Why this is not same with dchar ?


appender!(dchar[]) put fail

2015-06-13 Thread kerdemdemir via Digitalmars-d-learn
I have two strings(stringB,stringC) which I need to repeat(bCount 
times, cCountTimes) and then chain.


auto charAppender = appender!(dchar[]);
auto totalStr = 
stringB.repeat(bCount).chain(stringC.repeat(cCount));


This compiles and works ok,
But when I try to append new string to charAppender :

charAppender.put(totalStr); 
Error: template std.array.join cannot deduce function from 
argument types


I tried:
charAppender.put(totalStr.array());
charAppender.data.chain(totalStr);
charAppender.data.chain(totalStr.array()); etc...

I always get compile errors.
Do you have any idea or fix about that?

Also what is the difference between algorithm.joiner without 
seperator and range.chain?



As requested before, this time I will copy full code,

int[dchar] mapA;
int includeCounter(T)(T tuple)
{
int curMax = 10;

foreach ( elem ; tuple )
{
int numberInA = 0;
if (elem[0] in mapA)
numberInA = mapA[elem[0]] ;
else
{
curMax = 0;
break;
}

if (  numberInA  elem[1] )
{
curMax = 0;
break;
}
else
{
auto newCount = numberInA / elem[1];
if ( newCount  curMax )
curMax = newCount;
}
}
if (curMax  0)
{
foreach (  elem ; tuple )   
{
mapA[elem[0]] -= curMax;
}
}

return curMax;  
}

void readInput()
{
size_t lineSize;

	auto stringA = stdin.readln.chomp().map!( a = 
to!dchar(a)).array();
	auto stringB = stdin.readln.chomp().map!( a = 
to!dchar(a)).array();
	auto stringC = stdin.readln.chomp().map!( a = 
to!dchar(a)).array();


foreach ( elem ; stringA)
mapA[elem]++;

auto tupleB = stringB.group();
auto tupleC = stringC.group();

auto bCount = includeCounter( tupleB );
auto cCount = includeCounter( tupleC );

auto charAppender = appender!(dchar[]);
foreach ( elem ; mapA.keys)
{
int* count = mapA[elem];
if ( *count  0)
{
while((*count)--)
charAppender.put(elem) ;
}
}

	auto totalStr = 
stringB.repeat(bCount).chain(stringC.repeat(cCount));

charAppender.put(totalStr); 
}

void main(  string[] args )
{
readInput();
}


How to pass voldemort types to functions

2015-06-12 Thread kerdemdemir via Digitalmars-d-learn

Hi;

I have tuples created by std.algorithm.group function.  

auto tupleB = stringB.group();

I need to write a a function which takes tubleB and do some cool 
stuff. If I don't use a function and write all code below 
.group() everytihng works but for reusing the code I want to call 
a function with my tuples.


I tried ;

void foo(T...)(T tuple)
void foo(Tuple!(dchar,uint) tuplle)

But even couldn't compiile.

Do you have any idea for passing result of std.algorithm.group() 
to my free function?


Re: Reading array of integers readln performance issues

2015-06-12 Thread kerdemdemir via Digitalmars-d-learn
Thanks a lot for your great advices and exaamples. Yes if I don't 
return; web-site won't show it as wrong answer.


As a learner I am very happy with the responsiveness of the 
community.


Regards


Re: How to pass voldemort types to functions

2015-06-12 Thread kerdemdemir via Digitalmars-d-learn



void foo(R)(R range)
if (isInstanceOf!(Tuple, ElementType!R))// -- optional
{



Ali thanks a lot. I don't believe I didn't simply try your way. 
It works.
I am also happy to learn optional static if . Your examples are 
really useful for me.


Next time I will share whole code.

Thanks a lot.


Reading array of integers readln performance issues

2015-06-11 Thread kerdemdemir via Digitalmars-d-learn

Hi;

To learn D better and challanging myself I am tring code 
computation's with D.


There is a question which is about reading a line of integer 
which consist of 20 elements.


My solution fails because Time limit exceeded, I thought it is 
because of my algorithm first. I realize time limit is exceeded 
even before my algorithm starts while reading line of integers. I 
understand this by giving a wrong answer to question after readln 
statement. I did that to get a wrong answer error but my code 
still get a Time limit exceed error because readln takes very 
long time.


Can I achieve something faster than code below?

auto peopleMoney = stdin.readln().split().map!(a = 
to!int(a)).array();

if (peopleMoney.length == 20)
 writeln(:();

Regards
Erdem


Ps: I do not want to bore you with long code, but I am sending 
link to whole program anyway if anyone need.

 http://codeforces.com/contest/549/submission/11537206


Re: Writeln does not prints if array has more than 500 elements

2015-06-10 Thread kerdemdemir via Digitalmars-d-learn

I am running DMD on windows, my DMD version is DMD32 V2.067.1.
It might be because I installed 32bit version on 64bit windows.


Writeln does not prints if array has more than 500 elements

2015-06-10 Thread kerdemdemir via Digitalmars-d-learn

Hi

Following code works

int[] peopleMoney = iota(0, 500, 1).array();
writeln(peopleMoney.map!(a = to!string(a)).joiner( ));
= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

It writes the contents to std.output as expected.
But if I change 500 to 600 nothing is being printed.

int[] peopleMoney = iota(0, 600, 1).array();
writeln(peopleMoney.map!(a = to!string(a)).joiner( ));
== NOTHİNG PRINTS

What am I doing wrong?