Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 17 January 2022 at 03:11:50 UTC, Steven Schveighoffer wrote: The profile=gc appears to only show GC allocations that the *compiler* initiates (i.e. via `new`, array operations (like appending) or closure allocations). It does not detect that the functions that actually allocate

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 17 January 2022 at 00:54:19 UTC, forkit wrote: .. module test; import std; @safe void main() { // mArr1 is a dynamic array allocated on the gc heap. int[][] mArr1 = [[1, 2], [3, 4], [5, 6], [7, 8]]; static assert(is(typeof(mArr1.array(; alias R1 =

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 23:34:41 UTC, Ali Çehreli wrote: Definitely a -profile=gc bug. Here are the existing ones: https://issues.dlang.org/buglist.cgi?quicksearch=profile%20gc Ali yeah, a bug makes more sense ... otherwise I really would have had a slice to data that doesn't exist

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 23:03:49 UTC, Ali Çehreli wrote: That's not correct. There are many range algorithms that are lazy to defer memory allocation but array() is not one of those. array() does eagerly allocate memory, which is it's whole purpose:

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: So, in all three examples it is the same D feature, a slice, that references data but the data is managed in different ways. Ali Well, it's fair to say, that 'range-based programming' is kinda new to me. With this statement:

Re: Starting and managing threads

2022-01-16 Thread forkit via Digitalmars-d-learn
On Monday, 27 December 2021 at 10:59:07 UTC, Ali Çehreli wrote: ...my DConf Online 2020 presentation here: https://dconf.org/2020/online/#ali1 Ali Hey, that is a really great presentation! Many more people should watch it, and learn ;-)

Re: Dynamic array ot not

2022-01-16 Thread forkit via Digitalmars-d-learn
On Sunday, 16 January 2022 at 04:58:21 UTC, Ali Çehreli wrote: I have a problem with calling type[] a dynamic array because it is a slice, which may be providing access to the elements of a dynamic array. Yes. A more useful way of describing [] would be to say: "[] represents a dynamic

Dynamic array ot not

2022-01-15 Thread forkit via Digitalmars-d-learn
so at this link: https://dlang.org/spec/arrays.html it indicates an array of type[] is of type 'Dynamic array'. with that in mind, I ask, is this below a 'Dynamic array'. If not, why not? int[][] mArr2 = array(iota(1, 9).chunks(2).map!array.array);

Re: How to convert a chunks result to a two-dimensional array

2022-01-14 Thread forkit via Digitalmars-d-learn
On Saturday, 15 January 2022 at 03:48:41 UTC, Steven Schveighoffer wrote: Alternatively (with only one allocation for the int[] data): ```d int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array; ``` -Steve All answers were helpful ;-) But I like this one the best, because I find it

Re: How to convert a chunks result to a two-dimensional array

2022-01-14 Thread forkit via Digitalmars-d-learn
On Saturday, 15 January 2022 at 02:41:08 UTC, Paul Backus wrote: import std.algorithm: map; import std.array: array; int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array; oh. thanks! also it seems I reposted the same question (didn't realise this one got posted)

Re: chunks result to array

2022-01-14 Thread forkit via Digitalmars-d-learn
On Saturday, 15 January 2022 at 03:18:21 UTC, forkit wrote: oops. ignore this - was a repost of another post that I thought never got posted ;-)

chunks result to array

2022-01-14 Thread forkit via Digitalmars-d-learn
I want to do the equivalent of this: int[][] arrayOfarrays = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]; But getting the initialiser values using iota and chunks instead. Any help will be appreciated. note: to2Darray is not a valid statement ;-) // --- module test; import

How to convert a chunks result to a two-dimensional array

2022-01-14 Thread forkit via Digitalmars-d-learn
I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] Any help will be appreciated. note: to2Darray is not a valid statement ;-) // --- module test; import std; void main() { int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // how to convert

Re: A slice consisting of non-consecutive elements of an array?

