Re: wrapSocket for socket_t? As wrapFile for FILE*

2016-02-14 Thread Beginner-8 via Digitalmars-d-learn

On Sunday, 14 February 2016 at 08:19:16 UTC, Ali Çehreli wrote:

On 02/14/2016 12:03 AM, Beginner-8 wrote:

Uh, wait! Forgot about that Socket calls .close() in its dtor


Try duplicating the socket handle before handing it over to 
Socket (not compiled nor tested):


import core.sys.posix.unistd;

Socket(dup(myHandle))

I think socket handles are duplicatable :p things. Only the 
last close() would actually close the socket.


Ali


This recipe works for me! Thanks!


Re: Installing 32 bit libcurl.so.4 on Ubuntu

2016-02-14 Thread hanifnoor via Digitalmars-d-learn

I'm not 100% sure it's included, but I think so.


__
NOOR


Re: Reserving capacity in associative arrays

2016-02-14 Thread Jon D via Digitalmars-d-learn

On Monday, 15 February 2016 at 05:29:23 UTC, sigod wrote:

On Monday, 15 February 2016 at 03:22:44 UTC, Jon D wrote:

Is there a way to reserve capacity in associative arrays?
[snip]


Maybe try using this: http://code.dlang.org/packages/aammm


Thanks, I wasn't aware of this package. I'll give it a try.

--Jon


Re: Reserving capacity in associative arrays

2016-02-14 Thread sigod via Digitalmars-d-learn

On Monday, 15 February 2016 at 03:22:44 UTC, Jon D wrote:
Is there a way to reserve capacity in associative arrays? In 
some programs I've been writing I've been getting reasonable 
performance up to about 10 million entries, but beyond that 
performance is impacted considerably (say, 30 million or 50 
million entries). GC stats (via the "--DRT-gcopt=profile:1" 
option) indicate dramatic increases in gc time, which I'm 
assuming comes from resizing the underlying hash table. I'm 
guessing that by preallocating a large size the performance 
degradation would not be quite so dramatic. The underlying 
implementation of associative arrays appears to take an initial 
number of buckets, and there's a private resize() method, but 
it's not clear if there's a public way to use these.


--Jon


Maybe try using this: http://code.dlang.org/packages/aammm


Re: nanosecond time

2016-02-14 Thread Saurabh Das via Digitalmars-d-learn

On Saturday, 13 February 2016 at 19:24:44 UTC, ishwar wrote:
I am stumped on need finding interval between two events in a 
program execution  in nanoseconds. Any sample code will be 
appreciated (along with imports needed to make it work):

- time in nanoseconds-now
- do-some processing
- time in nano-second-now

Thanks


As suggested above, use a StopWatch.

I use this (not-very-cross-platform) function for latency 
measurement purposes. It is precise on Linux. For Mac OS and 
Windows, it's returns an approximation:


version(linux)  import core.sys.linux.time;
version(OSX)import core.time;
version(Windows) import core.sys.windows.windows;

static if(is(typeof(clockid_t)))
{
extern(C) nothrow int clock_gettime(clockid_t, timespec*);
}

public ulong getLocalTimestampNS() nothrow @nogc
{
static if(is(typeof(clock_gettime)))
{
timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, );
return ts.tv_sec * 10 + ts.tv_nsec;
}
static if(is(typeof(mach_absolute_time)))
{
// IMPORTANT NOTE: mach_absolute_time does not always 
return nanoseconds.
// Sometimes it returns other values. We are using it 
here as an approximation.

return mach_absolute_time();
}
version (Windows)
{
// Just an approximation
long cnt;
if (QueryPerformanceCounter())
{
return cnt * 500;
}
return 0;
}
}



Reserving capacity in associative arrays

2016-02-14 Thread Jon D via Digitalmars-d-learn
Is there a way to reserve capacity in associative arrays? In some 
programs I've been writing I've been getting reasonable 
performance up to about 10 million entries, but beyond that 
performance is impacted considerably (say, 30 million or 50 
million entries). GC stats (via the "--DRT-gcopt=profile:1" 
option) indicate dramatic increases in gc time, which I'm 
assuming comes from resizing the underlying hash table. I'm 
guessing that by preallocating a large size the performance 
degradation would not be quite so dramatic. The underlying 
implementation of associative arrays appears to take an initial 
number of buckets, and there's a private resize() method, but 
it's not clear if there's a public way to use these.


