Re: Why do we have Dmain?

2021-10-22 Thread Kirill via Digitalmars-d-learn

On Friday, 22 October 2021 at 07:00:25 UTC, Mike Parker wrote:

[...]


Thank you for such a clear explanation Mike and for a quick reply!




Why do we have Dmain?

2021-10-21 Thread Kirill via Digitalmars-d-learn
I am not a compiler expert, but I genuinely would like to know 
why we have Dmain.


I've been looking at the generated assembly code recently and 
noticed the _Dmain function. I didn't notice it before. Then 
there is main, where Dmain is called.


Why is that?


Extract base type of any array?

2021-09-18 Thread Kirill via Digitalmars-d-learn
How can I get the base type of any 
(multidimensional/static/dynamic/associative) array?


Example:
```
void main() {
int[][] intArr;
double[4][] doubleArr;
string[string][] strArr;

intArr.example; // T = int
doubleArr.example; // T = double
strArr.example; // T = string
}

void example(T)(<...> data) {
// extract the base of data (inside func's body or in <...>)
// preferably, T must become data's base type
}
```


Re: A way to mixin during runtime?

2021-08-27 Thread Kirill via Digitalmars-d-learn

On Friday, 27 August 2021 at 09:51:46 UTC, Mathias LANG wrote:

On Friday, 27 August 2021 at 06:52:10 UTC, Kirill wrote:

Is there a way to do mixin or similar during runtime?

I'm trying to read a csv file and extract data types. Any 
ideas on how this should be approached in D are greatly 
appreciated.


You cannot mixin at runtime. However, it is fairly easy to map 
a finite and CT-know set of argument to runtime arguments via 
`static foreach`.
Could you give us example of the content of your CSV file and 
what you are trying to do ?


Each csv file will be different.

For example:
```
name;surname;age;grade
Alex;Wong;18;87
John;Doe;19;65
Alice;Doe;18;73
etc...
```

I'd like to extract the data types automatically. For instance, 
if using tuples:

```
Tuple!(string, string, int, int) ...
```
instead I'd like to have:
```
auto mytuple = read_csv(path); // returns Tuple!(string, string, 
int, int)[]

```


A way to mixin during runtime?

2021-08-27 Thread Kirill via Digitalmars-d-learn

Is there a way to do mixin or similar during runtime?

I'm trying to read a csv file and extract data types. Any ideas 
on how this should be approached in D are greatly appreciated.


Re: How do I check if a variable is a multidimensional (2D) array?

2021-07-11 Thread Kirill via Digitalmars-d-learn

On Monday, 12 July 2021 at 05:08:29 UTC, jfondren wrote:

On Monday, 12 July 2021 at 04:25:00 UTC, Kirill wrote:
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can 
help me check if I have a multidimensional array. Is there 
any? How do I create my own?


Thanks in advance.


from https://github.com/PhilippeSigaud/D-templates-tutorial

```d
template rank(T) {
static if (is(T t == U[], U))
enum size_t rank = 1 + rank!(U);
else
enum size_t rank = 0;
}

unittest {
int a;
int[] b;
int[][] c;
assert(rank!(typeof(a)) == 0);
assert(rank!(typeof(b)) == 1);
assert(rank!(typeof(c)) == 2);
}
```

as an example of a recursive template. There's also an 
implementation for ranges.


Thanks!


How do I check if a variable is a multidimensional (2D) array?

2021-07-11 Thread Kirill via Digitalmars-d-learn
I know there is isArray!T and similar functionality in 
std.traits. But I couldn't find the functionality that can help 
me check if I have a multidimensional array. Is there any? How do 
I create my own?


Thanks in advance.


Printing Tuple!(...)[] using for loop?

2021-07-01 Thread Kirill via Digitalmars-d-learn
I have a `Tuple!(string, ..., string)[] data` that I would like 
to print out:

`a   b   c`
`1   2   3`
`4   5   6`

Furthermore, I want to be able to print any N rows and M 
columns of that table. For instance:

`b   c`
`2   3`
or
`1   2   3`
`4   5   6`

I am using for loops for that:

