Re: DConf 2017 Videos

2020-04-24 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Saturday, 25 April 2020 at 04:11:02 UTC, Ali Çehreli wrote:
On 4/24/20 2:11 PM, Steven Schveighoffer wrote:> On 4/24/20 
4:24 PM, matheus wrote:


> whomever controlled the sociomantic youtube account took down
> all the videos.

I think it's unintentional because the same thing happened to 
my Weka account: After I stopped working there, the company 
naturally deleted my account and the videos that were 
associated with that account disappeared from YouTube after a 
grace period. I think it was one month at that time but nobody 
was aware until it was too late.


Apparently the internet can actually forget some things. :)

Ali


Andrei was asking something similar yesterday.

https://twitter.com/incomputable/status/1253342091188502528?s=19



Re: How to import & export modules

2019-11-10 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 11 November 2019 at 01:28:54 UTC, userTY wrote:
On Sunday, 10 November 2019 at 23:53:22 UTC, Vinod K Chandran 
wrote:

[...]


You must use a module that has public imports.
Public imports are visible from the module that contain them 
but most importantly from the module that imports the module 
containing the public imports.


---
module all;

public import App, Form, Button;
---

---
module app;

import all; // can see App, Form and Button exported (public) 
symbols

---

See the specifications [1] for more comprehenssive details.

[1]: https://dlang.org/spec/module.html#public_imports


Just a nitpick, prefer to use D style: 
https://dlang.org/dstyle.html


Modules are essentially files. So keeping them lower case makes 
it easier.


Re: How to use classes from another d files

2019-10-22 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Tuesday, 22 October 2019 at 17:34:51 UTC, Vinod K Chandran 
wrote:

Hi all,
I am new to D. But some fair experience with vb.net. I was 
playing with D classes.  I wrote a class in a D file.