--Jon


Re: D Book page 402 Concurrency FAIL

2016-02-14 Thread Mike Parker via Digitalmars-d-learn

On Monday, 15 February 2016 at 00:58:54 UTC, Brother Bill wrote:

On Sunday, 14 February 2016 at 23:39:33 UTC, cym13 wrote:
On Sunday, 14 February 2016 at 22:54:36 UTC, Brother Bill 
wrote:
In "The D Programming Language", page 402, the toy program 
fails.


[...]


Can't reproduce with DMD 2.0.70, LDC 0.16.1 or GDC 5.3.0 on 
Linux x86_64. The code seems to work as intended.


It is failing in Windows, not Linux.
Can someone reproduce this in Windows with VisualD?


Works for me on the command line. I don't have VisualD installed 
right now, but I can't see how that would make a difference.


Re: joiner: How to iterate over immutable ranges?

2016-02-14 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 14 February 2016 at 19:32:31 UTC, Bastiaan Veelo wrote:

Thanks. I didn't know that iterating a range means mutating its 
contents. I still don't quite get it, and it is probably 
because I don't fully understand ranges. I think what confuses 
me the most is their analogy to containers. It's no problem to 
iterate over a container of immutable data, but it is for a 
range.



Maybe this [1] will help shed some light.

[1] https://www.packtpub.com/books/content/understanding-ranges


Re: joiner: How to iterate over immutable ranges?

2016-02-14 Thread Ali Çehreli via Digitalmars-d-learn

On 02/14/2016 11:32 AM, Bastiaan Veelo wrote:

> Thanks. I didn't know that iterating a range means mutating its
> contents.

That's not the case: Just like an iterator, a range must maintain some 
state to know which item is next. What needs to be mutated is that 
iteration state.


> I still don't quite get it, and it is probably because I don't
> fully understand ranges. I think what confuses me the most is their
> analogy to containers.