// inside some function(rows, cols):
immutable startFromRow = ...;
immutable endWithRow = ...;
immutable startFromCol = ...;
immutable endWithCol = ...;

// print data
for(size_t i = startFromRow; i < endWithRow; i++) {
for(size_t j = startFromCol; j < endWithCol; j++) {
writef("%s", data[i][j]);
}
writef("\n");
}

And the compiler puts out the following:
`Error: variable 'j' cannot be read at compile time`

I tried `data[i].expand[j]`, but the same error occurs anyway.

I have two questions:
1. How do I print the contents of a Tuple using for loops? Or any 
other method?

2. What am I missing? How does it actually work under the hood?

Thanks in advance. Any help is greatly appreciated.




Re: Parallel foreach iteration with Associative Arrays

2021-04-16 Thread Kirill via Digitalmars-d-learn

On Saturday, 17 April 2021 at 02:14:50 UTC, Paul Backus wrote:
`parallel` requires a range [1], and an associative array is 
not a range. To get a range of an AA's keys and values, you can 
use the method `.byKeyValue`:


foreach (pair; parallel(example.byKeyValue)) {
writeln(pair.key, ": ", pair.value);
}

If you're confused about what a "range" is, the short answer is 
that it's kind of like an iterator. For the long answer, check 
out Andrei Alexandrescu's article "On Iteration" [2], or the 
"Ranges" chapter of Ali Çehreli's "Programming in D" [3].


[1] 
https://phobos.dpldocs.info/std.parallelism.TaskPool.parallel.2.html

[2] https://www.informit.com/articles/printerfriendly/1407357
[3] http://ddili.org/ders/d.en/ranges.html



That worked! Thanks you!


Parallel foreach iteration with Associative Arrays

2021-04-16 Thread Kirill via Digitalmars-d-learn
I'd like to iterate over an associative array and output it's key 
and value using parallel from std.parallelism.


But I get an error message: ParallelForeach!(int[string]) error 
instantiating.


My code:

auto example = ["apples": 100, "orange": 250, "banana": 175];
foreach(key, value; parallel(example)) { writeln(key, ": ", 
value); }


What am I doing wrong?

Thanks in advance.


Re: Tool to measure the time a function takes to execute?

2020-12-27 Thread Kirill via Digitalmars-d-learn

On Monday, 28 December 2020 at 07:00:59 UTC, Ali Çehreli wrote:

On 12/27/20 10:24 PM, Kirill wrote:
Hello, is there a tool to measure the execution time of a 
function in D? Can the GC do it?


StopWatch and benchmark():

  https://dlang.org/phobos/std_datetime.html

Ali


Thanks Ali! That's exactly what I needed.


Tool to measure the time a function takes to execute?

2020-12-27 Thread Kirill via Digitalmars-d-learn
Hello, is there a tool to measure the execution time of a 
function in D? Can the GC do it?


Re: How do I statically build a project using DUB?

2020-08-29 Thread Kirill via Digitalmars-d-learn

On Saturday, 29 August 2020 at 12:06:38 UTC, Andre Pany wrote:

On Saturday, 29 August 2020 at 11:27:28 UTC, Kirill wrote:
I need a stand-alone executable that does not require the user 
to install any libraries on their computer. Everything should 
be packed into the executable.


I understand that I need to statically link all of the 
libraries I use in my project, but how do I do this? What do I 
need to add to dub.json file? What compiler flags do I need to 
use?


Thanks in advance.


This highly depends on your actual project:

- which 3rd party dependencies do you have
- are these dependencies d source code only or wrapping C 
libraries
- is the c source code of your dependencies available and you 
are able to create a static library of them

- does the license of the c dependencies allows static linking

Kind regards
Andre


I am trying to link the GTK library. I have the GTK Runtime 
installed on Windows 10 pc.


Do I need to build the static GTK library from the source? Then 
how do I tell DUB to include that library?


How do I statically build a project using DUB?

2020-08-29 Thread Kirill via Digitalmars-d-learn
I need a stand-alone executable that does not require the user to 
install any libraries on their computer. Everything should be 
packed into the executable.