2022-01-13 Thread forkit via Digitalmars-d-learn
On Thursday, 13 January 2022 at 20:32:40 UTC, Stanislav Blinov wrote: On Thursday, 13 January 2022 at 19:52:27 UTC, forkit wrote: Any idea on how I can get a ptr (without hardcoding C style) e.g. something like this: immutable(string)*[] pointers = strings.filter!(x => x ==

Re: A slice consisting of non-consecutive elements of an array?

2022-01-13 Thread forkit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{ auto a = ["one", "one", "two", "one", "two", "one", "one", "two"]; writeln(a);

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread forkit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{ auto a = ["one", "one", "two", "one", "two", "one", "one", "two"]; writeln(a);

A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread forkit via Digitalmars-d-learn
I am familiar with the concept of a slice in D. However, a slice is a consecutive slice, is in not? (e.g) [4..$-1] I would like a slice (or a view, or whatever name you wanna call it), of particular elements within an array that may not be consecutive. e.g. [4-7,8,10,13-16] Consider below:

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 21:50:00 UTC, Paul Backus wrote: .. If you know a particular bit of code is memory safe, but the compiler can't prove it, you can mark that code as @trusted. For example: () @trusted { pointers ~= )(); This example uses an immediately-invoked function

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 11 January 2022 at 14:54:51 UTC, Paul Backus wrote: .. If you compile with -preview=dip1000, the compiler will actually keep track of which pointers point to stack memory, and will allow your original code. But -preview=dip1000 is still somewhat experimental, and the documentation

Re: @safe question

2022-01-11 Thread forkit via Digitalmars-d-learn
On Monday, 10 January 2022 at 03:21:46 UTC, Paul Backus wrote: Taking the address of a local variable is forbidden in @safe code. Even though str is a ref variable that points to a heap-allocated string, it is still considered a local variable because it is declared inside the body of a

Re: @safe question

2022-01-09 Thread forkit via Digitalmars-d-learn
On Sunday, 9 January 2022 at 21:56:05 UTC, Salih Dincer wrote: Try the @trusted and in/out: ... .. . thanks for introducing me to the in/out feature of D :-) I'll certainly look into that feature more. But my question still remains: //pointers ~= // why is this *not* allowed in @safe

@safe question

2022-01-09 Thread forkit via Digitalmars-d-learn
Do not understand why one line is not considered @safe, but the other is. // module test; import std; @safe void main() { immutable string[] strings = ["one", "one", "two"]; immutable(string)*[] pointers = null; foreach(size_t i, ref str; strings) { if(str ==

Re: what is going on here?

2022-01-05 Thread forkit via Digitalmars-d-learn
On Tuesday, 4 January 2022 at 23:37:57 UTC, H. S. Teoh wrote: Or the compiler would run out of memory before it gets to optimizing away those assignments, so it would just outright crash. I ran your code on my computer and got this: uncaught exception

Re: what is going on here?

2022-01-04 Thread forkit via Digitalmars-d-learn
On Tuesday, 4 January 2022 at 23:37:57 UTC, H. S. Teoh wrote: ... .. . Which seems to confirm my suspicions. T yes, this sounds like it might be it. I tried using = void; .. results: dmd -m64 -> just results in an 'out of memory' message (but at least i get a message this time) ldc2

what is going on here?

2022-01-04 Thread forkit via Digitalmars-d-learn
strange things happen when I compile (on windows) the code below with: dmd -m64 (compilation just crashes - no error message at all) or ldc2 -m64 (compilation works, but memory usage during compilation goes completely wild! upto 22GB, then down to 7GB, then finally completes.) //

Re: A debug class has started

2021-12-14 Thread forkit via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:07:43 UTC, WebFreak001 wrote: The best way would be not doing this at all - when you manipulate strings/arrays in D you can do so by just assigning the elements like this: ```d immutable(char)[] replaceChar(char[] str, char ch1, char ch2) { for (ulong

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 21:13:25 UTC, H. S. Teoh wrote: What you should be doing is: return to!string(str[0 .. len]); Or just: return str[0 .. len].idup; T oh.. so many different ways...(to both produce the same bug, and also to produce the correct output). ...

Re: ImportC std support

2021-12-13 Thread forkit via Digitalmars-d-learn
On Sunday, 12 December 2021 at 08:25:09 UTC, Dave P. wrote: ImportC is not ready for general use yet. I think you nailed it ;-) https://dlang.org/changelog/2.098.1.html

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn wrote: On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: > > You should really use `.dup` if you want to mutate your > string. (You w

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn wrote: On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: > > You should really use `.dup` if you want to mutate your > string. (You w

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: You should really use `.dup` if you want to mutate your string. (You would need to duplicate anyway if you don't want an unsafe cast) (this produces an unpredictable result??) char* w = cast(char*)str.dup; (but this seems to

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: char* w = cast(char*)str.toStringz; // this seems to be the solution class has ended ;-)

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: oh... Windows - dmd version is 2.098.0-dirty - ldc2 version is 1.28 (based on dmd v2.098.0) Linux - dmd version is 2.098 - ldc2 version is 1.20.1 (based on dmd v2.090.1)

A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
ok. one line in this code is causing a problem (as noted in comments) an explanation as to the cause, is welcome ;-) // module test; import std : writeln, writefln; import std.conv : to; void main() { string str = "abc;def;ab"; char* w = cast(char*)str;

Re: How to loop through characters of a string in D language?

2021-12-12 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:25:37 UTC, Ola Fosheim Grøstad wrote: ```putchar(…)``` is too slow! On planet Mars maybe, but here on earth, my computer can do about 4 billion ticks per second, and my entire program (using putchar) takes only 3084 ticks.

Re: ImportC std support

2021-12-11 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 21:42:49 UTC, ManKey wrote: What implementations of the C standard library does importC support? umm... the site has search function you know ;-) https://dlang.org/spec/importc.html

Re: How to loop through characters of a string in D language?

2021-12-11 Thread forkit via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:05:01 UTC, Ola Fosheim Grøstad wrote: Using libraries can trigger hidden allocations. ok. fine. no unnecessary, hidden allocations then. // -- module test; import core.stdc.stdio : putchar; nothrow @nogc void main() { string str =

Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn
On Friday, 10 December 2021 at 22:35:58 UTC, Arjan wrote: "abc;def;ghi".tr(";", "", "d" ); I don't think we have enough ways of doing the same thing yet... so here's one more.. "abc;def;ghi".substitute(";", "");

Re: How to loop through characters of a string in D language?

2021-12-10 Thread forkit via Digitalmars-d-learn
On Friday, 10 December 2021 at 12:15:18 UTC, Rumbu wrote: I thought it's a beauty contest. Well, if it's a beauty contest, then i got a beauty.. char[("abc;def;ab".length - count("abc;def;ab", ";"))] b = "abc;def;ab".replace(";", "");

Re: How to loop through characters of a string in D language?

2021-12-09 Thread forkit via Digitalmars-d-learn
On Thursday, 9 December 2021 at 18:00:42 UTC, kdevel wrote: PRO: - saves two lines of boilerplate code CONS: - raw loop - postinc ++ is only permitted in ++C - inconsistent spacing around "=" - mixing tabs and spaces for indentation - arrow code more PROs: - You become less dependent on

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:55:02 UTC, forkit wrote: On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote: You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code. but this will change nothing. the compilation cost of

Re: How to loop through characters of a string in D language?

2021-12-08 Thread forkit via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 14:27:22 UTC, BoQsc wrote: On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote: On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: Let's say I want to skip characters and build a new string. The string example to loop/iterate: ``` import

Re: d strings are the bane of my existance

2021-12-06 Thread forkit via Digitalmars-d-learn
On Sunday, 5 December 2021 at 16:24:34 UTC, Chris Katko wrote: I know there "is" a solution, it's just so odd to have this much difficulty using a string. Paying attention to the online docs would help you too ;-) https://dlang.org/library/std/socket/internet_address.this.html But, in the

Re: How to deploy single exe application (?)

2021-11-29 Thread forkit via Digitalmars-d-learn
On Monday, 29 November 2021 at 14:58:07 UTC, Willem wrote: Thanks again for all the responses. For now -- I am simply adding the DLL to the EXE and writing it out to the working directory. Not elegant - but it does work. "Programmers are not to be measured by their ingenuity and their

Re: How to read a single character in D language?

2021-11-24 Thread forkit via Digitalmars-d-learn
On Friday, 19 November 2021 at 17:36:55 UTC, BoQsc wrote: Let's say I want to write a simple program that asks for an input of a single character. After pressing a single key on a keyboard, the character is printed out and the program should stop. module test; void main() { import

Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn
On Sunday, 14 November 2021 at 04:24:09 UTC, Stanislav Blinov wrote: On Sunday, 14 November 2021 at 04:05:45 UTC, forkit wrote: However, there is no isClass method. Why not? How do I determine if a member is a class.. I wonder... ``` static if (is(something == class)) { /* ... */ } ``` or,

Re: Completing C code with D style

2021-11-13 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 23:02:15 UTC, pascal111 wrote: I touch that D is big language, it's not small like standard C. This will cost me much studying, so I think I need slow down and learn it step by step. Yes. C is so much smaller, and thus simpler (till you wanna do something

Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 17:22:16 UTC, Stanislav Blinov wrote: On Saturday, 13 November 2021 at 08:04:56 UTC, forkit wrote: int i; foreach(m; __traits(allMembers, mixin(__MODULE__))) // ... __traits(getLocation, mixin(m))[1]); What you really should be doing is this:

Re: using __traits to get line number of a member

2021-11-13 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 07:20:14 UTC, Ali Çehreli wrote: It works because we mix-in the value of the string 'm', which becomes a symbol. ('foreach' instead of 'static foreach' works as well.) Ali Thanks. Really appreciate the help provided in this thread :-) Final working code

Re: using __traits to get line number of a member

2021-11-12 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 06:05:37 UTC, Stanislav Blinov wrote: On Saturday, 13 November 2021 at 05:31:51 UTC, forkit wrote: Code below is self explanatory. Any assistance on how to get the line number is welcome ;-) https://dlang.org/spec/traits.html#getLocation That? Thanks. That

