Out of memory error (even when using destroy())

2017-05-25 Thread realhet via Digitalmars-d-learn

Hi,

I'm kinda new to the D language and I love it already. :D So far 
I haven't got any serious problems but this one seems like beyond 
me.


import std.stdio;
void main(){
foreach(i; 0..2000){
writeln(i);
auto st = new ubyte[500_000_000];
destroy(st); //<-this doesnt matter
}
}

Compiled with DMD 2.074.0 Win32 it produces the following output:
0
1
2
core.exception.OutOfMemoryError@src\core\exception.d(696): Memory 
allocation failed


It doesn't matter that I call destroy() or not. This is ok 
because as I learned: destroy only calls the destructor and marks 
the memory block as unused.


But I also learned that GC will start to collect when it run out 
of memory but in this time the following happens:
3x half GB of allocations and deallocations, and on the 4th the 
system runs out of the 2GB
 limit which is ok. At this point the GC already has 1.5GB of 
free memory but instead of using that, it returns a Memory Error. 
Why?


Note: This is not a problem when I use smaller blocks (like 50MB).
But I want to use large blocks, without making a slow wrapper 
that emulates a large block by using smaller GC allocated blocks.


Is there a solution to this?

Thank You!


Re: Sudoku Py / C++11 / D?

2017-05-25 Thread Era Scarecrow via Digitalmars-d-learn

On Wednesday, 15 August 2012 at 20:13:10 UTC, Era Scarecrow wrote:

On Wednesday, 15 August 2012 at 15:39:26 UTC, ixid wrote:
Could you supply your code? Which one are you using as the 
hardest? If you're solving the 1400 second one in 12 seconds 
that's very impressive, I can't get it below 240 seconds.


Expanded to 225 lines after comments and refactoring for names. 
I think it should be fairly easy to follow.


https://github.com/rtcvb32/D-Sudoku-Solver


 While an old thread, I decided to try a different approach to 
sudoku solving. In no way is this better, just a different 
approach. At 200 lines (needs some heavy unittests added, but 
appears to work).


 Using a sorting method to solve the puzzle. The idea is to take 
your puzzle, sort it by weight (how many possible numbers) and 
only take guesses with the smallest number of combinations 
possible, meaning any puzzle with 1 solution won't take long. The 
idea behind this method is to ignore combinations that might 
never come up; Afterall if you have a block with 2 possibilities, 
why start brute forcing the one with 7? Fewer wasted cycles. Yes 
it still uses brute force and built-in backtracking (and also 
outputs all combinations of a solution).


 Trying the REALLY REALLY hard one from before? (17 numbers) 
Well... I had it run in the background for a few hours, and got 
69,555,823 answers before the output (610Mb compressed, 11,067Mb 
uncompressed) simply filled up the free space on my ramdrive thus 
crashing the program.


Re: [Starting with D] d2html.d issues an error

2017-05-25 Thread Basile B. via Digitalmars-d-learn

On Thursday, 25 May 2017 at 15:02:33 UTC, Oleksii wrote:

Hi everyone,

Could you please help me? I'm get the following error from 
all.sh:


  $ /e/D/dmd2/windows/bin/shell.exe all.sh
  shell 1.05
  ..\..\windows\bin\dmd d2html
  d2html.d(18): Error: module stream is in file 'std\stream.d' 
which cannot be read

  import path[0] = E:\D\dmd2\windows\bin\..\..\src\phobos
  import path[1] = 
E:\D\dmd2\windows\bin\..\..\src\druntime\import


  --- errorlevel 1

Where is std\stream.d located, and how can I make DMD find it?

Thanks,
--
Oleksii


Examples will work again in the next DMD release. Recently 
There's been some changes to fix them all. For now you can copy 
the new versions from the repository.


https://github.com/dlang/dmd/tree/master/samples


Re: Code improvement for DNA reverse complement?

2017-05-25 Thread aberba via Digitalmars-d-learn

On Friday, 19 May 2017 at 07:46:13 UTC, Stefan Koch wrote:

On Friday, 19 May 2017 at 07:29:44 UTC, biocyberman wrote:
I am solving this problem http://rosalind.info/problems/revc/ 
as an exercise to learn D. This is my solution:


https://dpaste.dzfl.pl/8aa667f962b7

Is there some D tricks I can use to make the 
`reverseComplement` function more concise and speedy? Any 
other comments for improvement of the whole solution are also 
much appreciated.


I think doing a switch or even a if-else chain would be faster 
then using an AA.


As part of your work on newCTFE, you are in a position to write a 
book titled "Fast D", about code that compiles, execute fast and 
efficiently.


Re: Code improvement for DNA reverse complement?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 10:41 PM, Ali Çehreli wrote:
I would like to acknowledge you in the book hopefully with your real 
name but if you don't want or care, I will use ag0aep6g. :)


Thanks, but I don't really care for the recognition. If anything, 
please use ag0aep6g.


Re: Code improvement for DNA reverse complement?

2017-05-25 Thread Ali Çehreli via Digitalmars-d-learn

On 05/22/2017 03:35 AM, ag0aep6g wrote:

> But Ali Çehreli covers it in his book on the "immutability" page (I
> would have expected to find it on the "enum" page):
>
> 
http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.enum


Thank you for your feedback. I'm inserting a forward reference from the 
enum chapter.


I would like to acknowledge you in the book hopefully with your real 
name but if you don't want or care, I will use ag0aep6g. :)


Ali



Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 09:15 PM, Moritz Maxeiner wrote:

Ok, you are right. Open a bug report?


Sure. https://issues.dlang.org/show_bug.cgi?id=17435


Re: Searching strings with indexOf vs countUntil

2017-05-25 Thread Stanislav Blinov via Digitalmars-d-learn

On Thursday, 25 May 2017 at 18:13:15 UTC, Anonymouse wrote:

Part of the strings I'm working with can be assumed to be only 
ASCII, yes. indexOf only wants strings or char[]s, but 
interestingly if I use the same benchmark but have countUntil 
work on raw ubyte[]s, it is faster. See 
https://dpaste.dzfl.pl/2e095f7d18be.


There's a less ugly cast in std.string:

import std.string : representation;

//(cast(ubyte[])line).countUntil(cast(ubyte[])" :");
line.representation.countUntil(" :".representation);


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 19:09:06 UTC, ag0aep6g wrote:

[...]

Also, simply instantiating a function template inside a class 
doesn't result in a method. If it did, the function/method 
should be able to access class members. But it can't:



int ft()() { return age; } /* Error: undefined identifier age */
class Person
{
int age = 42;
alias method = ft!(); /* error instantiating */
}



Ok, you are right. Open a bug report?


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 08:14 PM, Moritz Maxeiner wrote:
Well, then I guess we need a compiler guy to clear this up, because from 
my point of view, the template is instantiated within the scope of the 
class (way before we reach the mixin), nesting the template's scope 
within the class' scope, which makes the function within that template's 
scope a member function of the class.


You get a very similar error when you instantiate outside:


class Person { int age; }
string AutoConstructor(fields ...)() { return ""; }
enum s = AutoConstructor!(Person.age);
/* Error: need 'this' for 'AutoConstructor' of type 'string()' */


So I don't think it has anything to do with having the instantiation 
inside the class. Passing a class member to a function template seems to 
be the trigger. But it's more complicated than that. The instantiation 
itself goes through. The error only occurs when you actually call the 
function.


Also, simply instantiating a function template inside a class doesn't 
result in a method. If it did, the function/method should be able to 
access class members. But it can't:



int ft()() { return age; } /* Error: undefined identifier age */
class Person
{
int age = 42;
alias method = ft!(); /* error instantiating */
}



Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 13:53:01 UTC, ag0aep6g wrote:

On 05/25/2017 03:13 PM, Moritz Maxeiner wrote:
After thinking about this a bit I think I know why it doesn't 
work without static and it's not a compiler bug. Since


---
string AutoConstructor(fields ...)() {}
---

is just syntax sugar for
---
template AutoConstructor(fields ...)
{
 string AutoConstructor() {}
}
---

instantiating the template AutoConstructor inside class Person 
gives you a non-static member function AutoConstructor of 
class Person, so obviously we need an instance of Person to 
call it on. Or make it a static member function.