The file name is "classFile.d"
```D
class TestClass {

[...]


What you are seeing is a linker error. Build it as follows:

dmd caller.d classFile.d

Unrelated, first line of classFile.d needs be `module classFile`


Re: D for sciencetific scripting / rapid protoryping

2019-10-22 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Tuesday, 22 October 2019 at 07:51:16 UTC, Arun Chandrasekaran 
wrote:
On Tuesday, 22 October 2019 at 07:40:01 UTC, Prokop Hapala 
wrote:

[...]


If you are building individual files, use ldc2 with 
--link-defaultlib-shared flag:


arun@home-pc:/tmp$ cat a.d
void main() { import std; writeln("Hai"); }
arun@home-pc:/tmp$ ldc2 a.d
arun@home-pc:/tmp$ ldd a
linux-vdso.so.1 (0x7fff6395b000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 
(0x7f1ec91ea000)
libpthread.so.0 => 
/lib/x86_64-linux-gnu/libpthread.so.0 (0x7f1ec91c7000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 
(0x7f1ec9078000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 
(0x7f1ec905e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 
(0x7f1ec8e6d000)

/lib64/ld-linux-x86-64.so.2 (0x7f1ec92c4000)
arun@home-pc:/tmp$ ldc2 a.d --link-defaultlib-shared
arun@home-pc:/tmp$ ldd a
linux-vdso.so.1 (0x7ffcbda7f000)
libphobos2-ldc-shared.so.88 => 
/home/arun/.bin/ldc2-1.18.0-linux-x86_64/bin/../lib/libphobos2-ldc-shared.so.88 (0x7f1b57be8000)
libdruntime-ldc-shared.so.88 => 
/home/arun/.bin/ldc2-1.18.0-linux-x86_64/bin/../lib/libdruntime-ldc-shared.so.88 (0x7f1b57abc000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 
(0x7f1b57a84000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 
(0x7f1b57893000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 
(0x7f1b57744000)
libpthread.so.0 => 
/lib/x86_64-linux-gnu/libpthread.so.0 (0x7f1b57721000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 
(0x7f1b57714000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 
(0x7f1b5770e000)

/lib64/ld-linux-x86-64.so.2 (0x7f1b58067000)
arun@home-pc:/tmp$


If you want similar behavior with dub, here is a sample dub.json

arun@home-pc:/tmp/test$ cat dub.json
{
"authors": [
"Arun"
],
"copyright": "Copyright © 2019, Arun",
"description": "A minimal D application.",
"license": "proprietary",
"dflags-ldc": ["-link-defaultlib-shared"],
"name": "test"
}
arun@home-pc:/tmp/test$

Dub settings can be at times intimidating, but still give it a 
read https://dub.pm/package-format-json#build-settings


Re: D for sciencetific scripting / rapid protoryping

2019-10-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Tuesday, 22 October 2019 at 07:40:01 UTC, Prokop Hapala wrote:

On Tuesday, 22 October 2019 at 07:23:46 UTC, Daniel Kozak wrote:
On Tue, Oct 22, 2019 at 8:00 AM Prokop Hapala via 
Digitalmars-d-learn  wrote:


I'm examining the possibility to move from Python+C/C++ to D 
or

Python+D. I read
(https://wiki.dlang.org/Programming_in_D_for_Python_Programmers)
and
(https://jackstouffer.com/blog/nd_slice.html), where is 
mentioned
PyD, Mir-algorithm, all seems very promising. But I did not 
test

it yet.

 >...

You should try to use https://github.com/BindBC/bindbc-opengl 
and
https://github.com/BindBC/bindbc-sdl. There seems to be an 
issue with

derelict packages (mainly with the gl3 one)
And as far as I know derelict should be replaced by bindbc 
anyway in future.


And if you plan to have *.so libs you should add "targetType" 
: "dynamicLibrary", to you dub.json


OK, thanks. That is useful to know. But just to not turn the 
topic elsewhere I should make clear that:


1) I'm not speaking about OpenGL and SDL specifically (that was 
just small example which I tried first)


2) I'm more concerned about how to D compiler links 
dependencies when it compiles simple .d program (with lot of 
dependencies).


I think if I can make it link everything dynamically, It would 
considerably reduce both size of binary target (whether it is 
executable or .so) and compilation speed (since it would not 
re-compile dependencies).


What I want is to recompile and run quite large 
programs/projects composed composed of many little 
sub-programs/sub-libraries from Python+D in fast cycles (<< 1 
second), because that would make debugging workflow much more 
pleasant and efficient (in comparison to Python+C/C++ or Julia).


If you are building individual files, use ldc2 with 
--link-defaultlib-shared flag:


arun@home-pc:/tmp$ cat a.d
void main() { import std; writeln("Hai"); }
arun@home-pc:/tmp$ ldc2 a.d
arun@home-pc:/tmp$ ldd a
linux-vdso.so.1 (0x7fff6395b000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 
(0x7f1ec91ea000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 
(0x7f1ec91c7000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 
(0x7f1ec9078000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 
(0x7f1ec905e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 
(0x7f1ec8e6d000)

/lib64/ld-linux-x86-64.so.2 (0x7f1ec92c4000)
arun@home-pc:/tmp$ ldc2 a.d --link-defaultlib-shared
arun@home-pc:/tmp$ ldd a
linux-vdso.so.1 (0x7ffcbda7f000)
libphobos2-ldc-shared.so.88 => 
/home/arun/.bin/ldc2-1.18.0-linux-x86_64/bin/../lib/libphobos2-ldc-shared.so.88 (0x7f1b57be8000)
libdruntime-ldc-shared.so.88 => 
/home/arun/.bin/ldc2-1.18.0-linux-x86_64/bin/../lib/libdruntime-ldc-shared.so.88 (0x7f1b57abc000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 
(0x7f1b57a84000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 
(0x7f1b57893000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 
(0x7f1b57744000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 
(0x7f1b57721000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 
(0x7f1b57714000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 
(0x7f1b5770e000)

/lib64/ld-linux-x86-64.so.2 (0x7f1b58067000)
arun@home-pc:/tmp$


Re: Setting process name

2019-05-02 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Wed, May 1, 2019 at 7:50 PM Anonymouse via Digitalmars-d-learn
 wrote:
>
> Is there a way of setting the process/thread name that's neater
> than this?
>
> import core.sys.posix.pthread;
>
> extern(C) int pthread_setname_np(pthread_t, const char*);
>
> void main()
> {
>  import std.string : toStringz;
>  pthread_setname_np(pthread_self(), toStringz("thread_name"));
>  // ...
> }
>
> It seems to work, but it also seems peculiar that
> pthread_setname_np isn't in core.sys.posix.pthread with the rest.
> Why is this? Is there an easier way? Thread.getThis().name
> doesn't seem to be it.
>
>
> https://forum.dlang.org/thread/gudwwmmdcqrfhbdux...@forum.dlang.org

prctl can set and get the process name and thread name on Linux. You
can do something like this:

import core.sys.linux.sys.prctl;
byte[16] name = cast(byte[]) "Blabla";
name[name.length-1] = 0;
prctl(PR_SET_NAME, cast(size_t) name.ptr, cast(size_t) null,
cast(size_t) null, cast(size_t) null);

// to confirm it works fine
byte[16] newname;
prctl(PR_GET_NAME, cast(size_t) newname.ptr, cast(size_t) null,
cast(size_t) null, cast(size_t) null);
int i;
foreach (b; newname)
{
assert(b == name[i]);
i++;
}
writeln(cast(string) newname);


Re: OT - Git training Lon/HK and book recommendation on taste in programming

2019-05-01 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Wed, May 1, 2019 at 9:18 AM Arun Chandrasekaran  wrote:
>
> On Wed, May 1, 2019 at 8:15 AM Guillaume Piolat via
> Digitalmars-d-learn  wrote:
> >
> > On Wednesday, 1 May 2019 at 09:51:01 UTC, Laeeth Isharc wrote:
> > >
> > > Second question.  Lots of people these days start to program to
> > > solve their problems at work but they may never have been shown
> > > the basic principles of design, structuring and maintenance of
> > > their code.  If I could give them one book (and a few YouTube
> > > links) what should it be ?
> >
> > Pragmatic Programmer
>
> +1

In case interested, here is the summary of Pragmatic Programmer:
https://github.com/HugoMatilla/The-Pragmatic-Programmer


Re: OT - Git training Lon/HK and book recommendation on taste in programming

2019-05-01 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Wed, May 1, 2019 at 8:15 AM Guillaume Piolat via
Digitalmars-d-learn  wrote:
>
> On Wednesday, 1 May 2019 at 09:51:01 UTC, Laeeth Isharc wrote:
> >
> > Second question.  Lots of people these days start to program to
> > solve their problems at work but they may never have been shown
> > the basic principles of design, structuring and maintenance of
> > their code.  If I could give them one book (and a few YouTube
> > links) what should it be ?
>
> Pragmatic Programmer

+1


Re: Logging best practices

2019-04-29 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Sat, Apr 27, 2019 at 2:55 AM Andre Pany via Digitalmars-d-learn
 wrote:
>
> On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm
> wrote:
> > Hello.
> >
> > Is there a current "Best Practices" for logging in D?
> >
> > For the actual logging, I know of `std.experimental.logger`.
> > However, the `experimental` has kept me away from it.
> >
> > Is it good, or are there any better alternatives?
>
> I also use it for all of my applications. But I really miss a
> RotatedTimeFileLogger. One big log file which will grow and grow
> is not that usuable in cloud environment.
>

std.experimenetal.logger is very limited with the feature set it
offers. You can't set custom log patterns, etc.

If you use AWS (docker), you could log to stdout and let the
cloudwatch do all the magic. Based on a config parameter, you can
enable/disable logging to a file.

module my.logger;

import std.experimental.logger;

__gshared MultiLogger log;
private __gshared FileLogger fileLog;
private __gshared FileLogger onScreen;

shared static this()
{
import std.file;
import std.stdio : stdout, File;

auto file = File("LMS.log", "a");
fileLog = new FileLogger(file);
onScreen = new FileLogger(stdout);

log = new MultiLogger();
if (cfg.logToFile)
log.insertLogger("file", fileLog);
log.insertLogger("stdout", onScreen);
}

> Also I haven't found out wheter it is safe / how to write from
> different threads to the same log file.

std.experimental.logger is perfectly thread safe. However printing the
logging thread ID is still pending with this PR
https://github.com/dlang/phobos/pull/6978


Re: alias fails to compile

2019-04-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 22 April 2019 at 19:57:11 UTC, aliak wrote:
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran 
wrote:

What am I doing wrong here?

struct A
{
union B
{
int bb;
}
B b;
alias aa = B.bb;
}

void main()
{
A a = A();
// a.b.bb = 4; // works
a.aa = 4; // fails
}


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


You can get the behaviour you want with opDispatch, and 
generalize it with a mixin template:


mixin template AliasMember(string aliasName, string memberName) 
{

ref opDispatch(string name)() if (name == aliasName) {
return mixin(memberName);
}
}

struct A {
union B {
int bb;
}
B b;
mixin AliasMember!("aa", "b.bb");
}

void main() {
A a = A();
a.aa = 4;
}


Works perfect for a single member! Not otherwise.


Re: How to do alias to a C struct member?

2019-04-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 22 April 2019 at 11:04:49 UTC, dangbinghoo wrote:

hi all,

as now we don't have the net/if.h binding, I need to do struct 
ifreq binding myself, here's my code:


[...]


Looks like we both were working on the same thing, few hours 
apart. 
https://forum.dlang.org/post/nkbnknlgmcwaivppm...@forum.dlang.org


Re: alias fails to compile

2019-04-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 22 April 2019 at 14:07:11 UTC, Alex wrote:
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran 
wrote:

What am I doing wrong here?

struct A
{
union B
{
int bb;
}
B b;
alias aa = b.bb;
}

void main()
{
A a = A();
// a.b.bb = 4; // works
a.aa = 4; // fails
}


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


aa is a "static" semantic.

your statement a.aa does not get translated in to a.b.bb like 
you think it does.


You are doing nothing any different than A.aa = 4.

which, when you realize this you'll understand the "need this" 
error.



It's as if you are doing


struct A
{
union B
{
int bb;
}
B b;
}

 alias aa = A.b.bb;

aa = 4;

which is like trying to say

A.b.bb = 4;


In D we can access "static" stuff using the object and so this 
can make things look like the mean something they are not.


If you could magically pass the this to it using something like 
UFCS then it could work.


import std.stdio;
struct A
{
union B
{
int bb;
}
B b;
alias aa = (ref typeof(this) a) { return  };
}

void main()
{
A a = A();
a.b.bb = 4;
a.aa(a).bb = 5;
writeln(a.aa(a).bb);
}

But here a.aa is not aa(a) and UFCS does not work so one must 
first access the alias and then pass the this.


Basically you are not going to get it to work. Just not how the 
semantics works.


It would be nice if a.aa(a) could reduce to a.aa as is UFCS was 
smart enough to realize it could pass it to the alias as this, 
but it doesn't.


else one might could do

alias aa = this.b.bb;

and

a.aa = 4;


I see. This is a simplified use case in /usr/include/net/if.h on 
Linux, where struct ifreq has something like this:


struct ifreq
  {
# define IFHWADDRLEN6
# define IFNAMSIZ   IF_NAMESIZE
union
  {
char ifrn_name[IFNAMSIZ];   /* Interface name, e.g. "en0".  */
  } ifr_ifrn;

union
  {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short int ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ];  /* Just fits the size */
char ifru_newname[IFNAMSIZ];
__caddr_t ifru_data;
  } ifr_ifru;
  };
# define ifr_name   ifr_ifrn.ifrn_name  /* interface name   */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr/* MAC address  */
# define ifr_addr   ifr_ifru.ifru_addr  /* address  */
# define ifr_dstaddrifr_ifru.ifru_dstaddr   /* other end of 
p-p lnk */
# define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast 
address*/
# define ifr_netmaskifr_ifru.ifru_netmask   /* interface net 
mask   */

# define ifr_flags  ifr_ifru.ifru_flags /* flags*/
# define ifr_metric ifr_ifru.ifru_ivalue/* metric   */
# define ifr_mtuifr_ifru.ifru_mtu   /* mtu  */
# define ifr_mapifr_ifru.ifru_map   /* device map   */
# define ifr_slave  ifr_ifru.ifru_slave /* slave device */
# define ifr_data   ifr_ifru.ifru_data  /* for use by interface */
# define ifr_ifindexifr_ifru.ifru_ivalue/* interface 
index  */
# define ifr_bandwidth  ifr_ifru.ifru_ivalue/* link bandwidth 
  */

# define ifr_qlen   ifr_ifru.ifru_ivalue/* queue length */
# define ifr_newnameifr_ifru.ifru_newname   /* New name */
# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short 
_IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int 
_IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)



Macro magic has been doing the job and luckily there is no 
ifr_name, etc defined in the user code. Looks like there is no 
way but to name the elements of this struct as ifr_name, etc?


alias fails to compile

2019-04-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

What am I doing wrong here?

struct A
{
union B
{
int bb;
}
B b;
alias aa = B.bb;
}

void main()
{
A a = A();
// a.b.bb = 4; // works
a.aa = 4; // fails
}


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


Re: Is it possible to modify shared struct array in a function.

2019-02-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 8 February 2019 at 04:51:08 UTC, Sudhi wrote:
On Friday, 8 February 2019 at 04:30:23 UTC, Arun Chandrasekaran 
wrote:

[...]



My example code was wrong. Below is the right one.

struct Company
{
string name;
string location;
}

struct Racks
{
int number;
int location;
}

struct Metadata
{
string name;
Company[] companies;
Racks[] racks;
}

struct Item
{
Metadata[] met;
int count;
}

shared (Item) item;

void main()
{
   updateMetadata();
}

void updateMetadata()
{
   Company company;
   company.name = "Hello";
   company.location = "Bangalore";
   item.met.companies ~= company;
   import std.stdio: writeln;
   writeln(item);
}

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


`shared struct Metadata` should make it work.

Turtles (shared) all the way down.


Re: Is it possible to modify shared struct array in a function.

2019-02-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 8 February 2019 at 04:13:39 UTC, Sudhi wrote:
I have a situation, where i want to modify a shared variable in 
a function. Something like below


struct Company
{
string name;
string location;
}

struct Racks
{
int number;
int location;
}

struct Metadata
{
string name;
Company[] companies;
Racks[] racks;
}

struct Item
{
Metadata met;
int count;
}

shared (Item) item

void main()
{
   updateMetadata()
}

void updateMetadata()
{
   Company company;
   company.name = "Hello";
   company.location = "Bangalore";
   item.met.companies ~= company;
}

Compiler throws me error in last line for appending as below.

"cannot append type Metadata to type shared(Metadata[])".

Please let me know if there is a way to acheive this.

Thanks,
Sudhi


Works fine for me with DMD64 D Compiler v2.083.1. 
https://run.dlang.io/is/RRM8GU




How to split strings into AA using phobos

2018-12-11 Thread Arun Chandrasekaran via Digitalmars-d-learn
A typical example would be to split the HTTP query string into an 
AA.


vibe.d has req.queryString, but no convenient wrapper to access 
it as an AA.


http://localhost/hello?name=abc=123

I've got this far.

auto arr = req.queryString.splitter('&').map!(a => 
a.splitter('='));


Thanks


Re: How to get a function name (string) @ compile time

2018-12-09 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Sunday, 9 December 2018 at 03:29:27 UTC, Andrew Pennebaker 
wrote:
On Monday, 3 November 2008 at 12:29:16 UTC, Simen Kjaeraas 
wrote:
On Mon, 03 Nov 2008 12:33:05 +0100, Denis Koroskin 
<2kor...@gmail.com> wrote:



[...]


That's not the only error here. Your template function also 
calls
foo with no arguments on  the line below that bug. Fixing that 
would
probably include the ParameterTypeTuple and ReturnType 
templates.


Er, when I try to use either foo.stringof, or 
__trait(identifier, foo), I always get that binding name, 
rather than the original function name, sad panda.


I can only print out the current variable name, but I want to 
print the name of the function declaration, no matter how 
deeply I pass that first function pointer into different calls 
:/


You may want to look at 
https://forum.dlang.org/post/yxobqahkvfcpcvidq...@forum.dlang.org


Re: std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 7 December 2018 at 20:28:37 UTC, Seb wrote:
On Friday, 7 December 2018 at 19:38:29 UTC, Arun Chandrasekaran 
wrote:

On Friday, 7 December 2018 at 19:12:31 UTC, Seb wrote:
On Friday, 7 December 2018 at 18:51:27 UTC, Arun 
Chandrasekaran wrote:

[...]


Alternatively to the answers above you can also use a custom 
lambda for canFind:


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


Just curious, how do we find multiple needles? This throws 
compilation error


assert(hay.canFind!(e => (e.canFind(["111", "222"];

An example in the doc would be helpful.



It's variadic: https://run.dlang.io/is/AKkKA9

Please feel free to add an example to the docs.


Sure, here it is https://github.com/dlang/phobos/pull/6796


Re: std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 7 December 2018 at 19:12:31 UTC, Seb wrote:
On Friday, 7 December 2018 at 18:51:27 UTC, Arun Chandrasekaran 
wrote:
I'm trying to find the needle in the hay that's an array of 
strings. So the second assert fails for some reason. Is this 
expected? https://run.dlang.io/is/7OrZTA


```
#!/usr/bin/rdmd

