Re: Some comments on learning D using the tour

2020-01-16 Thread James Blachly via Digitalmars-d-learn

On 1/15/20 3:06 PM, mark wrote:

I am learning D for the first time.

While I wait for Mike Parker's "Learning D" book to arrive, I have 
started using the tour.


I really like the tour, especially the fact that you can run and tweak 
the code as well as read the explanations.


However, ...


Mark, thank you for your comments. It is always valuable to hear the 
perspective of someone approaching any problem or situation with a fresh 
set of eyes.




Historical Data

2020-01-16 Thread ish via Digitalmars-d-learn
Which site is good to find up to 3 years of historical data 
through CSV?




Re: confused about string and lambda args

2020-01-16 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jan 16, 2020 at 05:03:33PM +, mark via Digitalmars-d-learn wrote:
[...]
> auto wordCharCounts4 = words // I added this and it won't compile
>   .map!(a => a.count); // Error: no property count for type string
> writeln(wordCharCounts4);

You need to import std.algorithm to get `count`.


> I don't understand why both syntaxes work for .length but only the
> string form for .count?

Because .length is a property of strings, whereas .count is actually not
a string property, but a function that's being called via UFCS: Unified
Function Call Syntax, in which when the compiler sees something like:

obj.func(x, y, z);

but `obj` doesn't have a member named `func`, then it will try to
rewrite it into:

func(obj, x, y, z);

instead.


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose 
Bierce


Re: Information about the 'magic' field in object.Object class

2020-01-16 Thread kinke via Digitalmars-d-learn

On Thursday, 16 January 2020 at 15:28:06 UTC, realhet wrote:

Update:
- All of the child classes needed to be marked with extern(C++)
- static class members are not supported, only __gshared static.
- passing a delegate to a constructor of this class expects a 
(extern(C++) delegate) too.
- Internal compiler error: string[string] can not be mapped to 
C++


So extern(C++) is not good in the current case.


The 3 latter points can be trivially worked around via extern(D):

extern(C++) class C
{
extern(D):
static int tlsGlobal;
this(void delegate()) {}
void foo(string[string] aa) {}
}

void main()
{
C.tlsGlobal = 123;
auto c = new C(() {});
c.foo(null);
}

I will not do any synchronization, but I think the GC will 
crash upon releasing these objects.


Very likely.


Re: confused about string and lambda args

2020-01-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 16 January 2020 at 17:03:33 UTC, mark wrote:
auto wordCharCounts = words // I added this and it works 
fine

.map!"a.length";
writeln(wordCharCounts);


The string thing probably shouldn't be used anymore. I suggest 
you always use the => form instead.


The string thing is a legacy version that was before the language 
had =>.


I don't understand why both syntaxes work for .length but only 
the string form for .count?


It is because of imports.

So the string version passes the string to the library, which 
pastes it into some skeleton code and makes a function out of it.


It basically does:

string code = "import some_stuff; (a) { return " ~ 
your_string ~ "; }";

mixin(code);

Note it does this INSIDE the library.

It is that `import some_stuff;` that accounts for this 
difference. The string one pastes in some library imports so some 
functions are available. The => form does not.


Since the string one is inside the lib, it can NOT see your own 
functions from your module! But since the lib imports a few other 
library modules, it may be able to see things your module didn't 
import.


The better way to do it is to use your => format, but go ahead 
and import the necessary module.


I believe `count` is located in `import std.algorithm;`. So add 
that to your module and it should work now.


confused about string and lambda args

2020-01-16 Thread mark via Digitalmars-d-learn
I'm looking at 
https://tour.dlang.org/tour/en/gems/range-algorithms

(IMO the example code is far too long and complicated.)
But here's the thing:

auto wordCharCounts = words // I added this and it works fine
.map!"a.length";
writeln(wordCharCounts);

auto wordCharCounts2 = words // I added this and it works fine
.map!(a => a.length);
writeln(wordCharCounts2);

auto wordCharCounts3 = words // this is in the tutorial
  .map!"a.count";
writeln(wordCharCounts3);

auto wordCharCounts4 = words // I added this and it won't 
compile
  .map!(a => a.count); // Error: no property count for type 
string

writeln(wordCharCounts4);

I don't understand why both syntaxes work for .length but only 
the string form for .count?


Re: Information about the 'magic' field in object.Object class

2020-01-16 Thread realhet via Digitalmars-d-learn

On Thursday, 16 January 2020 at 14:32:24 UTC, Adam D. Ruppe wrote:

On Thursday, 16 January 2020 at 14:30:04 UTC, realhet wrote:

Is there a documentation about that 'magic' field?


I'm pretty sure the only fields in there are pointer to vtable 
and pointer to monitor object...


I have a really small object, only 32 bytes. At this point if 
I want to add a flag bit I have 3 choices:


Do you need virtual functions? If not, you could probably just 
make a struct instead.


Thank you both for the hints!

Yes I need virtual functions (this is the base class of my layout 
system: it can be a letter, a picture, or a paragraph of text.).


I've tried extern(C++) and it went down 8 bytes.

But the I tried the following:
synchronized(obj){ ... }
Compiles without a problem. Hmm...

I think I will use extern(C++) and later when I will have a big 
system to test, I will benchmark it. I'm sure that Monitor 
functionality is not needed for these objects, so that extra 8 
bytes will worth it.


Update:
- All of the child classes needed to be marked with extern(C++)
- static class members are not supported, only __gshared static.
- passing a delegate to a constructor of this class expects a 
(extern(C++) delegate) too.

- Internal compiler error: string[string] can not be mapped to C++

So extern(C++) is not good in the current case.

I will try to use that 8 byte magic illegally, and will see if it 
is unstable or not.
I will not do any synchronization, but I think the GC will crash 
upon releasing these objects.


Thx for the help!


Re: How to remove whitespace from a string

2020-01-16 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 16 January 2020 at 13:36:10 UTC, Namal wrote:
Hello, what is the way to remove whitespace from a string (not 
only at the beginning and end)..


import std.algorithm: filter;
import std.uni: isWhite;
import std.stdio: writeln;

void main()
{
string s = " hello world ! ";
writeln(s.filter!(c => !c.isWhite));
// prints: helloworld!
}


Re: sdl 2 - text is not displayed correctly

2020-01-16 Thread TodNaz via Digitalmars-d-learn

On Wednesday, 15 January 2020 at 16:28:58 UTC, drug wrote:

On 1/15/20 6:26 PM, TodNaz wrote:

Hello!
Maybe someone came across ... I use sdl 2 derelcit, and wanted 
to draw text (not English, but Russian, for example). And when 
called in TTF_RenderUTF8_Blended, the text is drawn with 
incorrect characters (with these: ""). And with wstribg and 
TTF_RendererUNICODE_Blended, the surface has the wrong size. 
Maybe someone knows what to do? Thanks in advance!


[Sorry for the bad English, the translator helped.]


Probably the reason is wrong font. Are you sure your font 
contains Cyrillic symbols? Try the following fonts: 
https://github.com/Immediate-Mode-UI/Nuklear/tree/master/extra_font


IIRC, DroidSans.ttf contains Cyrillic symbols, probably others 
too


You were right. I downloaded another font with the Cyrillic 
alphabet, and the problem is solved. thanks!


Re: Information about the 'magic' field in object.Object class

2020-01-16 Thread Petar via Digitalmars-d-learn

On Thursday, 16 January 2020 at 14:32:24 UTC, Adam D. Ruppe wrote:

On Thursday, 16 January 2020 at 14:30:04 UTC, realhet wrote:

Is there a documentation about that 'magic' field?


I'm pretty sure the only fields in there are pointer to vtable 
and pointer to monitor object...


I have a really small object, only 32 bytes. At this point if 
I want to add a flag bit I have 3 choices:


Do you need virtual functions? If not, you could probably just 
make a struct instead.


Alternatively, the class can be marked as extern(C++).


Re: Information about the 'magic' field in object.Object class

2020-01-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 16 January 2020 at 14:30:04 UTC, realhet wrote:

Is there a documentation about that 'magic' field?


I'm pretty sure the only fields in there are pointer to vtable 
and pointer to monitor object...


I have a really small object, only 32 bytes. At this point if I 
want to add a flag bit I have 3 choices:


Do you need virtual functions? If not, you could probably just 
make a struct instead.


Information about the 'magic' field in object.Object class

2020-01-16 Thread realhet via Digitalmars-d-learn

Hello,
I'm try to figure out the contents od the base class instance in 
D, LDC Windows 64bit.

I'm sure that there is a 8 byte VMT pointer.
But unable to fund information about the remaining 8 byte. I 
guess it must be a unique 8byte (void* ?) identifier that is 
useful for Monitor. In a video someone mentioned this as 'magic'.


Is there a documentation about that 'magic' field? I'd love to 
use some bits in it if I'm sure about the consequences.


I have a really small object, only 32 bytes. At this point if I 
want to add a flag bit I have 3 choices:
- add a new field to the class -> effectively the instanceSize 
will grow by 50% (16bytes with alignment)

- compress the current 16bytes of data. -> eats more cpu
- Hide it somewhere 'magically'.

Thank You in advance!




How to remove whitespace from a string

2020-01-16 Thread Namal via Digitalmars-d-learn
Hello, what is the way to remove whitespace from a string (not 
only at the beginning and end)..


sdl 2 - text is not displayed correctly

2020-01-16 Thread TodNaz via Digitalmars-d-learn