I don't think that's it.

The function itself is not mixed into the class. It's called 
and the result is mixed in. I don't see how it makes sense if 
the compiler tries to turn the called function into a method.


Well, then I guess we need a compiler guy to clear this up, 
because from my point of view, the template is instantiated 
within the scope of the class (way before we reach the mixin), 
nesting the template's scope within the class' scope, which makes 
the function within that template's scope a member function of 
the class.


Re: Searching strings with indexOf vs countUntil

2017-05-25 Thread Anonymouse via Digitalmars-d-learn

On Thursday, 25 May 2017 at 11:55:21 UTC, Kagamin wrote:
I would guess indexOf returns a value suitable for indexing, 
therefore it counts code units, while countUntil counts range 
elements - code points in case of a string. Also number of code 
points is not suitable for indexing an utf8 string, it can be 
used to allocate a dstring, but not so much for anything else. 
What do you use the resulting value for?


I see. I would have thought indexOf would be more keen to decode, 
but that's bias talking.


The project is an IRC bot. I use indexOf/used countUntil to slice 
up strings into one part leading up to some separator (" :" in 
that example), and another into everything after it. See 
https://dpaste.dzfl.pl/326e450058c1.



On Thursday, 25 May 2017 at 12:46:43 UTC, Basile B. wrote:
To get rid of decoding he can cast to ubyte[]. I would do that 
if sure that the input is only made of ascii chars.


Part of the strings I'm working with can be assumed to be only 
ASCII, yes. indexOf only wants strings or char[]s, but 
interestingly if I use the same benchmark but have countUntil 
work on raw ubyte[]s, it is faster. See 
https://dpaste.dzfl.pl/2e095f7d18be.


Re: Mutiple AliasSeq as input to template

2017-05-25 Thread jmh530 via Digitalmars-d-learn

On Thursday, 25 May 2017 at 16:36:45 UTC, jmh530 wrote:

[snip]


I haven't played around with it fully, but it seems like the 
following resolves my issue in a sort of manual way:


template Process1(A, B)
{
static if (!isIndex!B)
alias Process1 = A;
else
alias Process1 = B;
}

template Process(size_t n, A...)
if (n > 0)
{
import std.meta : AliasSeq;

alias B = A[0..n];
alias C = A[n..$];

static if (n == 1)
{
alias Process = AliasSeq!(Process1!(B[0], C[0]));
}
else static if (n > 1)
{
alias Process = AliasSeq!(Process1!(B[0], C[0]),
  Process!(n - 
1, B[1..$], C[1..$]));
}
}





Re: site examples

2017-05-25 Thread Seb via Digitalmars-d-learn

On Thursday, 25 May 2017 at 16:07:34 UTC, bachmeier wrote:

On Thursday, 25 May 2017 at 15:41:47 UTC, crimaniak wrote:
I tried to run an example from the site. Example from 
https://dlang.org/phobos/std_regex.html#replaceAllInto 
section. And this is problem number 0: I can't give a link to 
the example, only to section. The section can be long, there 
can be more than one example, so it's good to have an anchor 
for each example. Problem #1: "Compilation output (255: 
Unknown signal 255)
unable to fork: Cannot allocate memory." As I understand it, 
this is a common problem, and it leaves a bad impression.


You do realize that this is due DPaste's poor performance?
There's not much we can do except for replacing it with a proper 
execution backend.
I gave it a short try a couple of weeks ago, but unfortunately I 
stopped as my intended replacement (DLang Tour) didn't support 
stdin and args:


https://github.com/dlang/dlang.org/pull/1647

I can confirm that this is a problem. It does indeed leave a 
bad impression, and needs to be fixed or removed.


Just for the record: as part of the Phobos CI system, we _do_ 
have a script that is run for every PR and automatically extracts 
all public unittests and tries to run them separately, exactly to 
avoid there's a bad impression ;-)

In case you are interested, the regarding Makefile target is:


make -f posix.mak publictests


You can also run it individually for a single module, e.g.


make -f posix.mak std/regex/package.publictests


For reference, the example that we are talking about is:

https://github.com/dlang/phobos/blob/master/std/regex/package.d#L1422

I do agree, that this is a horrible example for potential users 
and should be replaced with a proper example.


It seems to me that examples on the site require additional 
work and in the current form are counterproductive in terms of 
attracting new users.


When I wrote the bits that allowed the examples to be runnable, I 
also wrote the tooling to ensure that they are _always_ runnable. 
For more infos: 
http://dlang.org/blog/2017/03/08/editable-and-runnable-doc-examples-on-dlang-org


However, it was deemed too noisy to add "import , 
std.stdio;" to the examples. That's why we do this on-the-fly:


https://github.com/dlang/dlang.org/blob/master/js/run_examples.js#L29

If you get the approval, it would be pretty trivial (i.e. a 
two-liner) to add these imports, considering that (1) the test 
extractor adds the import of the current module:


https://github.com/dlang/tools/blob/master/styles/tests_extractor.d

And (2) we already have a rewrite pipeline in-place to transform 
asserts into writelns:


https://github.com/dlang/dlang.org/blob/master/assert_writeln_magic.d

I am in agreement. The easiest solution would be to add 
examples to the wiki and link to them from the documentation.


Sorry, but I think you will have a _very_ hard time, because
- People won't be able to find them
- Examples in D get "broken" very quickly due to missing imports, 
minor changes in Phobos, compiler "features" etc.


Why do I believe the latter?
- There was a time when CircleCi wasn't enforced at Phobos and 
people used to break it every one or two weeks...
- I am one of the maintainer of the DLang Tour and before there 
was a CI, people used to make tiny good-looking changes to the 
examples there, with the result that 50% of all examples weren't 
compilable
- Some of the official examples shipped with dmd don't compile 
anymore


Mutiple AliasSeq as input to template

2017-05-25 Thread jmh530 via Digitalmars-d-learn
I'm trying to process one AliasSeq based on the types in another. 
I've tried to sketch it out below. However, it doesn't work 
because when you combine together two AliasSeq's in the template, 
then it creates one AliasSeq.


The only other thing I considered was a staticMap with isIndex, 
but I wasn't sure if it would work with two AliasSeqs.



enum isIndex(I) = is(I : size_t);

template Process(A, B)
{
import std.meta : AliasSeq;

static if (A.length == 1)
{
static if (!isIndex!B[0])
alias Process = AliasSeq!(A[0]);
else
alias Process = AliasSeq!(B[0]);
}
else static if (A.length > 1)
{
static if (!isIndex!B[0])
alias Process = AliasSeq!(A[0], Process!(A[1..$], 
B[1..$]));
else
alias Process = AliasSeq!(B[0], Process!(A[1..$], 
B[1..$]));
}
}

void main()
{
import std.meta : AliasSeq;

alias T = AliasSeq!(int[], int[]);
alias U = AliasSeq!(int, string);
	alias V = Process!(T, U); //result should be AliasSeq!(int, 
int[])

}


Re: site examples

2017-05-25 Thread bachmeier via Digitalmars-d-learn

On Thursday, 25 May 2017 at 15:41:47 UTC, crimaniak wrote:
I tried to run an example from the site. Example from 
https://dlang.org/phobos/std_regex.html#replaceAllInto section. 
And this is problem number 0: I can't give a link to the 
example, only to section. The section can be long, there can be 
more than one example, so it's good to have an anchor for each 
example. Problem #1: "Compilation output (255: Unknown signal 
255)
unable to fork: Cannot allocate memory." As I understand it, 
this is a common problem, and it leaves a bad impression.


I can confirm that this is a problem. It does indeed leave a bad 
impression, and needs to be fixed or removed.


It seems to me that examples on the site require additional 
work and in the current form are counterproductive in terms of 
attracting new users.


I am in agreement. The easiest solution would be to add examples 
to the wiki and link to them from the documentation.


Re: site examples

2017-05-25 Thread Stanislav Blinov via Digitalmars-d-learn

On Thursday, 25 May 2017 at 15:41:47 UTC, crimaniak wrote:

It seems to me that examples on the site require additional 
work and in the current form are counterproductive in terms of 
attracting new users.