using __traits to get line number of a member

2021-11-12 Thread forkit via Digitalmars-d-learn
Code below is self explanatory. Any assistance on how to get the line number is welcome ;-) // ++ module test; import std; class myClass{ void foo(){}} void myFunction1(){} void main() { // list the first user defined member of this module (other than main)

Re: How to use dmd code coverage

2021-11-12 Thread forkit via Digitalmars-d-learn
On Friday, 12 November 2021 at 19:32:31 UTC, Dr Machine Code wrote: On Thursday, 11 November 2021 at 23:51:42 UTC, foxit wrote: On Thursday, 11 November 2021 at 22:35:21 UTC, forkit wrote: [...] Actually, the reason I got soo confused is clear to me now. I have my own GUI IDE, which I

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 19:34:42 UTC, Stanislav Blinov wrote: Original C code from the first post can only fail on I/O, which is arguably out of your control. And the meat of it amounts to 10 conditional stores. Your implementations, in both C and D, are a very, very far distance

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Friday, 12 November 2021 at 01:05:15 UTC, Stanislav Blinov wrote: On Thursday, 11 November 2021 at 22:10:04 UTC, forkit wrote: It's called 'staged learning'. Staged learning is the only way for humans to learn, due to the limitations of the human cognitive system. Specifically, the way

