Re: Reading a file of words line by line

2020-01-15 Thread Jesse Phillips via Digitalmars-d-learn

On Wednesday, 15 January 2020 at 19:50:31 UTC, mark wrote:
I really do need a set for the next part of the program, but 
taking your code and ideas I have now reduced the function to 
this:


WordSet getWords(string filename, int wordsize) {
WordSet words;
File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.each!(word => words[word.to!string.toUpper] = 0);
return words;
}

This is also 4x faster than my version that used a regex -- 
thanks!


Why did you use string.count rather than string.length?


Your solution is fine, but also



void main () {

auto file = ["word one", "my word", "word"] ;
writeln (uniqueWords(file, 4));
}

auto uniqueWords(string[] file, uint wordsize) {
import std.algorithm, std.array, std.conv, std.functional, 
std.uni;


return file
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.map!(word => word.to!string.toUpper)
.array
.sort
.uniq
.map!(x => tuple (x, 0))
.assocArray ;
}




Re: Some comments on learning D using the tour

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

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.


Re: Some comments on learning D using the tour

2020-01-15 Thread Mike Parker via Digitalmars-d-learn

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



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


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


Re: Reading a file of words line by line

2020-01-15 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 15, 2020 at 07:50:31PM +, mark via Digitalmars-d-learn wrote:
[...]
> Why did you use string.count rather than string.length?

The .length of a `string` type is the number of bytes that it occupies,
which is not necessarily the same thing as the number of characters in
the string. E.g., if you receive a Unicode string, there may be
multi-byte characters in it.


T

-- 
A computer doesn't mind if its programs are put to purposes that don't match 
their names. -- D. Knuth


Some comments on learning D using the tour

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

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, 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.


For example, I haven't found one definitive place in the docs 
that document D's strings. I've found the std.string module's 
docs and the Array's string docs -- which unfortunately are not 
cross-ref'd with links to each other. I know that I've covered 
strings in the tour, but with no list of contents I can't find it 
without "endlessly" clicking back to find it.


Another thing I like about the tour is that the imports specify 
the imported functions so you can see exactly where they come 
from.





Re: Reading a file of words line by line

2020-01-15 Thread mark via Digitalmars-d-learn
I really do need a set for the next part of the program, but 
taking your code and ideas I have now reduced the function to 
this:


WordSet getWords(string filename, int wordsize) {
WordSet words;
File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.each!(word => words[word.to!string.toUpper] = 0);
return words;
}

This is also 4x faster than my version that used a regex -- 
thanks!


Why did you use string.count rather than string.length?



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

2020-01-15 Thread Jacob Carlborg via Digitalmars-d-learn

On 2020-01-15 19:45, Chris Katko wrote:

Still, some internal instrumentation would be nice. But I'm not aware of 
any.


There are some runtime flags that can be passed to the executable when 
running it to get some info about the GC. Of course these are not 
documented (except perhaps in the changelog). I'm too lazy to try to 
find them now.


--
/Jacob Carlborg


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

2020-01-15 Thread Chris Katko 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?


Valgrind will tell you the exact usage (except no delineation for 
GC) per here:


https://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process

Valgrind is an overall extremely useful tool for Linux devs.

Still, some internal instrumentation would be nice. But I'm not 
aware of any.





Re: Reading a file of words line by line

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

On 2020-01-15 16:34, mark via Digitalmars-d-learn wrote:

Is this as compact as it _reasonably_ can be?


How about this?

auto uniqueWords(string filename, uint wordsize) {
import std.algorithm, std.array, std.conv, std.functional, std.uni;

return File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.map!(word => word.to!string.toUpper)
.array
.sort
.uniq;
}


Re: How to create meson.build with external libs?

2020-01-15 Thread 9il via Digitalmars-d-learn

On Sunday, 12 January 2020 at 22:00:33 UTC, p.shkadzko wrote:
Ok, I am trying to meson and is struggling with meson.build 
file. I looked up the examples page: 
https://github.com/mesonbuild/meson/tree/master/test%20cases/d 
which has a lot of examples but not the one that shows you how 
to build your project with some external dependency :)


Let's say we have a simple dir "myproj" with "meson.build" in 
it and some source files like "app.d" and "helper_functions.d".