https://github.com/dlang/phobos


site examples

2017-05-25 Thread crimaniak via Digitalmars-d-learn
I tried to run an example from the site. Example from 
https://dlang.org/phobos/std_regex.html#replaceAllInto section. 
And this is problem number 0: I can't give a link to the example, 
only to section. The section can be long, there can be more than 
one example, so it's good to have an anchor for each example. 
Problem #1: "Compilation output (255: Unknown signal 255)
unable to fork: Cannot allocate memory." As I understand it, this 
is a common problem, and it leaves a bad impression. Ok, I copy 
example text and try to compile it. Oh, I need to make main(). 
Ok. "t1.d(6): Error: undefined identifier 'regex'

t1.d(12): Error: undefined identifier 'replaceAllInto'
t1.d(14): Error: 'writeln' is not defined, perhaps you need to 
import std.stdio; ?"
Problem #3: we have imports in the example, but not all imports. 
Ok, I add it. Compile, run. A lot of commas. Is this proof that 
the method worked? Yes. Do these commas illustrate how this 
method works? Absolutely not. This is a replacement method, so I 
expect to see "before" and "after".
It seems to me that examples on the site require additional work 
and in the current form are counterproductive in terms of 
attracting new users.


Re: [Starting with D] d2html.d issues an error

2017-05-25 Thread Stefan Koch via Digitalmars-d-learn

On Thursday, 25 May 2017 at 15:02:33 UTC, Oleksii wrote:

Hi everyone,

Could you please help me? I'm get the following error from 
all.sh:


  $ /e/D/dmd2/windows/bin/shell.exe all.sh
  shell 1.05
  ..\..\windows\bin\dmd d2html
  d2html.d(18): Error: module stream is in file 'std\stream.d' 
which cannot be read

  import path[0] = E:\D\dmd2\windows\bin\..\..\src\phobos
  import path[1] = 
E:\D\dmd2\windows\bin\..\..\src\druntime\import


  --- errorlevel 1

Where is std\stream.d located, and how can I make DMD find it?

Thanks,
--
Oleksii


std.stream was deprecated and is now apparently removed.



[Starting with D] d2html.d issues an error

2017-05-25 Thread Oleksii via Digitalmars-d-learn

Hi everyone,

Could you please help me? I'm get the following error from all.sh:

  $ /e/D/dmd2/windows/bin/shell.exe all.sh
  shell 1.05
  ..\..\windows\bin\dmd d2html
  d2html.d(18): Error: module stream is in file 'std\stream.d' 
which cannot be read

  import path[0] = E:\D\dmd2\windows\bin\..\..\src\phobos
  import path[1] = E:\D\dmd2\windows\bin\..\..\src\druntime\import

  --- errorlevel 1

Where is std\stream.d located, and how can I make DMD find it?

Thanks,
--
Oleksii


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 03:13 PM, Moritz Maxeiner wrote:
After thinking about this a bit I think I know why it doesn't work 
without static and it's not a compiler bug. Since


---
string AutoConstructor(fields ...)() {}
---

is just syntax sugar for
---
template AutoConstructor(fields ...)
{
 string AutoConstructor() {}
}
---

instantiating the template AutoConstructor inside class Person gives you 
a non-static member function AutoConstructor of class Person, so 
obviously we need an instance of Person to call it on. Or make it a 
static member function.


I don't think that's it.

The function itself is not mixed into the class. It's called and the 
result is mixed in. I don't see how it makes sense if the compiler tries 
to turn the called function into a method.


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 12:35:57 UTC, Moritz Maxeiner wrote:

On Thursday, 25 May 2017 at 11:31:43 UTC, ag0aep6g wrote:

On 05/25/2017 12:52 PM, Moritz Maxeiner wrote:
Be aware, though, that constructors mixed in via a mixin 
template behave differently with regards to overloading[1].


[1] https://issues.dlang.org/show_bug.cgi?id=11500


Of course it couldn't be that simple :(

Adam's workaround (`alias __ctor = mixin_thing.__ctor;`) might 
be workable, though.