Maybe I made a mistake? Sorry, I'm new to this business ...

[https://pastebin.com/Yyzg4iZf]


Re: sdl 2 - text is not displayed correctly

2020-01-16 Thread TodNaz via Digitalmars-d-learn

On Wednesday, 15 January 2020 at 16:28:58 UTC, drug wrote:

On 1/15/20 6:26 PM, TodNaz wrote:

Hello!
Maybe someone came across ... I use sdl 2 derelcit, and wanted 
to draw text (not English, but Russian, for example). And when 
called in TTF_RenderUTF8_Blended, the text is drawn with 
incorrect characters (with these: ""). And with wstribg and 
TTF_RendererUNICODE_Blended, the surface has the wrong size. 
Maybe someone knows what to do? Thanks in advance!


[Sorry for the bad English, the translator helped.]


Probably the reason is wrong font. Are you sure your font 
contains Cyrillic symbols? Try the following fonts: 
https://github.com/Immediate-Mode-UI/Nuklear/tree/master/extra_font


IIRC, DroidSans.ttf contains Cyrillic symbols, probably others 
too


Thanks for answering. I forgot to mention, the characters are 
displayed, and the last one that displayed the character becomes 
invalid, like: . Fonts have nothing to do with it. Any ideas?


Re: Reading a file of words line by line

2020-01-16 Thread mark via Digitalmars-d-learn

On Thursday, 16 January 2020 at 10:10:02 UTC, dwdv wrote:
On 2020-01-16 04:54, Jesse Phillips via Digitalmars-d-learn 
wrote:

[...]

[...]

isn't far off, but could also be (sans imports):

return File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.map!(word => word.to!string.toUpper)
.assocArray(0.repeat);


That's what I'm now using -- thanks!
(Now I can try the next bit.)


Re: Some comments on learning D using the tour

2020-01-16 Thread mark via Digitalmars-d-learn

On Thursday, 16 January 2020 at 02:32:07 UTC, Adam D. Ruppe wrote:

On Wednesday, 15 January 2020 at 20:06:01 UTC, mark wrote:
For example, I haven't found one definitive place in the docs 
that document D's strings.


My unofficial docs are built from the same source, so not 
perfect, but at least have better cross linking:


http://dpldocs.info/experimental-docs/std.string.html

might be helpful to you.


Comparing
https://dlang.org/phobos/std_string.html vs
http://dpldocs.info/experimental-docs/std.string.html
I generally find yours easier to read but miss the examples that 
are in the std docs. And in both cases there is no coverage of 
(or cross-refs to coverage of) string iteration, string indexing, 
and byte, code point, and grapheme counting. Also yours doesn't 
have the search box.


I've now discovered the std.uni and std.utf modules. There really 
ought to be cross-refs to/from these and std.string and Array 
string.


Re: Some comments on learning D using the tour

2020-01-16 Thread mark via Digitalmars-d-learn

On Thursday, 16 January 2020 at 01:02:46 UTC, Mike Parker wrote:

On Wednesday, 15 January 2020 at 20:06:01 UTC, mark wrote:


However, what I really miss is a contents page so that I can 
look at each topic and jump back when I want to recap 
something.


Please submit an enhancement request:

https://github.com/dlang-tour/core/issues


Done: https://github.com/dlang-tour/core/issues/741

For example, I haven't found one definitive place in the docs 
that document D's strings.


https://dlang.org/spec/arrays.html#strings


I'd found that, as I mentioned, but it is unsatisfactory. For 
example, it says nothing about how to iterate the characters in a 
string or about the different kinds of string lengths (byte 
count, code point count, grapheme count).


Strings are important enough to deserve their own chapter in the 
online "D Programming Language". Such a chapter ought to have 
cross-refs to arrays and draw together _all_ the relevant string 
related info.


Re: Reading a file of words line by line

2020-01-16 Thread dwdv via Digitalmars-d-learn

On 2020-01-16 04:54, Jesse Phillips via Digitalmars-d-learn wrote:

[...]
.map!(word => word.to!string.toUpper)
.array
.sort
.uniq
.map!(x => tuple (x, 0))
.assocArray ;



.each!(word => words[word.to!string.toUpper] = 0);

isn't far off, but could also be (sans imports):

return File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.map!(word => word.to!string.toUpper)
.assocArray(0.repeat);


Re: Get memory used by current process at specific point in time

2020-01-16 Thread Boris Carvajal via Digitalmars-d-learn

On Sunday, 12 January 2020 at 13:58:18 UTC, Per Nordlöw wrote:
Is there a druntime/phobos function for getting the amount of 
memory (both, stack, malloc, and GC) being used by the current 
process?


At least for the GC I remember using GC.stats and GC.profileStats 
to get some info.


https://dlang.org/phobos/core_memory.html#.GC