Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Era Scarecrow via Digitalmars-d-learn

On Friday, 5 July 2019 at 16:25:10 UTC, Nick Treleaven wrote:
Yes, I was wondering why the compiler doesn't statically 
allocate it automatically as an optimization.


 Which i would think it could, but silently adds .dup to the end 
as it points to a unnamed memory block of N size. Or if it's 
immutable i would point to the same shared data.


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Patrick Schluter via Digitalmars-d-learn

On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote:

On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote:

immutable(int[]) f() @nogc {
return [1,2];
}

onlineapp.d(2): Error: array literal in `@nogc` function 
`onlineapp.f` may cause a GC allocation


This makes dynamic array literals unusable with @nogc, and 
adds to GC pressure for no reason. What code would break if 
dmd used only static data for [1,2]?


int[] in D is not an array but a fat pointer. When one realizes 
that then it become quite obvious why [1,2] was allocated. 
There is somewhere in the binary a static array [1,2] but as it 
is assigned to a pointer to mutable data, the compiler has no 
choice as to allocate a mutable copy of that immutable array.


and it cannot optimize it away because it doesn't know what the 
caller want to do with it. It might in another module invoke it 
and modify it, the compiler cannot tell. auto a=f(); a[0]++;


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Patrick Schluter via Digitalmars-d-learn

On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote:

immutable(int[]) f() @nogc {
return [1,2];
}

onlineapp.d(2): Error: array literal in `@nogc` function 
`onlineapp.f` may cause a GC allocation


This makes dynamic array literals unusable with @nogc, and adds 
to GC pressure for no reason. What code would break if dmd used 
only static data for [1,2]?


int[] in D is not an array but a fat pointer. When one realizes 
that then it become quite obvious why [1,2] was allocated. There 
is somewhere in the binary a static array [1,2] but as it is 
assigned to a pointer to mutable data, the compiler has no choice 
as to allocate a mutable copy of that immutable array.


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, July 5, 2019 10:25:10 AM MDT Nick Treleaven via Digitalmars-d-
learn wrote:
> On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote:
> > static immutable arr = [1, 2];
> >
> > You have to spell it out that the data is static.
>
> Yes, I was wondering why the compiler doesn't statically allocate
> it automatically as an optimization.

It would have to be set up to store the literal somewhere else. Certainly,
it can't just put it on the stack, because that risks it going out of scope
and causing memory problems. It could theoretically be done with some types,
but it's more than simply optimizing the code. As it stands, AFAIK, string
literals are the only case where you avoid such allocations, and they get
put in a particular place in memory for that to work. Something similar
would have to be done with any other array literals where allocation was
being avoided (save maybe for a case where scope is used and, and the
compiler can statically verify that if it put it on the stack, it wouldn't
escape the scope). And if I understand correctly what's being done with
string literals, it requires that the value be known at compile time, in
which case, any array literals with variables or function calls or the like
couldn't work that way. Ultimately though, I think that what it comes down
to is that rather than the compiler figuring out which array literals it can
treat as special, it simply just knows that it can treat string literals
that way and does it with them and nothing else.

Another thing to consider is that optimizations shouldn't affect the
semantics. So, no matter what optimizations the compiler does with array
literals, that shouldn't affect whether the function can be @nogc. For it to
affect that, it would have to be something that was guaranteed by the
language's semantics regardless of whether any optimizations were being
done. So, even if an advanced optimizer really did figure out how to avoid
the GC allocations, that wouldn't help with @nogc. Rather, it would have to
be built into the semantics of the language.

- Jonathan M Davis





Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread dwdv via Digitalmars-d-learn

On 7/5/19 9:56 PM, ag0aep6g via Digitalmars-d-learn wrote:

On 05.07.19 20:49, Samir wrote:
As a follow-on to my earlier question, is there a way to pass a 
variable to the `map` function that specifies the column, rather than 
hard-coding it?  I'm thinking of something like:


p.map!("a[column]").maxElement.writeln;