If that makes the usage too verbose, then a string mixin is 
the way to go, I guess. It's immune to issue 11500, but 
AutoConstructor isn't as nice to look at:



static string AutoConstructor(fields ...)()
{
[...]
}


Weird: AutoConstructor needs to be static. Doesn't make sense 
to me. Looks like a compiler bug.


Nice. If you look at my initial reply to OP you'll see that I 
mentioned that error and I worked around it differently (worse 
than you). Didn't know that static would fix that (and I don't 
understand why it does), but thanks for the info!


After thinking about this a bit I think I know why it doesn't 
work without static and it's not a compiler bug. Since


---
string AutoConstructor(fields ...)() {}
---

is just syntax sugar for
---
template AutoConstructor(fields ...)
{
string AutoConstructor() {}
}
---

instantiating the template AutoConstructor inside class Person 
gives you a non-static member function AutoConstructor of class 
Person, so obviously we need an instance of Person to call it on. 
Or make it a static member function.


Re: Sorted sequences

2017-05-25 Thread Era Scarecrow via Digitalmars-d-learn

On Thursday, 25 May 2017 at 10:39:01 UTC, Russel Winder wrote:
C++ has std:priority_queue as a wrapper around a heap to create 
a sorted queue. Am I right in thinking that D has no direct 
equivalent, that you have to build you own wrapper around a 
heap?


 Do you even need a wrapper?

 Glancing at priority_queue it more or less ensures the largest 
element is always first...


 However glancing at the D documentation, you already get the 
same thing.


https://dlang.org/phobos/std_container_binaryheap.html
@property ElementType!Store front();
Returns a copy of the front of the heap, which is the 
largest element according to less.



 A quick test shows inserted items are both sorted and you get 
the largest element immediately. So honestly it sounds like it's 
already built in... no modification or wrapper needed, unless of 
course I'm missing something?


Re: Searching strings with indexOf vs countUntil

2017-05-25 Thread Basile B. via Digitalmars-d-learn

On Thursday, 25 May 2017 at 11:55:21 UTC, Kagamin wrote:
I would guess indexOf returns a value suitable for indexing, 
therefore it counts code units, while countUntil counts range 
elements - code points in case of a string. Also number of code 
points is not suitable for indexing an utf8 string, it can be 
used to allocate a dstring, but not so much for anything else. 
What do you use the resulting value for?


To get rid of decoding he can cast to ubyte[]. I would do that if 
sure that the input is only made of ascii chars.


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 11:31:43 UTC, ag0aep6g wrote:

On 05/25/2017 12:52 PM, Moritz Maxeiner wrote:
Be aware, though, that constructors mixed in via a mixin 
template behave differently with regards to overloading[1].


[1] https://issues.dlang.org/show_bug.cgi?id=11500


Of course it couldn't be that simple :(

Adam's workaround (`alias __ctor = mixin_thing.__ctor;`) might 
be workable, though.


If that makes the usage too verbose, then a string mixin is the 
way to go, I guess. It's immune to issue 11500, but 
AutoConstructor isn't as nice to look at:



static string AutoConstructor(fields ...)()
{
[...]
}


Weird: AutoConstructor needs to be static. Doesn't make sense 
to me. Looks like a compiler bug.


Nice. If you look at my initial reply to OP you'll see that I 
mentioned that error and I worked around it differently (worse 
than you). Didn't know that static would fix that (and I don't 
understand why it does), but thanks for the info!




Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Seb via Digitalmars-d-learn

On Thursday, 25 May 2017 at 11:31:43 UTC, ag0aep6g wrote:

On 05/25/2017 12:52 PM, Moritz Maxeiner wrote:

[...]


Of course it couldn't be that simple :(

Adam's workaround (`alias __ctor = mixin_thing.__ctor;`) might 
be workable, though.


[...]


FWIW if this is properly working (e.g. with 11500 fixed), it 
would make a lot of sense to me to add it to Phobos as this looks 
like a very useful piece (useful enough maybe even try a DIP to 
get it into D).


Re: Searching strings with indexOf vs countUntil