void main()
{
import std.experimental.all;
string s1 = "aaa111aaa";
string s2 = "aaa222aaa";
string s3 = "aaa333aaa";
string s4 = "aaa444aaa";
const hay = [s1, s2, s3, s4];
assert(canFind(s1, "111"));
assert(canFind(hay, "111"));
}
```

Why is there a difference in the behavior?


Alternatively to the answers above you can also use a custom 
lambda for canFind:


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


Just curious, how do we find multiple needles? This throws 
compilation error


assert(hay.canFind!(e => (e.canFind(["111", "222"];

An example in the doc would be helpful.



Re: std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 7 December 2018 at 19:12:31 UTC, Seb wrote:
On Friday, 7 December 2018 at 18:51:27 UTC, Arun Chandrasekaran 
wrote:
I'm trying to find the needle in the hay that's an array of 
strings. So the second assert fails for some reason. Is this 
expected? https://run.dlang.io/is/7OrZTA


```
#!/usr/bin/rdmd

void main()
{
import std.experimental.all;
string s1 = "aaa111aaa";
string s2 = "aaa222aaa";
string s3 = "aaa333aaa";
string s4 = "aaa444aaa";
const hay = [s1, s2, s3, s4];
assert(canFind(s1, "111"));
assert(canFind(hay, "111"));
}
```

Why is there a difference in the behavior?


Alternatively to the answers above you can also use a custom 
lambda for canFind:


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


That's elegant!


Re: std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Friday, 7 December 2018 at 19:08:05 UTC, Arun Chandrasekaran 
wrote:

On Friday, 7 December 2018 at 18:57:48 UTC, Dennis wrote:
On Friday, 7 December 2018 at 18:51:27 UTC, Arun 
Chandrasekaran wrote:

Why is there a difference in the behavior?


Your first assert expression is looking for a string in a 
larger string, your second expression looks for hay which is 
not a string but a string[]. To flatten the array, use:


assert(canFind(hay.join, "111"));


This succeeds.

assert(canFind(hay, "aaa222aaa"));

So the difference in the behaviour is caused by canFind 
checking for equality when string[] is passed. Is this the 
expected behaviour? I wouldn't want to join the array, for the 
array could be really big.


Actually, canFind documentation is perfect. 
https://dlang.org/phobos/std_algorithm_searching.html#.canFind


assert( canFind!((string a, string b) => a.startsWith(b))(words, 
"bees"));


Thanks for the help anyways!


Re: std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 7 December 2018 at 18:57:48 UTC, Dennis wrote:
On Friday, 7 December 2018 at 18:51:27 UTC, Arun Chandrasekaran 
wrote:

Why is there a difference in the behavior?


Your first assert expression is looking for a string in a 
larger string, your second expression looks for hay which is 
not a string but a string[]. To flatten the array, use:


assert(canFind(hay.join, "111"));


This succeeds.

assert(canFind(hay, "aaa222aaa"));

So the difference in the behaviour is caused by canFind checking 
for equality when string[] is passed. Is this the expected 
behaviour? I wouldn't want to join the array, for the array could 
be really big.


std.algorithm.canFind behavior difference between arrays and elements

2018-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn
I'm trying to find the needle in the hay that's an array of 
strings. So the second assert fails for some reason. Is this 
expected? https://run.dlang.io/is/7OrZTA


```
#!/usr/bin/rdmd

void main()
{
import std.experimental.all;
string s1 = "aaa111aaa";
string s2 = "aaa222aaa";
string s3 = "aaa333aaa";
string s4 = "aaa444aaa";
const hay = [s1, s2, s3, s4];
assert(canFind(s1, "111"));
assert(canFind(hay, "111"));
}
```

Why is there a difference in the behavior?


Trying to get current function name results in compiler error with __traits

2018-12-06 Thread Arun Chandrasekaran via Digitalmars-d-learn
I'm trying to get the current function name and apparently the 
commented line errors out.


What am I doing wrong?

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

```
#!/usr/bin/rdmd