~/myproj
   app.d
   helper_functions.d
   meson.build

"helper_functions.d" uses let's say lubeck library which 
according to 
https://forum.dlang.org/thread/nghoprwkihazjikyh...@forum.dlang.org is supported by meson.


Here is my meson.build:
---
project('demo', 'd',
  version : '0.1',
  default_options : ['warning_level=3']
  )

lubeck = dependency('lubeck', version: '>=1.1.7')
ed = executable('mir_quickstart', 'app.d', dependencies: 
lubeck, install : true)



However, when I try to build it I get the following error:
-
$ meson build
The Meson build system
Version: 0.52.1
Source dir: /home/user/dev/github/demo
Build dir: /home/user/dev/github/demo/build
Build type: native build
Project name: demo
Project version: 0.1
D compiler for the host machine: ldc2 (llvm 1.18.0 "LDC - the 
LLVM D compiler (1.18.0):")

D linker for the host machine: GNU ld.gold 2.33.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Found pkg-config: /usr/bin/pkg-config (1.6.3)
Found CMake: /usr/bin/cmake (3.16.2)
Run-time dependency lubeck found: NO (tried pkgconfig and cmake)

meson.build:8:0: ERROR: Dependency "lubeck" not found, tried 
pkgconfig and cmake


A full log can be found at 
/home/user/dev/github/demo/build/meson-l

-

What do I need to do in order to build the project with 
"lubeck" dependency in meson?


Seems like you have missed the *.wrap files in subprojects 
folder. It's a bad idea to install D meson libs into system as 
probably you will want to control versions easily.


You need all *wrap files recursively.
Check this 
https://github.com/kaleidicassociates/lubeck/tree/master/subprojects

and add a wrap file for Lubeck.
You can specify tags instead of the master branches. - it is 
recommended for stable work.


Ilya


Re: Practical parallelization of D compilation

2020-01-15 Thread Guillaume Lathoud via Digitalmars-d-learn

Concerning the first (fresh) compilation:

On Wednesday, 8 January 2020 at 13:14:38 UTC, kinke wrote:
[...] you can also try to compile all modules at once, but 
output separate object files - `ldc2 -c a.d b.d c.d`.


I just had another try at `ldc2 -c ...`. It does work when 
grouping the files in chunks of say 50.


So now I could parallelize the first (fresh) compilation using 
that chunk approach, and this leads to compilation times 
comparable to, or even faster than the single-process approach 
`ldmd2 -i main.d`.


real time:
 * 4-core: 57 seconds with chunks instead of 52 seconds with the 
single-process approach
 * 8-core: 23 seconds with chunks instead of 33 seconds with the 
single-process approach


So the drawback of this approach has pretty much disappeared, at 
least on my 200 files :)

Thanks a lot for all the feedback!

Guillaume



Re: sdl 2 - text is not displayed correctly

2020-01-15 Thread drug via Digitalmars-d-learn

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


Re: range algorithms on container class

2020-01-15 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 9 January 2020 at 10:26:07 UTC, Jonathan M Davis 
wrote:
On Wednesday, January 8, 2020 10:56:20 PM MST rikki cattermole 
via Digitalmars-d-learn wrote:
Slicing via the opSlice operator overload is a convention, not 
a requirement.


It's not a requirement, but it's more than a convention. If you 
use the container with foreach, the compiler will call opSlice 
on the container to get a range. So, there's no need to 
implement opApply to iterate over a container with foreach. You 
could choose to implement a container with opApply and use a 
function other than opSlice for getting a range, but the 
compiler understands enough to try to slice the container for 
you automatically when using it with foreach.


- Jonathan M Davis


Is this documented in the language spec anywhere? I don't see 
anything about it in the section on foreach [1] or the section on 
slice operator overloading [2].


[1] https://dlang.org/spec/statement.html#foreach-statement
[2] https://dlang.org/spec/operatoroverloading.html#slice


Re: Reading a file of words line by line

2020-01-15 Thread mark via Digitalmars-d-learn
Thanks for the ideas, I've now reduced the size of the getWords() 
function (even allowing for moving the imports to the top of the 
file) to this:


WordSet getWords(string filename, int wordsize) {
string bareWord(string line) {
auto rx = ctRegex!(r"^([a-z]+)", "i");
auto match = matchFirst(line, rx);
return match.empty ? "" : match.hit.to!string;
}
WordSet words;
slurp!string(filename, "%s")
.map!(line => bareWord(line))
.filter!(word => word.length == wordsize)
.each!(word => words[word.toUpper] = 0);
return words;
}

Is this as compact as it _reasonably_ can be?


sdl 2 - text is not displayed correctly

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

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.]


How to use -profile when I initialize D-runtime myself?

2020-01-15 Thread Robert M. Münch via Digitalmars-d-learn

Hi, I had a related question in the "Does -profile need the D runtime?" thread.

Since D runtime is required for profile to work, the question is how 
can I use profile when initializing it myself?


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: Invalid memory operation during allocation with `new`

2020-01-15 Thread Per Nordlöw via Digitalmars-d-learn

On Monday, 13 January 2020 at 23:06:01 UTC, Per Nordlöw wrote:
Unfortunately that didn't tell me the cause of the problem but 
only the stack trace of the failing call to `new` which is not 
called inside a destructor.


I found the problem. I was greedly using the `__monitor__` slot 
of some classes in order to get one extra word of class storage. 
Setting it to `null` in those classes' destructors made the 
memory error go away. Thanks anyway for valuable input.


Re: range algorithms on container class

2020-01-15 Thread Ali Çehreli via Digitalmars-d-learn
On 1/14/20 8:15 PM, Alex Burton wrote:> On Thursday, 9 January 2020 at 
10:26:07 UTC, Jonathan M Davis wrote:

>> On Wednesday, January 8, 2020 10:56:20 PM MST rikki cattermole via
>> Digitalmars-d-learn wrote:
>>> Slicing via the opSlice operator overload is a convention, not a
>>> requirement.
>>
>> It's not a requirement, but it's more than a convention. If you use
>> the container with foreach, the compiler will call opSlice on the
>> container to get a range. So, there's no need to implement opApply to
>> iterate over a container with foreach. You could choose to implement a
>> container with opApply and use a function other than opSlice for
>> getting a range, but the compiler understands enough to try to slice
>> the container for you automatically when using it with foreach.
>>
>> - Jonathan M Davis
>
> Implementing opApply allowed me to use foreach on the container.

However, that method does not allow you to have more than one iteration 
on the same container. Once opApply is running, the state of iteration 
is in function call stack. (opApply is calling out to the body of the 
foreach loop until the loop is over.)


In contrast, you can have multiple range objects over the same container:

  auto a = cont[];
  auto b = cont[];

Now the state of iteration are independently handled by a and b.

There is only one case that comes to mind where opApply is superior: 
When iteration involves recursion like tree traversal, then the way 
opApply takes advantage of function call stack is vastly easier (to me 
at least :) ). Even in that case, one can use fibers but it is a little 
bit more complication over opApply.


> I would expect that returning a slice would not be logically possible
> for many container types.

That is true if you're thinking about a slice as a RandomAccessRange, 
which is the case for arrays. However, if we think of slicing as 
"returning a range object over all elements" (i.e. an InputRange 
suffices), then it fits all containers.


> A slice cannot be a range either, otherwise
> you would need to call the array algorithm to assign a slice of an array
> to another array.

Part of this confusion (for me as well) is due to D's conflation of 
slices with dynamic arrays. When we see the slice as the range and think 
of the memory location as the sometimes anonymous container, then slice 
assignment may be valid for user-defined containers. The name of the 
operator is not clear (opIndexAssign):


  https://dlang.org/spec/operatoroverloading.html#slice_assignment_operator

I have two sections that show examples of its single-dimensional and 
multi-dimensional uses:



http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opIndexAssign

The multi-dimensional use case shows how a container and its range types 
are different types:



http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.opIndexAssign%20template

That example was difficult for me to understand. :)

> So the compiler must be creating a forward iterating range to pass into
> the range algorithms in these cases.

Exactly and should do the same with user-defined types.

> That foreach, and range algorithms can work on a native array but only
> foreach can work on custom container type looks like a gap in the 
language.


I think there are some corner cases but not for most (all?) cases.

Ali