Re: How to use dmd code coverage

2021-11-11 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 21:40:33 UTC, Ali Çehreli wrote: On 11/11/21 1:37 PM, forkit wrote: dmd test.d -cov ...but no .lst file anywhere to be found. Huh! I don't get it. Please run the program! :) Ali oh! that kinda makes sense, now that I think of it ;-)

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 21:13:03 UTC, Stanislav Blinov wrote: On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: It depends on what you're doing. In the OP's example, yeah worrying about allocations is totally blowing things out of proportions. But that's the thing.

How to use dmd code coverage

2021-11-11 Thread forkit via Digitalmars-d-learn
// -- module test; import std.stdio; void main() { writeln("Hello World!"); } // --- dmd test.d -cov ..but no .lst file anywhere to be found. Huh! I don't get it.

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: On Wed, Nov 10, 2021 at 11:39:40PM +, forkit via Digitalmars-d-learn wrote: [...] I still remember compiling code on my 286x86 ... talk about low memory..whoaaah. ... But if you're in a time-constrained inner loop, you do

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 23:05:06 UTC, H. S. Teoh wrote: On Wed, Nov 10, 2021 at 10:17:48PM +, russhy via Digitalmars-d-learn wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: > btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. > So I really don't

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 22:17:48 UTC, russhy wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations .. not one little bit ;-) Having the right mindset helps

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 04:54:58 UTC, Stanislav Blinov wrote: On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 04:54:58 UTC, Stanislav Blinov wrote: On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote: On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it

Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn
On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote: case 'o' : result = result.filter!(a => (a % 2 == 1)).array; oops. case 'o' : result = result.filter!(a => (a % 2 != 0)).array;

Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D

<    1   2