void main()
{
import std.experimental.all;
void foo() {
// __traits(identifier, mixin(__FUNCTION__)).writeln; // 
compilation error

__FUNCTION__.split('.')[$-1].writeln;
}
__traits(identifier, mixin(__FUNCTION__)).writeln;
__FUNCTION__.split('.')[$-1].writeln;
foo();
}
```



Re: file io

2018-09-06 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Thursday, 6 September 2018 at 16:13:42 UTC, hridyansh thakur 
wrote:

how to read a file line by line in D


std.stdio.File.byLine()

Refer the doc here: 
https://dlang.org/library/std/stdio/file.by_line.html


An example from the doc:

```
import std.algorithm, std.stdio, std.string;
// Count words in a file using ranges.
void main()
{
auto file = File("file.txt"); // Open for reading
const wordCount = file.byLine()// Read lines
  .map!split   // Split into words
  .map!(a => a.length) // Count words per 
line

  .sum();  // Total word count
writeln(wordCount);
}
```


Re: Example of using C API from D?

2018-09-02 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Sunday, 2 September 2018 at 12:52:11 UTC, Russel Winder wrote:
I am rewriting a C++ program in D, but need to access a C 
library that has no D binding: this is a GtkD based program 
which has a Pango binding, but Pango doesn't offer the 
information I need, that is hidden in the underlying Fontconfig 
C API.


I could create a complete D binding for Fontconfig using the 
GIR files but that seems a bit over the top.


Can anyone point me at an example of a D program using a C API 
that has structs, enums and functions so I can see if I just 
hack enough for my use or go on to the full binding activity.


You can look at zmqd[1] as an example. I've been using it in 
production. I've also used dstep[2] to translate C headers to D.


[1] https://github.com/kyllingstad/zmqd
[2] https://github.com/jacob-carlborg/dstep


Re: Why The D Style constants are written in camelCase?

2018-05-09 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Wednesday, 9 May 2018 at 15:20:12 UTC, Jonathan M Davis wrote:
On Wednesday, May 09, 2018 14:12:41 Dmitry Olshansky via 
Digitalmars-d-learn wrote:

[...]


To an extent that's true, but anyone providing a library for 
use by others in the D community should seriously consider 
following it with regards to public symbols so that they're 
consistent with how stuff is named across the ecosystem. It's 
not the end of the world to use a library that did something 
like use PascalCase instead of camelCase for its function 
names, or which used lowercase and underscores for its type 
names, or did any number of other things which are perfectly 
legitimate but don't follow the D style. However, they tend to 
throw people off when they don't follow the naming style of the 
rest of the ecosystem and generally cause friction when using 
3rd party libraries.


[...]


If this issue https://github.com/dlang-community/dfmt/issues/227 
is fixed we could potentially summarize that in .editorconfig 
file so that anyone who wishes can easily adopt it.


Re: How to curl!

2018-05-01 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Tuesday, 1 May 2018 at 21:57:22 UTC, IntegratedDimensions 
wrote:

Trying to curl basic stuff but std.net.curl isn't cooperating:
This is one of the reasons I hate D ;/ I've spent more time 
trying to get std.net.curl to do something that just works from 
the command line.


std.net.curl is not quite up to date when compared to the curl 
command. It is not useful any non-trivial stuff. Right now I'm 
using ikod's dlang-requests[1] and I'm quite happy with it. I 
would recommend you to try it.


[1] https://github.com/ikod/dlang-requests


Warning on self assignment

2018-04-24 Thread Arun Chandrasekaran via Digitalmars-d-learn
So I was telling my colleague that D would warn on self 
assignment, but found that I was wrong.


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

```
module a;

import std.stdio;

void main() {
string a;
a = a;  // Can the compiler warn at this line that 
there is no effect?

writeln(a);
return;
}
```


Re: Benchmarking sigmoid function between C and D

2018-04-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Saturday, 7 April 2018 at 23:48:36 UTC, kinke wrote:
On Saturday, 7 April 2018 at 20:33:13 UTC, Arun Chandrasekaran 
wrote:

[...]


As this appears to be benchmarking mostly the 
std.math.exp(float) performance - some/many basic algos in 
std.math, incl. exp(), are currently using the x87 FPU for all 
3 floating-point types, although there are treacherous 
float/double overloads, sacrificing performance in many cases.
See 
https://github.com/dlang/phobos/pull/6272#issuecomment-373967109 (and the later numbers for current Intel and GCC compilers) for a performance comparison of some std.math algos incl. exp(). For LDC, the double/float versions of the few worked-on algos in that PR were sped-up by an overall factor of 3 [but only by 1.46 for exp(float)].


Interesting to see this, thanks! Did you also generate the bar 
graph plot using D?


Re: Benchmarking sigmoid function between C and D

2018-04-07 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Saturday, 7 April 2018 at 19:14:27 UTC, Daniel Kozak wrote:
or for ldc 
http://docs.algorithm.dlang.io/latest/mir_math_common.html


On Sat, Apr 7, 2018 at 9:10 PM, Daniel Kozak 
<kozz...@gmail.com> wrote:



can you try it with c math functions?

instead of std.math, try to use core.stdc.math

On Sat, Apr 7, 2018 at 8:53 PM, Arun Chandrasekaran via 
Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:



[...]


Much better with mir.math.common, still a bit slower than C (even 
with larger loops):


10^7 iterations using sigmoid1: 168 ms
10^7 iterations using sigmoid2: 39 ms

Also LDC optimized away the computation. So I had to modify the 
code a bit.


```
private auto sigmoid1_perf() {
auto sw = StopWatch(AutoStart.yes);
int i;
float x, y = 0.0f;

for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y += sigmoid1(x);
}
}
auto t = sw.peek.total!"msecs";
return tuple(y, t);
}

private auto sigmoid2_perf() {
auto sw = StopWatch(AutoStart.yes);
int i;
float x, y = 0.0f;
for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y += sigmoid2(x);
}
}
auto t = sw.peek.total!"msecs";
return tuple(y, t);
}
```


Benchmarking sigmoid function between C and D

2018-04-07 Thread Arun Chandrasekaran via Digitalmars-d-learn
What am I doing wrong here that makes the D equivalent 2.5 times 
slower than it's C equivalent?


Compilers used:

LDC2: LDC - the LLVM D compiler (1.8.0)
GCC: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

11:36:39 ~/code/c/test2$ ldc2 sigmoid.d -O5 && ./sigmoid
Max deviation is 0.001664
10^7 iterations using sigmoid1: 308 ms
10^7 iterations using sigmoid2: 30 ms
11:36:55 ~/code/c/test2
$ gcc sigmoid.c -o sigmoid-c -O3 -lm 2>/dev/null && ./sigmoid-c
Max deviation is 0.001664
10^7 iterations using sigmoid1: 134 ms
10^7 iterations using sigmoid2: 29 ms
11:37:10 ~/code/c/test2
$

C code, taken from 
https://stackoverflow.com/questions/412019/math-optimization-in-c-sharp#412176:


```
#include 
#include 
#include 

#define SCALE 320.0f
#define RESOLUTION 2047
#define MIN -RESOLUTION / SCALE
#define MAX RESOLUTION / SCALE

static float sigmoid_lut[RESOLUTION + 1];

void init_sigmoid_lut(void) {
int i;
for (i = 0; i < RESOLUTION + 1; i++) {
sigmoid_lut[i] =  (1.0 / (1.0 + exp(-i / SCALE)));
}
}

static float sigmoid1(const float value) {
return (1.0f / (1.0f + expf(-value)));
}

static float sigmoid2(const float value) {
if (value <= MIN) return 0.0f;
if (value >= MAX) return 1.0f;
if (value >= 0) return sigmoid_lut[(int)(value * SCALE + 
0.5f)];

return 1.0f-sigmoid_lut[(int)(-value * SCALE + 0.5f)];
}

float test_error() {
float x;
float emax = 0.0;

for (x = -10.0f; x < 10.0f; x+=0.1f) {
float v0 = sigmoid1(x);
float v1 = sigmoid2(x);
float error = fabsf(v1 - v0);
if (error > emax) { emax = error; }
}
return emax;
}

int sigmoid1_perf() {
clock_t t0, t1;
int i;
float x, y = 0.0f;

t0 = clock();
for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y = sigmoid1(x);
}
}
t1 = clock();
printf("", y); /* To avoid sigmoidX() calls being optimized 
away */

return (t1 - t0) / (CLOCKS_PER_SEC / 1000);
}

int sigmoid2_perf() {
clock_t t0, t1;
int i;
float x, y = 0.0f;
t0 = clock();
for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y = sigmoid2(x);
}
}
t1 = clock();
printf("", y); /* To avoid sigmoidX() calls being optimized 
away */

return (t1 - t0) / (CLOCKS_PER_SEC / 1000);
}

int main(void) {
init_sigmoid_lut();
printf("Max deviation is %0.6f\n", test_error());
printf("10^7 iterations using sigmoid1: %d ms\n", 
sigmoid1_perf());
printf("10^7 iterations using sigmoid2: %d ms\n", 
sigmoid2_perf());


return 0;
}
```

D equivalent:

```
module sigmoid;

import std.stdio;
import std.math;
import std.datetime.stopwatch;

enum SCALE = 320.0f;
enum RESOLUTION = 2047;
enum MIN = -RESOLUTION / SCALE;
enum MAX = RESOLUTION / SCALE;

float[RESOLUTION + 1] sigmoid_lut;

void init_sigmoid_lut() {
int i;
for (i = 0; i < RESOLUTION + 1; i++) {
sigmoid_lut[i] =  (1.0 / (1.0 + exp(-i / SCALE)));
}
}

private float sigmoid1(const float value) {
return (1.0f / (1.0f + exp(-value)));
}