I understand that I need to statically link all of the libraries 
I use in my project, but how do I do this? What do I need to add 
to dub.json file? What compiler flags do I need to use?


Thanks in advance.



Re: Translating C headers to D: How do I compile it?

2020-06-28 Thread Kirill via Digitalmars-d-learn

On Sunday, 28 June 2020 at 05:13:32 UTC, Mike Parker wrote:

On Sunday, 28 June 2020 at 04:59:12 UTC, Kirill wrote:


something.d:
module something;
int add(int a, int b);


This should be extern(C) int add(int a, int b). The extern(C) 
tells the D compiler to use the standard C calling convention 
when calling that function.




Thanks! It all works now! 'extern(C)' is what was missing.


Translating C headers to D: How do I compile it?

2020-06-27 Thread Kirill via Digitalmars-d-learn
Hello. I am learning how to translate C headers to D. But how do 
I compile it? I am stuck with this.


I have 4 files in the same directory: main.d, something.d, 
some.h, some.c


some.h:
//header guards
int add(int a, int b);

some.c:
#include "some.h"
int add(int a, int b) { return a+b; }

something.d:
module something;
int add(int a, int b);

main.d:
import std.stdio: writeln;
import something;

void main() { writeln("5+7=", add(5, 7); }

How do I compile this correctly?

Thank you for your time.




Re: Why are class variables public, when marked by the 'private' keyword?

2020-03-20 Thread Kirill via Digitalmars-d-learn

On Saturday, 21 March 2020 at 04:58:32 UTC, Mike Parker wrote:
In D, the unit of encapsulation is the module. So private means 
"private to the module", i.e., private members are accessible 
within the same module. If ID were in a different module from 
main, you would see an error.


Indeed, I read something like this somewhere... It makes sense to 
me now! Thank you!


Why are class variables public, when marked by the 'private' keyword?

2020-03-20 Thread Kirill via Digitalmars-d-learn
I was playing around with visibility attributes in D. I created a 
class with private variables. Then I tried to access those 
variables through the class object. It compiled without any 
errors. However, ...


Shouldn't the compiler output an error for trying to access 
private members of a class? Do I get something wrong?


Here is the code:

import std.stdio;

class ID {
public:
 int id = 3849493;
private:
 string name = "Julia";
 int age = 17;
};

void main() {
 ID p = new ID();

 writeln(p.name, " ", p.age, " ", p.id);
}





Re: practicality of empirical cache optimization in D vs C++

2014-11-10 Thread Kirill via Digitalmars-d-learn
I would also be curious to see projects in D that involved cache 
optimization.


practicality of empirical cache optimization in D vs C++

2014-11-10 Thread Kirill via Digitalmars-d-learn

Dear D community (and specifically experts on cache optimization),

I'm a C++ programmer and was waiting for a while to do a project 
in D.


I'd like to build a cache-optimized decision tree forest library, 
and I'm debating between D and C++. I'd like to make it similar 
to atlas, spiral, or other libraries that partially use static 
optimization with recompilation and meta-programming to cache 
optimize the code for a specific architecture (specifically the 
latest xeons / xeon phi). Given D's compile speed and 
meta-programming, it should be a good fit. The problem that I 
might encounter is that C++ has a lot more information on the 
topic, which might be significant bottleneck given I'm just 
learning cache optimization (from a few papers and what every 
programmer should know about memory).


From my understanding, cache optimization mostly involves 
breaking data and loops into segments that fit in cache, and 
making sure that commonly used variables (for example sum in 
sum+=i) stay in cache. Most of this should be solved by 
statically defining sizes and paddings of blocks to be used for 
caching. It's more related to low level -- C, from my 
understanding. Are there any hidden stones?


The other question is how mature is the compiler in terms of 
optimizing for cache comparing to C++? I think gnu C++ does a few 
tricks to optimize for cache and there are ways to tweak cache 
line alignment.


My knowledge on the subject is not yet concrete and limited but I 
hope this gave an idea of what I'm looking for and you can 
recommend me a good direction to take.


Best regards,
--Kirill