Yes, that analogy is the wrong one (and D's slices (or dynamic arrays) 
don't help with that).


> It's no problem to iterate over a container of immutable data, but
> it is for a range.

Not true. You can iterate over immutable data. In fact, a string is 
nothing but a container of immutable characters yet we can iterate over it.


> I thought that joiner provided a contiguous view on distinct ranges,
> without needing to touch these. Is there another method to traverse a
> range of ranges without making copies or mutation?

Not without making copies but note that the copy that you need to make 
is just the slices (i.e. views into data), which consist of just a 
length and a pointer fields. If it's acceptable for you, the following 
code calls .save on the elements and it works:


import std.algorithm.iteration;
import std.stdio;
import std.array;// <-- ADDED

void main()
{
immutable(string[])[] icycles;
icycles ~= ["one", "two"];
icycles ~= ["three", "four"];
foreach (number; icycles.map!(r => r.save).joiner)
writeln(number);
}

Again, .save on an array is cheap. What will happen is that the original 
immutable arrays will be untouched but their proxies returned by .save 
will be consumed.


Ali



Re: Implicit conversion from string to custom type?

2016-02-14 Thread Ali Çehreli via Digitalmars-d-learn

On 02/14/2016 03:43 PM, Tofu Ninja wrote:

So I wrote a simple ref counted string type because using the built in
strings without the GC is extremely painful. It there any way I can get
strings to implicitly convert to my custom string type?


No, D does not support such implicit conversions.


struct rstring {...}
void fun(rstring s) {...}
...
fun("hello world"); // Currently an error

Would be super nice if it would just call the opAssign when trying to
call fun but I suppose that has some non-obvious problems for why it
does not work that way.


The only way is to be explicit. Three common options:

fun(rstring("hello world"));
fun("hello world".to!rstring);
fun(cast(rstring)"hello world");

Relatedly, user defined types can provide implicit conversions through 
'alias this' but unfortunately, current implementation supports only one 
such operator.


Ali



Re: D Book page 402 Concurrency FAIL

2016-02-14 Thread Brother Bill via Digitalmars-d-learn

On Sunday, 14 February 2016 at 23:39:33 UTC, cym13 wrote:

On Sunday, 14 February 2016 at 22:54:36 UTC, Brother Bill wrote:
In "The D Programming Language", page 402, the toy program 
fails.


[...]


Can't reproduce with DMD 2.0.70, LDC 0.16.1 or GDC 5.3.0 on 
Linux x86_64. The code seems to work as intended.


It is failing in Windows, not Linux.
Can someone reproduce this in Windows with VisualD?


Implicit conversion from string to custom type?

2016-02-14 Thread Tofu Ninja via Digitalmars-d-learn
So I wrote a simple ref counted string type because using the 
built in strings without the GC is extremely painful. It there 
any way I can get strings to implicitly convert to my custom 
string type?


Some way to make this work...

struct rstring {...}
void fun(rstring s) {...}
...
fun("hello world"); // Currently an error

Would be super nice if it would just call the opAssign when 
trying to call fun but I suppose that has some non-obvious 
problems for why it does not work that way.


Re: D Book page 402 Concurrency FAIL

2016-02-14 Thread cym13 via Digitalmars-d-learn

On Sunday, 14 February 2016 at 22:54:36 UTC, Brother Bill wrote:
In "The D Programming Language", page 402, the toy program 
fails.


[...]


Can't reproduce with DMD 2.0.70, LDC 0.16.1 or GDC 5.3.0 on Linux 
x86_64. The code seems to work as intended.


D Book page 402 Concurrency FAIL

2016-02-14 Thread Brother Bill via Digitalmars-d-learn

In "The D Programming Language", page 402, the toy program fails.

The first fail is that enforce() needs: import std.exception;

The second fail is that when debugging, in Visual Studio 2015 
Community Edition,

it fails with this error:
  First-change exception: std.format.FormatException Unterminated 
format specifier: "%" at 
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(830).


Please provide full replacement of this toy program that works 
with D version 2.070.0


This is what was entered:

import std.concurrency, std.stdio, std.exception;

void main() {
auto low = 0,
 high = 100;
auto tid = spawn();

foreach (i; low .. high) {
writeln("Main thread: ", i);
tid.send(thisTid, i);
enforce(receiveOnly!Tid() == tid);
}
}

void writer() {
for (;;) {
auto msg = receiveOnly!(Tid, int)();
writeln("secondary thread: ", msg[1]);
msg[0].send(thisTid);
}
}


Thank you.


Re: joiner: How to iterate over immutable ranges?

2016-02-14 Thread Bastiaan Veelo via Digitalmars-d-learn
On Sunday, 14 February 2016 at 18:28:11 UTC, Jonathan M Davis 
wrote:


An immutable range fundamentally does not work. The same goes 
with const. In fact, a type that's immutable is going to fail 
isInputRange precisely because it can't possibly function as 
one. While empty and front may be callable on an immutable 
range, depending on their exact signatures, popFront cannot be, 
because it has to mutate the range in order to work.


Thanks. I didn't know that iterating a range means mutating its 
contents. I still don't quite get it, and it is probably because 
I don't fully understand ranges. I think what confuses me the 
most is their analogy to containers. It's no problem to iterate 
over a container of immutable data, but it is for a range.


I thought that joiner provided a contiguous view on distinct 
ranges, without needing to touch these. Is there another method 
to traverse a range of ranges without making copies or mutation?


Re: joiner: How to iterate over immutable ranges?

2016-02-14 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, February 14, 2016 15:24:39 Bastiaan Veelo via Digitalmars-d-learn 
wrote:
> Hi,
>
> I am having trouble getting the iteration methods in
> std.algorithm.iteration to work on immutable data:
>
> > import std.algorithm.iteration;
> > import std.stdio;
> >
> > void main()
> > {
> > string[][] cycles;
> > cycles ~= ["one", "two"];
> > cycles ~= ["three", "four"];
> > foreach (number; cycles.joiner) // This works.
> > writeln(number);
> >
> > immutable(string[])[] icycles;
> > icycles ~= ["one", "two"];
> > icycles ~= ["three", "four"];
> > foreach (number; icycles.joiner)  // Should this work?
> > writeln(number);
> > }
>
> The error message is:
>
> /d149/f840.d(15): Error: template std.algorithm.iteration.joiner
> cannot deduce function from argument types
> !()(immutable(string[])[]), candidates are:
> /opt/compilers/dmd2/include/std/algorithm/iteration.d(1911):
>std.algorithm.iteration.joiner(RoR, Separator)(RoR r, Separator
> sep) if (isInputRange!RoR && isInputRange!(ElementType!RoR) &&
> isForwardRange!Separator && is(ElementType!Separator :
> ElementType!(ElementType!RoR)))
> /opt/compilers/dmd2/include/std/algorithm/iteration.d(2194):
>std.algorithm.iteration.joiner(RoR)(RoR r) if (isInputRange!RoR
> && isInputRange!(ElementType!RoR))
>
> I had expected this to work. What did I miss?

An immutable range fundamentally does not work. The same goes with const. In
fact, a type that's immutable is going to fail isInputRange precisely
because it can't possibly function as one. While empty and front may be
callable on an immutable range, depending on their exact signatures,
popFront cannot be, because it has to mutate the range in order to work.

Arrays do typically get sliced when they're passed to functions, and array
slices are tail-const (e.g. const(int[]) is sliced as const(int)[]), so
something like

immutable string foo = "hello world";
auto result = foo.startsWith("goodbye");

will compile. But that doesn't work with ranges in general. What you're
doing is probably failing, because icycle[] is immutable(string)[], which is
immutable(char[])[], and it can't iterate over the immutable(char[]). But
regardless, if you try and use immutable with ranges, even if it works in
some cases thanks to how arrays are treated, it's just going to end up
shooting you in the foot in the end. So, I'd advise that you not bother
trying to do much with const or immutable with ranges.

- Jonathan M Davis



Re: Is this a good singleton?

2016-02-14 Thread Vladde Nordholm via Digitalmars-d-learn

On Sunday, 14 February 2016 at 07:16:54 UTC, Russel Winder wrote:
On Sat, 2016-02-13 at 18:58 +, Vladde Nordholm via 
Digitalmars-d- learn wrote:

[...]


Following the ACCU consensus: there is never, ever a good 
Singleton or reason to contemplate using one.


Obviously though there are some good ones: about dialogues in 
GUIs for example.


The good/evil-ness of Singleton is definitely in it's use: most 
people use Singleton wrongly, most uses should be banned. Hence 
the ACCU consensus.


It's all about coupling and mostly testability of code..


Thanks. I'm using David Simcha's singleton now.


Re: Singleton, alias to self?

2016-02-14 Thread Guillaume Piolat via Digitalmars-d-learn
On Sunday, 14 February 2016 at 13:23:28 UTC, Guillaume Piolat 
wrote:
On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
wrote:
I'm not sure of how to use alias efficiently, so I want to 
know if I could somehow do this (psuedo-code)


class Singleton
{
  //So instead of calling `Singleton.getSingleton()` you just 
call `Singleton`

  alias this = getSingleon()

  //code for singleton...
}

Thanks in advance,
vladde


The "alias newThing = x;" syntax is not allowed with alias 
this. It's the only exception.


You have to use

alias getSingleton this;


I've tried and fail to do what you want, even with a templated 
class + eponymous trick.


Re: Singleton, alias to self?

2016-02-14 Thread Guillaume Piolat via Digitalmars-d-learn
On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
wrote:
I'm not sure of how to use alias efficiently, so I want to know 
if I could somehow do this (psuedo-code)


class Singleton
{
  //So instead of calling `Singleton.getSingleton()` you just 
call `Singleton`

  alias this = getSingleon()

  //code for singleton...
}

Thanks in advance,
vladde


The "alias newThing = x;" syntax is not allowed with alias this. 
It's the only exception.


You have to use

alias getSingleton this;




Singleton, alias to self?

2016-02-14 Thread Vladde Nordholm via Digitalmars-d-learn
I'm not sure of how to use alias efficiently, so I want to know 
if I could somehow do this (psuedo-code)


class Singleton
{
  //So instead of calling `Singleton.getSingleton()` you just 
call `Singleton`

  alias this = getSingleon()

  //code for singleton...
}

Thanks in advance,
vladde


joiner: How to iterate over immutable ranges?

2016-02-14 Thread Bastiaan Veelo via Digitalmars-d-learn

Hi,

I am having trouble getting the iteration methods in 
std.algorithm.iteration to work on immutable data:



import std.algorithm.iteration;
import std.stdio;

void main()
{
string[][] cycles;
cycles ~= ["one", "two"];
cycles ~= ["three", "four"];
foreach (number; cycles.joiner) // This works.
writeln(number);

immutable(string[])[] icycles;
icycles ~= ["one", "two"];
icycles ~= ["three", "four"];
foreach (number; icycles.joiner)  // Should this work?
writeln(number);
}


The error message is:

/d149/f840.d(15): Error: template std.algorithm.iteration.joiner 
cannot deduce function from argument types 
!()(immutable(string[])[]), candidates are:
/opt/compilers/dmd2/include/std/algorithm/iteration.d(1911):  
  std.algorithm.iteration.joiner(RoR, Separator)(RoR r, Separator 
sep) if (isInputRange!RoR && isInputRange!(ElementType!RoR) && 
isForwardRange!Separator && is(ElementType!Separator : 
ElementType!(ElementType!RoR)))
/opt/compilers/dmd2/include/std/algorithm/iteration.d(2194):  
  std.algorithm.iteration.joiner(RoR)(RoR r) if (isInputRange!RoR 
&& isInputRange!(ElementType!RoR))


I had expected this to work. What did I miss?

Thanks,
Bastiaan.


Re: Singleton, alias to self?

2016-02-14 Thread tcak via Digitalmars-d-learn
On Sunday, 14 February 2016 at 12:56:51 UTC, Vladde Nordholm 
wrote:
I'm not sure of how to use alias efficiently, so I want to know 
if I could somehow do this (psuedo-code)


class Singleton
{
  //So instead of calling `Singleton.getSingleton()` you just 
call `Singleton`

  alias this = getSingleon()

  //code for singleton...
}

Thanks in advance,
vladde


"this" is for an instance of class (or struct). There is no 
instance of define an alias at that time. I am not sure, but you 
can check "opCall" function as static for this. Give it a try at 
least.


Re: wrapSocket for socket_t? As wrapFile for FILE*

2016-02-14 Thread Beginner-8 via Digitalmars-d-learn

On Sunday, 14 February 2016 at 07:33:11 UTC, Ali Çehreli wrote:


Maybe another option is to duplicate the socket handle


Sure!

Nevertheless, it is need method for socket_t duplication. 
Something like:


class Socket
{
...
static Socket dup(socket_t)
...
}

before giving it to Socket but I am failing to find a 
definitive answer or an example.




Re: wrapSocket for socket_t? As wrapFile for FILE*

2016-02-14 Thread Ali Çehreli via Digitalmars-d-learn

On 02/14/2016 12:03 AM, Beginner-8 wrote:

Uh, wait! Forgot about that Socket calls .close() in its dtor


Try duplicating the socket handle before handing it over to Socket (not 
compiled nor tested):


import core.sys.posix.unistd;

Socket(dup(myHandle))

I think socket handles are duplicatable :p things. Only the last close() 
would actually close the socket.


Ali



Re: wrapSocket for socket_t? As wrapFile for FILE*

2016-02-14 Thread Beginner-8 via Digitalmars-d-learn

Uh, wait! Forgot about that Socket calls .close() in its dtor


Photoshop programming

2016-02-14 Thread Patience via Digitalmars-d-learn
Photoshop has the ability to be controlled by scripts and 
programming languages. For example, C# can be used to access 
photoshop by adding the appropriate reference and using 
directives. I believe it is COM based but I am not totally sure.


I've tried reading the docs but it's not making much sense.

http://www.lunesu.com/uploads/ModernCOMProgramminginD.pdf

links at the bottom are down.

It says one has to create a wrapper, but then talks like it can 
be done automatically. I assume that is what project does? But 
then one has to create an idl, not sure what that is ;\


Any info on how to do this stuff properly?