private float sigmoid2(const float value) {
if (value <= MIN) return 0.0f;
if (value >= MAX) return 1.0f;
if (value >= 0) return sigmoid_lut[cast(int)(value * SCALE + 
0.5f)];

return 1.0f-sigmoid_lut[cast(int)(-value * SCALE + 0.5f)];
}

private float test_error() {
float x;
float emax = 0.0;

for (x = -10.0f; x < 10.0f; x+=0.1f) {
float v0 = sigmoid1(x);
float v1 = sigmoid2(x);
float error = fabs(v1 - v0);
if (error > emax) { emax = error; }
}
return emax;
}

private auto sigmoid1_perf() {
auto sw = StopWatch(AutoStart.yes);
int i;
float x, y = 0.0f;

for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y = sigmoid1(x);
}
}
return sw.peek.total!"msecs";
}

private auto sigmoid2_perf() {
auto sw = StopWatch(AutoStart.yes);
int i;
float x, y = 0.0f;
for (i = 0; i < 10; i++) {
for (x = -5.0f; x <= 5.0f; x+=0.1f) {
y = sigmoid2(x);
}
}
return sw.peek.total!"msecs";
}

int main() {
init_sigmoid_lut();
writefln("Max deviation is %0.6f", test_error());
writefln("10^7 iterations using sigmoid1: %s ms", 
sigmoid1_perf());
writefln("10^7 iterations using sigmoid2: %s ms", 
sigmoid2_perf());


return 0;
}
```


Re: how to make private class member private

2018-03-13 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Tuesday, 13 March 2018 at 13:59:00 UTC, Steven Schveighoffer 
wrote:

On 3/12/18 10:06 PM, psychoticRabbit wrote:

[...]


OK, so I agree there are drawbacks. But these can be worked 
around.


[...]


Private members still have external linkage. Is there anyway to 
solve this?


Re: Access visibility and linkage

2018-02-15 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Thursday, 15 February 2018 at 06:52:15 UTC, Seb wrote:
On Thursday, 15 February 2018 at 06:43:52 UTC, Arun 
Chandrasekaran wrote:
I was reading through 
https://wiki.dlang.org/Access_specifiers_and_visibility#What_is_missing



[...]


DMD v2.077.1 exhibits the same behavior. Is this is already 
being worked on? Or is there any plan to address this? Can 
this be fixed? If so how? If not, why not?


AFAICT it's a feature as the article correctly explains this 
doesn't work well with serialization:


Compile-time reflection, i.e. serialization libraries or 
@attribute scanners. Limiting access for __traits may forbid 
certain currently working idioms.

Use Voldemort types if you want to truly encapsulate something.


ah, not to that extent though.. just need all my variables and 
functions to be private at the module level.



Simply use `private:` if you think otherwise.


Thanks! Turtles all the way "up" this time :)



Access visibility and linkage

2018-02-14 Thread Arun Chandrasekaran via Digitalmars-d-learn
I was reading through 
https://wiki.dlang.org/Access_specifiers_and_visibility#What_is_missing


There is currently no way in D to mark symbols for internal 
linkage, saying "this an implementation detail, you should not 
even know this one exists". This is an important module-level 
encapsulation tool which also somewhat guarantees that those 
symbols can't be linked to by accident by some other module and 
you are free to change them keeping binary interface same.


DMD v2.077.1 exhibits the same behavior. Is this is already being 
worked on? Or is there any plan to address this? Can this be 
fixed? If so how? If not, why not?


Name clash between public and private symbols has also been 
stated as unneeded and useless feature that makes possible to 
break a compilation of a project by changing private name. It 
is also impossible to use an UFCS function now if class already 
has private one with same signature.


Also, why is the default visibility of global variables at module 
level public Shouldn't they be private by default to provide 
better encapsulation guarantee (and linkage guarantee if the 
above is addressed in future)?
From readability point of view as well, for instance, if I want 
to know all the functions "exposed" by the current module, I can 
simply grep for public.


Re: parallelism

2018-01-28 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Sunday, 28 January 2018 at 04:44:23 UTC, thedeemon wrote:
On Saturday, 27 January 2018 at 20:49:43 UTC, Arun 
Chandrasekaran wrote:


But really I'm not sure why you want static foreach here


I was just trying to see if static foreach can be used here, but 
well, you showed that it's not required.


I just got intrigued to see the error on labeled break. Thanks 
anyway.


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Saturday, 27 January 2018 at 17:54:53 UTC, thedeemon wrote:
On Saturday, 27 January 2018 at 11:19:37 UTC, Arun 
Chandrasekaran wrote:

Simplified test case that still errors:


You got really close here. Here's a working version:

enum Operation {
a,
b
}

import std.traits, std.conv, std.stdio;

void main(string[] args) {
auto op = Operation.a;
foreach (_; 0 .. args.length) {
final switch (op) {
foreach(e; EnumMembers!Operation) {
case e:
mixin(e.to!string ~ "();");
break;
}
}
}
}

void a() { writeln("A!"); }
void b() { writeln("B!"); }


Thanks, that did the trick.

How to use break inside a static foreach? Changing the above 
foreach each to static gives the following error:


Error: must use labeled break within static foreach




Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Saturday, 27 January 2018 at 10:49:45 UTC, Arun Chandrasekaran 
wrote:
On Saturday, 27 January 2018 at 10:38:25 UTC, Nicholas Wilson 
wrote:

...
[snip]


Simplified test case that still errors:

```
enum Operation {
a,
b
}

import std.traits;
import std.conv;

void main(string[] args) {

foreach (_; 0 .. args.length) {
Operation operation;
switch (operation) {
static foreach(e; EnumMembers!Operation) {
case e:
mixin(to!string(e))();
break;
}
}
}
}