2017-05-25 Thread Kagamin via Digitalmars-d-learn
I would guess indexOf returns a value suitable for indexing, 
therefore it counts code units, while countUntil counts range 
elements - code points in case of a string. Also number of code 
points is not suitable for indexing an utf8 string, it can be 
used to allocate a dstring, but not so much for anything else. 
What do you use the resulting value for?


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 12:52 PM, Moritz Maxeiner wrote:
Be aware, though, that constructors mixed in via a mixin template behave 
differently with regards to overloading[1].


[1] https://issues.dlang.org/show_bug.cgi?id=11500


Of course it couldn't be that simple :(

Adam's workaround (`alias __ctor = mixin_thing.__ctor;`) might be 
workable, though.


If that makes the usage too verbose, then a string mixin is the way to 
go, I guess. It's immune to issue 11500, but AutoConstructor isn't as 
nice to look at:



static string AutoConstructor(fields ...)()
{
import std.meta: staticMap;
import std.traits: fullyQualifiedName;
import std.string: join;

enum fqns = staticMap!(fullyQualifiedName, fields);
auto fields_str = "std.meta.AliasSeq!(" ~ [fqns].join(", ") ~ ")";

return "
static import std.meta;
this(typeof(" ~ fields_str ~ ") args)
{
" ~ fields_str ~ " = args;
}
";
}

class Person
{
string name;
int age;
mixin(AutoConstructor!(age, name));
this(float f) {}
}

void main()
{
auto p = new Person(42, "Arthur");
assert(p.age == 42);
assert(p.name == "Arthur");
}


Weird: AutoConstructor needs to be static. Doesn't make sense to me. 
Looks like a compiler bug.


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 10:42:00 UTC, ag0aep6g wrote:

On 05/25/2017 10:34 AM, JN wrote:

class Person
{
   string name;
   int age;
   mixin(AutoConstructor!(age, name));
}

[...]
I am not looking for code, I can try that myself, just asking 
if such things are possible?


I know you're not asking for code, but without experimenting I 
wouldn't have known that this works. In the end it's 
surprisingly simple:



mixin template AutoConstructor(fields ...)
{
this(typeof(fields) args) { fields = args; }
}



Be aware, though, that constructors mixed in via a mixin template 
behave differently with regards to overloading[1].


[1] https://issues.dlang.org/show_bug.cgi?id=11500


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread ag0aep6g via Digitalmars-d-learn

On 05/25/2017 10:34 AM, JN wrote:

class Person
{
   string name;
   int age;
   mixin(AutoConstructor!(age, name));
}

[...]
I am not looking for code, I can try that myself, just asking if such 
things are possible?


I know you're not asking for code, but without experimenting I wouldn't 
have known that this works. In the end it's surprisingly simple:



mixin template AutoConstructor(fields ...)
{
this(typeof(fields) args) { fields = args; }
}

class Person
{
string name;
int age;
mixin AutoConstructor!(age, name);
}

void main()
{
auto p = new Person(42, "Arthur");
assert(p.age == 42);
assert(p.name == "Arthur");
}



Sorted sequences

2017-05-25 Thread Russel Winder via Digitalmars-d-learn
C++ has std:priority_queue as a wrapper around a heap to create a
sorted queue. Am I right in thinking that D has no direct equivalent,
that you have to build you own wrapper around a heap?
 
-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc
Description: This is a digitally signed message part


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Moritz Maxeiner via Digitalmars-d-learn

On Thursday, 25 May 2017 at 08:34:54 UTC, JN wrote:
One of my favourite language features of Dart (other one being 
factory constructors) are auto-assign constructors, for example 
(writing it in pseudo-D):


class Person
{
  string name;
  int age;
  this(this.age, this.name);
}

would translate to

class Person
{
  string name;
  int age;
  this(int age, string name)
  {
this.age = age;
this.name = name;
  }
}


It saves a lot of typing in the long run when doing lots of 
OOP, is there a way to reproduce such behaviour using mixins? I 
was thinking of some interface like:


class Person
{
  string name;
  int age;
  mixin(AutoConstructor!(age, name));
}



The syntax would be mixin(AutoConstructor!(Person, "age", 
"name")), as
1. the compiler will throw you a `need 'this' to access member 
AutoConstructor` if you try to pass the arguments the way you did 
in the above
2. to avoid having to do ugly things to then get back at the type 
of those arguments again you will want to pass the type of the 
class in.