You can't do that with the string style, because the string is turned 
into a function inside of `map`. Your `column` isn't visible there.


It works when you pass an actual callable instead, e.g. a lambda:

p.map!(a => a[column]).maxElement.writeln;


Furthermore, Samir, the parameter `a` can be renamed to whatever you 
prefer or what fits the code at hand best, e.g. `(row => row[column])`, 
as opposed to the string version, where only a small set of mostly 
single character names is supported.


Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread dwdv via Digitalmars-d-learn

On 7/5/19 9:56 PM, ag0aep6g via Digitalmars-d-learn wrote:

On 05.07.19 20:49, Samir wrote:
As a follow-on to my earlier question, is there a way to pass a 
variable to the `map` function that specifies the column, rather than 
hard-coding it?  I'm thinking of something like:


p.map!("a[column]").maxElement.writeln;


You can't do that with the string style, because the string is turned 
into a function inside of `map`. Your `column` isn't visible there.


It works when you pass an actual callable instead, e.g. a lambda:

p.map!(a => a[column]).maxElement.writeln;


Furthermore, Samir, the parameter `a` can be renamed to whatever you 
prefer or what fits the code at hand best, e.g. `(row => row[column])`, 
as opposed to the string version, where only a small set of mostly 
single character names is supported.


Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread ag0aep6g via Digitalmars-d-learn

On 05.07.19 20:49, Samir wrote:
As a follow-on to my earlier question, is there a way to pass a variable 
to the `map` function that specifies the column, rather than hard-coding 
it?  I'm thinking of something like:


p.map!("a[column]").maxElement.writeln;


You can't do that with the string style, because the string is turned 
into a function inside of `map`. Your `column` isn't visible there.


It works when you pass an actual callable instead, e.g. a lambda:

p.map!(a => a[column]).maxElement.writeln;


Re: Files as buffered InputRange

2019-07-05 Thread berni via Digitalmars-d-learn

On Friday, 5 July 2019 at 18:45:01 UTC, Les De Ridder wrote:

On Friday, 5 July 2019 at 18:29:36 UTC, berni wrote:

On Friday, 5 July 2019 at 17:57:39 UTC, Les De Ridder wrote:

File.byChunk[1] should do the trick.

[1] https://dlang.org/library/std/stdio/file.by_chunk.html


Not sure, if this is, what I'm looking for. I'd like to do 
something like



buffered_file.map!(a=>2*a).writeln();


When I understand it right, with byChunk I'll have to take 
care about the end of the buffer, myself...


You could use `joiner` from std.algorithm, e.g.
buffered_file.byChunk(4096).joiner.map!(a => 2 * a).writeln;


That's it. Thanks!


Re: Files as buffered InputRange

2019-07-05 Thread Les De Ridder via Digitalmars-d-learn

On Friday, 5 July 2019 at 18:29:36 UTC, berni wrote:

On Friday, 5 July 2019 at 17:57:39 UTC, Les De Ridder wrote:

File.byChunk[1] should do the trick.

[1] https://dlang.org/library/std/stdio/file.by_chunk.html


Not sure, if this is, what I'm looking for. I'd like to do 
something like



buffered_file.map!(a=>2*a).writeln();


When I understand it right, with byChunk I'll have to take care 
about the end of the buffer, myself...


You could use `joiner` from std.algorithm, e.g.
buffered_file.byChunk(4096).joiner.map!(a => 2 * a).writeln;


Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread Samir via Digitalmars-d-learn

On Friday, 5 July 2019 at 00:54:15 UTC, Samir wrote:
Is there a cleaner way of finding the maximum value of say the 
third column in a multi-dimensional array than this?

int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]];
writeln([p[0][2], p[1][2], p[2][2]].max);

I've tried the following
writeln([0, 1, 2].map!(p[a][2]).max);

but get an "Error: undefined identifier a" error.


As a follow-on to my earlier question, is there a way to pass a 
variable to the `map` function that specifies the column, rather 
than hard-coding it?  I'm thinking of something like:


p.map!("a[column]").maxElement.writeln;

In the mean time, I am looking further into Ilya's mir-algorithm 
library.


Thanks
Samir


Re: Files as buffered InputRange

2019-07-05 Thread berni via Digitalmars-d-learn

On Friday, 5 July 2019 at 17:57:39 UTC, Les De Ridder wrote:

File.byChunk[1] should do the trick.

[1] https://dlang.org/library/std/stdio/file.by_chunk.html


Not sure, if this is, what I'm looking for. I'd like to do 
something like



buffered_file.map!(a=>2*a).writeln();


When I understand it right, with byChunk I'll have to take care 
about the end of the buffer, myself...


Re: Files as buffered InputRange

2019-07-05 Thread Les De Ridder via Digitalmars-d-learn

On Friday, 5 July 2019 at 17:29:26 UTC, berni wrote:
I'd like to process a (binary) file as a buffered InputRange 
but I havn't found anything yet.  Is there anything or do I 
have to write it on my own?


File.byChunk[1] should do the trick.

[1] https://dlang.org/library/std/stdio/file.by_chunk.html


Files as buffered InputRange

2019-07-05 Thread berni via Digitalmars-d-learn
I'd like to process a (binary) file as a buffered InputRange but 
I havn't found anything yet.  Is there anything or do I have to 
write it on my own?


Re: Blog Post #0050: MVC III - ComboBoxText, Add & Remove

2019-07-05 Thread Ron Tarrant via Digitalmars-d-learn

On Friday, 5 July 2019 at 13:52:40 UTC, matheus wrote:

On Friday, 5 July 2019 at 09:34:08 UTC, Ron Tarrant wrote:
Today is a bit of a milestone for the blog as the 50th regular 
post goes up. Also, the facelift is coming along nicely, the 
next phase of which should be ready to push by July 9th.



Maybe you should post this on the Announce group?

And on Reddit too.

Matheus.


Point taken, but I was going to wait until the next phase of the 
face lift is ready to go.


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Max Haughton via Digitalmars-d-learn

On Friday, 5 July 2019 at 16:25:10 UTC, Nick Treleaven wrote:

On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote:

static immutable arr = [1, 2];

You have to spell it out that the data is static.


Yes, I was wondering why the compiler doesn't statically 
allocate it automatically as an optimization.


LDC might be able to optimize it away but by default its heap 
allocated, I imagine for thread safety


Re: Why are immutable array literals heap allocated?

2019-07-05 Thread Nick Treleaven via Digitalmars-d-learn

On Thursday, 4 July 2019 at 11:06:36 UTC, Eugene Wissner wrote:

static immutable arr = [1, 2];

You have to spell it out that the data is static.


Yes, I was wondering why the compiler doesn't statically allocate 
it automatically as an optimization.


Re: To learn D

2019-07-05 Thread Samir via Digitalmars-d-learn

On Friday, 5 July 2019 at 13:56:18 UTC, Craig Dillabaugh wrote:
Ali's book is targeted at beginners (see link below).  I don't 
see why D wouldn't make a good first language.  If your 
objective is to learn D, then I don't think learning C or 
Python is going to be help that much.  Obviously if you know 
C/Python you can learn D more quickly, but I doubt the effort 
is worth it if D is the ultimate goal.


http://ddili.org/ders/d.en/index.html


I will second Craig's recommendation to spend some time going 
through Ali's book.  It strikes a good balance between being an 
introduction to programming in general, and to D in particular.  
While I have dabbled in half a dozen languages or so over the 
years, I find D to be a lot more accessible than many of the 
other languages I've tried.  Part of that comes do the language 
design (similarities to C and Python) but mostly to the helpful 
community you will find here.  Good luck!





Re: To learn D

2019-07-05 Thread Craig Dillabaugh via Digitalmars-d-learn

On Friday, 5 July 2019 at 12:00:15 UTC, Binarydepth wrote:
I've considering learning full D. I remembered that D is not 
recommended as a first language, So I read time ago.