void a() {}
void b() {}
```


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Saturday, 27 January 2018 at 10:38:25 UTC, Nicholas Wilson 
wrote:
On Saturday, 27 January 2018 at 10:28:10 UTC, Arun 
Chandrasekaran wrote:

```
import std.parallelism;
auto pool = new TaskPool(options.threadCount);
foreach (_; 0 .. options.iterationCount) {
switch (options.operation) {
static foreach(e; EnumMembers!Operation) {
case e:
 pool.put(task!e(options));
 break;
}
pool.finish();
```


Does that do the trick?


No it doesn't.

```
/usr/include/dmd/phobos/std/parallelism.d(507,34): Error: 
function expected before (), not cast(Operation)0 of type 
Operation
/usr/include/dmd/phobos/std/parallelism.d(835,16): Error: 
template instance std.parallelism.Task!(cast(Operation)0, 
Options) error instantiating
src/app.d(159,32):instantiated from here: 
task!(cast(Operation)0, Options)
src/app.d(160,17): Error: must use labeled break within static 
foreach


### and so on till the end of enum
```


Re: parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Saturday, 27 January 2018 at 10:28:10 UTC, Arun Chandrasekaran 
wrote:

Hi All,

Is there a way to rewrite this

[...]


Damn! The subject should've been something else.. naming is 
surely hard..


parallelism

2018-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn

Hi All,

Is there a way to rewrite this

```
import std.parallelism;
auto pool = new TaskPool(options.threadCount);
foreach (_; 0 .. options.iterationCount) {
switch (options.operation) {
case Operation.a:
pool.put(task!a(options));
break;

case Operation.b:
pool.put(task!b(options));
break;

case Operation.c:
pool.put(task!c(options));
break;

case Operation.d:
pool.put(task!d(options));
break;

/// and so on.
}
pool.finish();
```

into this?

```
import std.parallelism;
import std.conv: to;
auto pool = new TaskPool(options.threadCount);
foreach (_; 0 .. options.iterationCount) {
pool.put(task!(to!string(options.operation))(options)); 
// this line errs.

}
pool.finish();
```

--
Arun


dub infinite loop

2018-01-22 Thread Arun Chandrasekaran via Digitalmars-d-learn
So dub gets into indefinite loop (let's not argue about 
infinite/indefinite).


I've reported the issue a month back here 
http://forum.rejectedsoftware.com/groups/rejectedsoftware.dub/thread/16888/ and recently here https://github.com/dlang/dub/issues/1345


Does anyone face the same issue? How do you work around this for 
a non trivial setup?


Re: Get pointer or reference of an element in Array(struct)

2017-12-11 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Saturday, 9 December 2017 at 19:26:26 UTC, David Nadlinger 
wrote:

but "free" references don't exist in the language.


To the point! Thanks!


Re: Get pointer or reference of an element in Array(struct)

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote:
On Saturday, 9 December 2017 at 06:15:16 UTC, Arun 
Chandrasekaran wrote:
Is there a way to get the pointer or reference of an element 
in Array(T)?

[...]

auto d2 = gallery[0];


auto d2 = [0];


Thanks. Just curious why reference can't be obtained here. Saves 
nasty null checks in most places.


Re: Parallel reads on std.container.array.Array

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Saturday, 9 December 2017 at 01:34:40 UTC, Arun Chandrasekaran 
wrote:
So I tried the same on Haswell processor with LDC 1.6.0 and it 
crashes


```
=== Starting D version ===
Took 1 sec, 107 ms, and 383 μs to load 100 items. Gonna 
search in parallel...
*** Error in `./dmain-ldc': double free or corruption 
(fasttop): 0x00edc6e0 ***
*** Error in `./dmain-ldc': double free or corruption 
(fasttop): 0x00edc6e0 ***

```


Learnt (from David Nadlinger) that due to lifetime management of 
transitory ranges, they can't be used for parallel reads. 
Iterating by index has solved the problem.


However, accessing the items in Array results in value copy. Is 
that expected? How can I fix this?


http://forum.dlang.org/post/cfhkszdbkaezprbzr...@forum.dlang.org


Get pointer or reference of an element in Array(struct)

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn
Is there a way to get the pointer or reference of an element in 
Array(T)?


https://run.dlang.io/gist/70fd499afe8438d4877f57aec90c3091?compiler=dmd

The assertion seems to fail below. Value copy is not is intended 
here.


module test;

void main()
{
struct Data
{
int id;
}

import std.container.array;
Array!Data gallery;

Data d1;
gallery.insertBack(d1);

auto d2 = gallery[0];
d2.id = 1;
assert(d2.id == gallery[0].id, "neither ref nor pointer");
}



Re: Parallel reads on std.container.array.Array

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn
So I tried the same on Haswell processor with LDC 1.6.0 and it 
crashes


```
=== Starting D version ===
Took 1 sec, 107 ms, and 383 μs to load 100 items. Gonna 
search in parallel...
*** Error in `./dmain-ldc': double free or corruption (fasttop): 
0x00edc6e0 ***
*** Error in `./dmain-ldc': double free or corruption (fasttop): 
0x00edc6e0 ***

```

DMD on the other hand takes forever to run and doesn't complete.


Re: Parallel reads on std.container.array.Array

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 8 December 2017 at 10:01:14 UTC, Kagamin wrote:
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran 
wrote:
I was wondering if std.container.array.Array supports 
threadsafe parallel reads similar to std::vector.


No, your code can also fail on a system with inconsistent cache 
because data written by writing thread can remain in its cache 
and not reach shared memory in time or reading threads can read 
from their stale cache.


I'm OK with some delay between the writes and the reads. The same 
applies to the writes and reads across processes. At least 
between threads the impact/delay is minimum whereas between 
processes it's even worse as the page will have to be reflected 
in all the mapped processes.


Re: Parallel reads on std.container.array.Array

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran 
wrote:
I was wondering if std.container.array.Array supports 
threadsafe parallel reads similar to std::vector. I've created 
a small program for demonstration 
https://github.com/carun/parallel-read-tester


It works fine with just couple of problems though:

1. D version takes way too long compared to C++ version.

My mistake (IO bottleneck, std.stdio.write is probably 
flushing?)! The timings are now close enough, in the order of 
milliseconds. This is not just with one run, but multiple runs. 
(I should probably test this on a Xeon server).


=== Starting CPP version ===
Took 3.79253 to load 200 items. Gonna search in parallel...
4 4
1 4
3 4
2 4
6 4
7 4
5 4
0 4
Took 6.28018 to search

=== Starting D version ===
Took 1 sec, 474 ms, 869 μs, and 4 hnsecs to load 200 items. 
Gonna search in parallel...

0 4
1 4
2 4
7 4
6 4
4 4
3 4
5 4
Took 6 secs, 472 ms, 467 μs, and 8 hnsecs to search.

The one that puzzles me is, what's wrong with the CPP version? :) 
Why is it slow loading the gallery (more than twice as slow as 
the D counterpart)? I thought std::vector::emplace_back should do 
a decent job. RVO in D?


2. Introducing a string in the struct Data results in 
"std.container.Array.reserve failed to allocate memory", 
whereas adding a similar std::string in the C++ struct seems to 
work fine.

Couldn't find the reason!


Am I missing anything obvious here?

Also why doesn't std.container.array support an equivalent of 
std::vector::erase?


Re: Parallel reads on std.container.array.Array

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran 
wrote:


2. I'm on an 8 CPU box and I don't seem to hit 800% CPU with D 
version (max 720%). However I can get 800% CPU usage with the 
C++ version.

Please ignore, this is because of the write.


Re: Create D portable binary

2017-12-08 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 8 December 2017 at 06:37:36 UTC, Adam D. Ruppe wrote:

On Friday, 8 December 2017 at 05:16:22 UTC, Fra Mecca wrote:
Is there a way to compile a project and deploying it as a 
single statically linked binary?


A default build of a D program is *reasonably* compatible. All 
its dependencies are core operating system components like 
libc. Now, there can certainly be libc version 
incompatibilities, but there's a decent chance it will just 
work.


I'm pretty sure this is the exact same situation Go is in; the 
default Go and D builds link the same way.



If you want to eliminate the potential C lib incompatibility 
too, you can do it basically the same way as in C, passing 
options to gcc with dmd's -L thing like `-L-static 
-L-nodefaultlib -L-lsome_alternate_clib`


On the same note, has https://wiki.dlang.org/DIP59 been dropped?


Parallel reads on std.container.array.Array

2017-12-07 Thread Arun Chandrasekaran via Digitalmars-d-learn
I was wondering if std.container.array.Array supports threadsafe 
parallel reads similar to std::vector. I've created a small 
program for demonstration 
https://github.com/carun/parallel-read-tester


It works fine with just couple of problems though:

1. D version takes way too long compared to C++ version.

```
bash build-and-run.sh
g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  
There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.


LDC - the LLVM D compiler (1.6.0):
  based on DMD v2.076.1 and LLVM 5.0.0
  built with LDC - the LLVM D compiler (1.6.0)
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake
  http://dlang.org - http://wiki.dlang.org/LDC

  Registered Targets:
aarch64- AArch64 (little endian)
aarch64_be - AArch64 (big endian)
arm- ARM
arm64  - ARM64 (little endian)
armeb  - ARM (big endian)
nvptx  - NVIDIA PTX 32-bit
nvptx64- NVIDIA PTX 64-bit
ppc32  - PowerPC 32
ppc64  - PowerPC 64
ppc64le- PowerPC 64 LE
thumb  - Thumb
thumbeb- Thumb (big endian)
x86- 32-bit X86: Pentium-Pro and above
x86-64 - 64-bit X86: EM64T and AMD64

=== Starting CPP version ===
Took 3.7583 to load 200 items. Gonna search in parallel...
5 400
6 400
2 400
0 400
1 400
7 400
4 400
3 400
Took 7.0247 to search

=== Starting D version ===
Took 1 sec, 506 ms, 672 μs, and 4 hnsecs to load 200 items. 
Gonna search in parallel...

3 400
4 400
2 400
6 400
7 400
5 400
1 400
0 400
Took 13 secs, 53 ms, 790 μs, and 3 hnsecs to search.
```
2. I'm on an 8 CPU box and I don't seem to hit 800% CPU with D 
version (max 720%). However I can get 800% CPU usage with the C++ 
version.


2. Introducing a string in the struct Data results in 
"std.container.Array.reserve failed to allocate memory", whereas 
adding a similar std::string in the C++ struct seems to work fine.


Am I missing anything obvious here?

Also why doesn't std.container.array support an equivalent of 
std::vector::erase?


Cheers,
Arun


Re: git workflow for D

2017-12-05 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Monday, 4 December 2017 at 01:26:45 UTC, Arun Chandrasekaran 
wrote:

On Sunday, 3 December 2017 at 23:39:49 UTC, Basile B. wrote:

[...]


If you still lose changes, you could try using Mercurial with 
hggit. It can be a bit slow, but not destructive as git itself. 
;)


I really wish Mercurial won instead of git. Now that hg evolve 
and hg topic are stable, that actually alleviates the need for 
git. But the world talks git now. So everyone else is forced to 
talk in git :(


I guess, without StackOverflow and GitHub, no one would be 
using git.


Facebook uses Mercurial and their team is working on a 
Mercurial server in Rust. 
https://github.com/facebookexperimental/mononoke


I thought Facebook uses DLang as well. No one's motivated to 
write one in DLang?


Looks like Mercurial is going to be rewritten in Rust 
https://www.mercurial-scm.org/wiki/OxidationPlan


So Facebook don't use D?


Re: git workflow for D

2017-12-03 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Sunday, 3 December 2017 at 23:39:49 UTC, Basile B. wrote:
On Sunday, 3 December 2017 at 22:22:47 UTC, Arun Chandrasekaran 
wrote:
Git CLI is arcane and esoteric. I've lost my commits before 
(yeah, my mistake).


Who hasn't ;)
Happened to me last time because i tried a command supposed to 
remove untracked files in submodules...but used "reset" in a 
wrong way... ouch.


If you still lose changes, you could try using Mercurial with 
hggit. It can be a bit slow, but not destructive as git itself. ;)


I really wish Mercurial won instead of git. Now that hg evolve 
and hg topic are stable, that actually alleviates the need for 
git. But the world talks git now. So everyone else is forced to 
talk in git :(


I guess, without StackOverflow and GitHub, no one would be using 
git.


Facebook uses Mercurial and their team is working on a Mercurial 
server in Rust. https://github.com/facebookexperimental/mononoke


I thought Facebook uses DLang as well. No one's motivated to 
write one in DLang?




Re: git workflow for D

2017-12-03 Thread Arun Chandrasekaran via Digitalmars-d-learn
Git CLI is arcane and esoteric. I've lost my commits before 
(yeah, my mistake). Since then I always access git via mercurial. 
In comparison Mercurial is far better a VCS tool.


Re: Andrei's "The D Programming Language" book. Up to date?

2017-11-29 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Wednesday, 29 November 2017 at 17:26:11 UTC, Nick Treleaven 
wrote:

Here's a list of significant things - maybe incomplete:
https://wiki.dlang.org/Differences_With_TDPL



Multiple alias this
You can only have one subtyping member currently.



Shared
Not all of shared's guarantees are implemented yet.



SafeD
@safe (and therefore SafeD) isn't fully implemented. So, it 
doesn't necessarily work quite like it's supposed to yet.


How much of these are relevant today with 2.077.0?


Re: CSV with empty values for integer fields

2017-10-30 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Sunday, 29 October 2017 at 15:45:23 UTC, Jesse Phillips wrote:
Not really you'll need to parse it out as a string and do the 
conversion later.


It probably would be good to support nullable!int pretty sure 
it doesn't currently.


Should I raise a ticket on Bugzilla to address this?


CSV with empty values for integer fields

2017-10-28 Thread Arun Chandrasekaran via Digitalmars-d-learn

CSV with empty values for integer fields throws

(Row: 1, Col: 3) Unexpected end of input when converting from 
type string to type int


Code that parses the CSV:

```
import std.algorithm;
import std.array;
import std.csv;
import std.stdio;
import std.conv;
import std.range;

private struct IdentifyResult
{
string probe;
string target;
int rank;
int score;
bool vendorMatch;
}

void main(string[] args)
{
if (args.length < 2)
{
writeln("Usage: ", args[0], "  ... 
");

return;
}

foreach (i; 1 .. args.length)
{
try
{
auto arr = File(args[i], 
"r").byLine.joiner("\n").csvReader!IdentifyResult.array;

writeln(args[i], "\tValid");
}
catch (std.csv.CSVException e)
{
writeln(e);
writeln(args[i], "\tInvalid");
}
}
}
```

Input CSV file below

CSV header (just for reference, but not part of the CSV)
Probe,Target,rank,score,match/nomatch,datetime,position,score
```
LIP_0905_1230.nistFALSE,2017-09-05 23:24:37,,
LIP_0905_1230.nistFALSE,2017-10-12 11:37:29,,
LIP_0905_1230.nistFALSE,2017-10-12 11:51:03,,
LIP_0905_1230.nistFALSE,2017-10-12 12:07:21,,
LIP_0905_1230.nist,CRM_1012_1920.nist,1,,true,2017-10-12 
19:56:00,25,
LIP_0905_1230.nist,CRM_1012_1920.nist,1,,true,2017-10-13 
00:55:00,25,
LIP_0905_1230.nist,CRM_1013_0005.nist,2,,true,2017-10-13 
00:55:00,25,
LIP_0905_1230.nist,CRM_1012_1920.nist,1,,true,2017-10-18 
18:27:22,25,
LIP_0905_1230.nist,CRM_1013_0005.nist,2,,true,2017-10-18 
18:27:22,25,
LIP_0905_1230.nist,CRM_1013_0005.nist,1,,true,2017-10-20 
11:04:31,25,

```


Is there anyway to overcome this without modifying the original 
CSV?



Cheers,
Arun


Re: writeln double precision

2017-10-24 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Tuesday, 24 October 2017 at 16:18:03 UTC, H. S. Teoh wrote:
On Tue, Oct 24, 2017 at 10:02:11AM +, Arun Chandrasekaran 
via Digitalmars-d-learn wrote:

On Monday, 23 October 2017 at 18:08:43 UTC, Ali Çehreli wrote:
> On 10/23/2017 07:22 AM, Arun Chandrasekaran wrote:
> > [...]
> The rule is that every expression has a type and 22/7 is int.

Thanks Ali. Is this for backward compatibility with C? 
Because, if there is a division, a natural/mathematical (not 
programmatic) expectation is to see a a double in the result.

[...]

I have never seen a programming language in which dividing two 
integers yields a float or double.  Either numbers default to a 
floating point type, in which case you begin with floats in the 
first place, or division is integer division, yielding an 
integer result.



T


I'm not denying that all the programming languages does it this 
way (even if it is a cause of related bugs).
I'm just questioning the reasoning behind why D does it this way 
and if it is for compatibility or if there is any other reasoning 
behind the decision.


Arun


Re: writeln double precision

2017-10-24 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 23 October 2017 at 18:08:43 UTC, Ali Çehreli wrote:

On 10/23/2017 07:22 AM, Arun Chandrasekaran wrote:
> [...]
The rule is that every expression has a type and 22/7 is int.


Thanks Ali. Is this for backward compatibility with C? Because, 
if there is a division, a natural/mathematical (not programmatic) 
expectation is to see a a double in the result.


Arun


Re: writeln double precision

2017-10-23 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Monday, 23 October 2017 at 14:07:06 UTC, Arun Chandrasekaran 
wrote:
I've written a simple tool [1] to find the DET and CMC 
specifically for biometrics performance measurement.


When I generate the report, I expected to see high precision 
floating point numbers, but I see that writefln trims the 
precision to the last 6 digits after decimal point.


Am I doing the right thing here? Should I use a different 
format specifier?


[1] https://bitbucket.org/carun/biometrics-reports/src

Cheers,
Arun


```
void main() {
double a = 22/7.0;
import std.stdio: writeln, writefln;
writefln("%.51f", a);
}
```

and it prints all the decimals. So I'm happy that I have not lost 
the precision. But why does the compiler bring the C baggage for 
the integer division? Why do I need to `cast (double)` ? Can't 
the compiler figure it out?


writeln double precision

2017-10-23 Thread Arun Chandrasekaran via Digitalmars-d-learn
I've written a simple tool [1] to find the DET and CMC 
specifically for biometrics performance measurement.


When I generate the report, I expected to see high precision 
floating point numbers, but I see that writefln trims the 
precision to the last 6 digits after decimal point.


Am I doing the right thing here? Should I use a different format 
specifier?


[1] https://bitbucket.org/carun/biometrics-reports/src

Cheers,
Arun


Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?

2017-10-16 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Monday, 16 October 2017 at 03:49:18 UTC, ketmar wrote:

Michael V. Franklin wrote:


[...]


judging from my several decades of expirience, bounties almost 
never works. there are alot of reasons for that, but the fact 
still stands: it is *almost* impossible to make something 
happen with boundy. it may work by accident ;-), but i myself 
wouldn't count on that. either bounty is too small ("hey, my 
time worth much more! i'd better spend it playing 
videogames!"), or it is too big ("hey, this is a Really Huge 
Problem, if somebody wants to pay than much! that means that 
i'll inevitably spend more time on that, and... the bounty is 
too small. oops." ;-).


[...]


True, that's how Eric Niebler gets to work on Range V1, V2, V3 as 
he was the only hired programmer by ISO CPP.


Re: What does ! mean?

2017-09-27 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Wednesday, 27 September 2017 at 14:34:06 UTC, Eugene Wissner 
wrote:
On Wednesday, 27 September 2017 at 14:23:01 UTC, Ky-Anh Huynh 
wrote:




See also the following chapter in Ali's book:
http://ddili.org/ders/d.en/templates.html


This chapter is what hooked me with D. Naming that chapter as 
"Templates for Human Beings" won't be an exaggeration.


Re: task parallelize dirEntries

2017-08-11 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Friday, 11 August 2017 at 21:58:20 UTC, Johnson wrote:
Just a thought, maybe the GC isn't cleaning up quick enough? 
You are allocating and md5 digest each iteration.


Possibly, an opitimization is use use a collection of md5 
hashes and reuse them. e.g., pre-allocate 100(you probably only 
need as many as the number of parallel loops going) and then 
attempt to resuse them. If all are in use, wait for a free one. 
Might require some synchronization.


John, thanks. That was it. md.d has nifty function that is 
straightforward than the OOP version.


```
void main(string[] args)
{
foreach (d; parallel(dirEntries(args[1], 
SpanMode.depth).filter!(f => f.isFile), 1))

{
auto data = cast(const(ubyte)[]) read(d.name);
auto hash = md5Of(data);
import std.array;
string[] t = split(d.name, '/');
writeln(toHexString(hash), "  ", t[$-1]);
}
}
```

Also I expected the performance to be faster than `md5sum`. 
However, that was not the case. Please see below. Is there anyway 
to optimize this further?


```
11-08-2017 17:22:54 vaalaham ~/code/d/d-mpmc-sample
$ time find /home/arun/downloads/boost_1_64_0/ -type f | xargs 
md5sum >/dev/null 2>&1


real0m1.124s
user0m0.952s
sys 0m0.208s
11-08-2017 17:23:16 vaalaham ~/code/d/d-mpmc-sample
$ ldc2 pmd.d -O3
11-08-2017 17:23:31 vaalaham ~/code/d/d-mpmc-sample
$ time ./pmd ~/downloads/boost_1_64_0 > /dev/null

real0m0.499s
user0m1.596s
sys 0m0.580s
11-08-2017 17:23:37 vaalaham ~/code/d/d-mpmc-sample
$
```

strace showed lots of futex exchanges. Why would that be?


Re: task parallelize dirEntries

2017-08-11 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Friday, 11 August 2017 at 21:33:51 UTC, Arun Chandrasekaran 
wrote:

I've modified the sample from tour.dlang.org to calculate the

[...]


RHEL 7.2 64 bit
dmd v2.075.0
ldc 1.1.0


task parallelize dirEntries

2017-08-11 Thread Arun Chandrasekaran via Digitalmars-d-learn
I've modified the sample from tour.dlang.org to calculate the md5 
digest of the files in a directory using std.parallelism.


When I run this on a dir with huge number of files, I get:

core.exception.OutOfMemoryError@src/core/exception.d(696): Memory 
allocation failed


Since dirEntries returns a range, I thought 
std.parallelism.parallel can make use of that without loading the 
entire file list into the memory.


What am I doing wrong here? Is there a way to achieve what I'm 
expecting?


```
import std.digest.md;
import std.stdio: writeln;
import std.file;
import std.algorithm;
import std.parallelism;

void printUsage()
{
writeln("Loops through a given directory and calculates the 
md5 digest of each file encountered.");

writeln("Usage: md ");
}

void safePrint(T...)(T args)
{
synchronized
{
import std.stdio : writeln;
writeln(args);
}
}

void main(string[] args)
{
if (args.length != 2)
return printUsage;

foreach (d; parallel(dirEntries(args[1], 
SpanMode.depth).filter!(f => f.isFile), 1))

{
auto md5 = new MD5Digest();
md5.reset();
auto data = cast(const(ubyte)[]) read(d.name);
md5.put(data);
auto hash = md5.finish();
import std.array;
string[] t = split(d.name, '/');
safePrint(toHexString!(LetterCase.lower)(hash), "  ", 
t[$-1]);

}
}
```


Re: RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Thursday, 23 February 2017 at 21:05:48 UTC, cym13 wrote:
It reminds me of 
https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/ which isn't what you want but may be interesting anyway.


It is interesting, indeed, thanks.


Re: RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:

Arun Chandrasekaran wrote:


I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?
why not static method or free function that returns struct? due 
to NRVO[0] it won't even be copied.


 auto lock = MyWrapper();

`MyWrapper()` may return voldemort type, so user won't create 
your struct accidentally.



[0] https://dlang.org/glossary.html#nrvo


Thanks for your help. NRVO looks interesting. However this may 
not be RAII after all. Or may be too much of C++ has spoiled me. 
I am not familiar enough with D to appreciate/question the 
language design choice. I'll accept this and move forward and see 
how my code evolves. Nevertheless, I'm still learning something 
new. :)


RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?

```
import core.sys.posix.pthread;
import core.sys.posix.sys.types;

/// Makes pthread_mutexattr_t cleanup easy when using exceptions
struct mutexattr_wrapper
{
/// Constructor
this(bool _)
{
if (pthread_mutexattr_init(_attr) != 0 ||
pthread_mutexattr_setpshared(_attr, 
PTHREAD_PROCESS_SHARED)!= 0)

  // may be I will add few more methods here
  throw custom_exception("pthread_mutexattr_ failed");
}

/// Destructor
~this() {  pthread_mutexattr_destroy(_attr);  }

/// This allows using mutexattr_wrapper as pthread_mutexattr_t
alias m_attr this;

pthread_mutexattr_t m_attr;
}
```


Concurrent containers

2017-01-27 Thread Arun Chandrasekaran via Digitalmars-d-learn

Does phobos offer concurrent containers?

I couldn't find one at http://dlang.org/phobos/std_container.html

Any other in the D land?

Arun


Re: Thread will get garbage collected?

2017-01-17 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Tuesday, 17 January 2017 at 08:12:50 UTC, ketmar wrote:

import core.thread;
import core.time;
import std.stdio;

void threadStarter (string path) {
  new Thread({
for (;;) {
  writeln(path);
  Thread.sleep(1.seconds);
}
  }).start();
}


class A {
  ~this () { import core.stdc.stdio; printf("i'm dead now!\n"); 
}

}


void main () {
  threadStarter("foo.txt");
  auto a = new A();
  import core.memory : GC;
  for (;;) {
writeln("collect...");
GC.collect();
Thread.sleep(500.msecs);
a = null;
  }
}


one will eventually see "i'm dead now!", yet "foo.txt" will 
continue to appear.


Interesting. Why doesn't the thread get GC'd in this case even 
without any reference still active?


Re: vibe.d with explicit threads/threadpool instead of fibers

2016-10-25 Thread Arun Chandrasekaran via Digitalmars-d-learn
On Tuesday, 25 October 2016 at 21:23:21 UTC, Arun Chandrasekaran 
wrote:
I am looking for a RESTful framework in D, which can interact 
with an existing backend processes (C++) via ZeroMQ and shared 
memory. I thought vibe.d might be a viable option. But I 
discussed with someone on Freenode #d and found that vibe.d 
doesn't support explicit threading [1].


I looked at vibe.d examples, one of them uses futures [1], which 
implies explicit thread handling is possible. But I'm not sure 
about pre-spawned threadpool.


[1] 
https://github.com/rejectedsoftware/vibe.d/blob/master/examples/future/source/app.d


vibe.d with explicit threads/threadpool instead of fibers

2016-10-25 Thread Arun Chandrasekaran via Digitalmars-d-learn
I am looking for a RESTful framework in D, which can interact 
with an existing backend processes (C++) via ZeroMQ and shared 
memory. I thought vibe.d might be a viable option. But I 
discussed with someone on Freenode #d and found that vibe.d 
doesn't support explicit threading [1].


Can someone confirm the behaviour?
Is it not possible to allocate a bunch of threads or a threadpool 
for vibe.d to work with?


PS: We have this working already with C++ only solution where the 
frontend communication happens on protobuf over ZeroMQ. I'm just 
trying to migrate it to RESTful interface.


[1] http://pastebin.com/cAw2yaYg


Setting native OS thread name (eg, via prctl)

2015-12-22 Thread Arun Chandrasekaran via Digitalmars-d-learn
I have this trivial code where the main thread clones a child 
thread.


import std.stdio;
import core.thread;
import std.concurrency;

class DerivedThread : Thread
{
this()
{
super();
}

void quit()
{
_quit = true;
}
private:
void setOSThreadName()
{
// TODO: Is there a way to set the native OS thread name, 
worst case, via prctl?

}
void run()
{
setOSThreadName();
while(!_quit)
{
writeln("Hello from ", thisTid);
Thread.sleep(dur!("seconds")(1));
}
writeln("I'll exit now.");
}

bool _quit = false;
string _threadName = "Derived";
}

void main()
{
auto derived = new DerivedThread();
derived.start();
Thread.sleep(dur!("seconds")(4));
derived.quit();
derived.join();
}

What do i have to do to set the thread name in setOSThreadName 
(for instance, on Linux, it will reflect in proc filesystem).


Re: Setting native OS thread name (eg, via prctl)

2015-12-22 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Tuesday, 22 December 2015 at 16:08:01 UTC, Dejan Lekic wrote:

Arun, isn't that what the 'name' property is there for?


Hi Dejan,

Thanks for a quick reply.

Setting the name property is not reflecting in the OS level. May 
be it is just used only at the object level?


After setting the thread name, I would like to see the it 
reflect, for instance, in the output of `top` command. You can 
press H in top to toggle threads and see their names there.


Cheers.