Searching strings with indexOf vs countUntil

2017-05-25 Thread Anonymouse via Digitalmars-d-learn
I was profiling my program with callgrind and saw that a lot of 
time was spent in countUntil (std.algorithm.searching) on 
strings. I had chosen to use it instead of indexOf (std.string), 
with the plain assumption that countUntil wouldn't decode, while 
indexOf would.


Comparing microbenchmarks between the two I see that countUntil 
is some 7x slower.



import std.stdio : writeln;
import std.string : indexOf;
import std.algorithm.searching : countUntil;
import std.datetime;

enum line = ":zorael!~n...@asdf.asdf.asdf PRIVMSG #d :Hello 
world!";

enum iterations = 1_000_000;

void main()
{
StopWatch sw;

sw.start();
foreach (i; 0..iterations)
{
line.indexOf(" :");
}
sw.stop();

immutable usecsIndexOf = sw.peek().usecs;

sw.reset();
sw.start();
foreach (i; 0..iterations)
{
line.countUntil(" :");
}
sw.stop();

immutable usecsCountUntil = sw.peek().usecs;

writeln("indexOf:", usecsIndexOf.usecs);
writeln("countUntil: ", usecsCountUntil.usecs);
writeln(cast(double)usecsCountUntil/usecsIndexOf, "x");
}


https://dpaste.dzfl.pl/0319edb79ec8 gives the output:

indexOf:160 ms and 534 μs
countUntil: 1 sec, 146 ms, and 842 μs
7.14392x

What is the fundamental difference between the two? When should I 
ever use countUntil on strings?


Re: Any way to reproduce Dart style constructors?

2017-05-25 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 25 May 2017 at 08:34:54 UTC, JN wrote:
One of my favourite language features of Dart (other one being 
factory constructors) are auto-assign constructors, for example 
(writing it in pseudo-D):


class Person
{
  string name;
  int age;
  this(this.age, this.name);
}

would translate to

class Person
{
  string name;
  int age;
  this(int age, string name)
  {
this.age = age;
this.name = name;
  }
}


It saves a lot of typing in the long run when doing lots of 
OOP, is there a way to reproduce such behaviour using mixins? I 
was thinking of some interface like:


class Person
{
  string name;
  int age;
  mixin(AutoConstructor!(age, name));
}

but I don't know if that's even doable using mixins. Even 
cooler might be something like an annotation (feels a bit 
Lombok-like from Java):


@AutoConstructor
class Person
{
  string name;
  int age;
}

but I don't think it's doable in D right now.

I am not looking for code, I can try that myself, just asking 
if such things are possible?


Not sure about classes (I don't use them much) but structs have 
an automatically defined constructor. It is most definitely 
possible to do that with a mixin template (I remember someone 
recently showing autogenerating properties for readonly private 
members, sorry don't have a link).


The UDA approach won't work because they are there for 
reflection. You can generate code based on the presence (or 
absence) of a UDA but you can't synthesise new methods with that 
without forwarding from a wrapper.


Any way to reproduce Dart style constructors?

2017-05-25 Thread JN via Digitalmars-d-learn
One of my favourite language features of Dart (other one being 
factory constructors) are auto-assign constructors, for example 
(writing it in pseudo-D):


class Person
{
  string name;
  int age;
  this(this.age, this.name);
}

would translate to

class Person
{
  string name;
  int age;
  this(int age, string name)
  {
this.age = age;
this.name = name;
  }
}


It saves a lot of typing in the long run when doing lots of OOP, 
is there a way to reproduce such behaviour using mixins? I was 
thinking of some interface like:


class Person
{
  string name;
  int age;
  mixin(AutoConstructor!(age, name));
}

but I don't know if that's even doable using mixins. Even cooler 
might be something like an annotation (feels a bit Lombok-like 
from Java):


@AutoConstructor
class Person
{
  string name;
  int age;
}

but I don't think it's doable in D right now.

I am not looking for code, I can try that myself, just asking if 
such things are possible?