So my question, is learning C and Python a good intro before 
learning D?


TY


Ali's book is targeted at beginners (see link below).  I don't 
see why D wouldn't make a good first language.  If your objective 
is to learn D, then I don't think learning C or Python is going 
to be help that much.  Obviously if you know C/Python you can 
learn D more quickly, but I doubt the effort is worth it if D is 
the ultimate goal.


http://ddili.org/ders/d.en/index.html


Re: Blog Post #0050: MVC III - ComboBoxText, Add & Remove

2019-07-05 Thread matheus via Digitalmars-d-learn

On Friday, 5 July 2019 at 09:34:08 UTC, Ron Tarrant wrote:
Today is a bit of a milestone for the blog as the 50th regular 
post goes up. Also, the facelift is coming along nicely, the 
next phase of which should be ready to push by July 9th.


And today's topic continues with the MVC series by 
demonstrating how to add and remove items from a ComboBoxText 
widget. You can read it here:


https://gtkdcoding.com/2019/07/05/0050-mvc-iii-comboboxtext-add-remove.html


Maybe you should post this on the Announce group?

And on Reddit too.

Matheus.


Re: Finding Max Value of Column in Multi-Dimesional Array

2019-07-05 Thread Samir via Digitalmars-d-learn

On Friday, 5 July 2019 at 01:41:38 UTC, 9il wrote:

You may want to take a look into mir-algorithm [1] library.
It contains ndsilce package [2] to work with multidimensional 
data.


Thanks for referring me to this library, Ilya.  I will have to 
check this out.  While it seems a bit more complicated for my 
particular use case, this definitely seems like something worth 
checking out for future use!


On Friday, 5 July 2019 at 03:02:29 UTC, Jordan Wilson wrote:

p.map!"a[2]".maxElement.writeln; // 5


Thank you, Jordan.  I think this is what I was looking for.  I am 
still struggling to wrap my head around the use of `map` but 
these examples really help.


Samir


Re: To learn D

2019-07-05 Thread Cym13 via Digitalmars-d-learn

On Friday, 5 July 2019 at 12:00:15 UTC, Binarydepth wrote:
I've considering learning full D. I remembered that D is not 
recommended as a first language, So I read time ago.


So my question, is learning C and Python a good intro before 
learning D?


TY


Both C and Python provide valuable and complementary experience 
no matter what you want to do. If your goal is specifically to 
learn D then I'd learn C up to structures. That way you'll have 
basic tools and vocabulary that you can reuse in D and you can 
learn the rest as you go.


The things that will be hard if you want to learn D directly:

- not as many examples and tutorials on the internet (although 
there are some very good ones)


- lots of concepts and vocabulary (always remember that you 
*don't* have to know every detail of the language, learn what you 
need to solve the problem at hand, one thing at a time)


- not as many libraries, which means that it can be harder to 
solve a problem that not many people have had yet (popular things 
like web applications will be alright)


I think D isn't that bad of a first language. Once you've passed 
the vocabulary barrier you'll get the benefit of having a 
language that'll fit most of your tasks from little scripting or 
web applications to low-level programming and big projects. You 
will be confronted to many concepts and ideas without having to 
learn a new language each time and this knowledge is useful even 
if you decide to use another language later on.


To learn D

2019-07-05 Thread Binarydepth via Digitalmars-d-learn
I've considering learning full D. I remembered that D is not 
recommended as a first language, So I read time ago.


So my question, is learning C and Python a good intro before 
learning D?


TY


Blog Post #0050: MVC III - ComboBoxText, Add & Remove

2019-07-05 Thread Ron Tarrant via Digitalmars-d-learn
Today is a bit of a milestone for the blog as the 50th regular 
post goes up. Also, the facelift is coming along nicely, the next 
phase of which should be ready to push by July 9th.


And today's topic continues with the MVC series by demonstrating 
how to add and remove items from a ComboBoxText widget. You can 
read it here:


https://gtkdcoding.com/2019/07/05/0050-mvc-iii-comboboxtext-add-remove.html