Re: is the array literal in a loop stack or heap allocated?

2023-10-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/10/23 10:54 PM, mw wrote: Hi, I want to confirm: in the following loop, is the array literal `a` vs. `b` stack or heap allocated? and how many times? ask the compiler: ```d void main() @nogc { int[2] a; int[] b; int i; while(++i <=100) { a = [i, i+1]; // array literal //b =

Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread Imperatorn via Digitalmars-d-learn
On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote: Hi, I want to confirm: in the following loop, is the array literal `a` vs. `b` stack or heap allocated? and how many times? void main() { int[2] a; int[] b; int i; While(++i <=100) { a = [i, i+1]; // array literal b = [i,

Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn
On Wednesday, 11 October 2023 at 03:15:30 UTC, H. S. Teoh wrote: On Wed, Oct 11, 2023 at 02:54:53AM +, mw via Digitalmars-d-learn wrote: Hi, I want to confirm: in the following loop, is the array literal `a` vs. `b` stack or heap allocated? and how many times? void main() { int[2

Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread ryuukk_ via Digitalmars-d-learn
On Wednesday, 11 October 2023 at 02:54:53 UTC, mw wrote: Hi, I want to confirm: in the following loop, is the array literal `a` vs. `b` stack or heap allocated? and how many times? void main() { int[2] a; int[] b; int i; While(++i <=100) { a = [i, i+1]; // array literal b = [i,

Re: is the array literal in a loop stack or heap allocated?

2023-10-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Oct 11, 2023 at 02:54:53AM +, mw via Digitalmars-d-learn wrote: > Hi, > > I want to confirm: in the following loop, is the array literal `a` vs. > `b` stack or heap allocated? and how many times? > > void main() { > > int[2] a; This is stack-allocated. Once

is the array literal in a loop stack or heap allocated?

2023-10-10 Thread mw via Digitalmars-d-learn
Hi, I want to confirm: in the following loop, is the array literal `a` vs. `b` stack or heap allocated? and how many times? void main() { int[2] a; int[] b; int i; While(++i <=100) { a = [i, i+1]; // array literal b = [i, i+1]; } } Thanks.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-09 Thread bauss via Digitalmars-d-learn
On Monday, 9 January 2023 at 00:19:39 UTC, thebluepandabear wrote: Fixing this will improve the quality of the language for newcomers such as myself greatly. This not only confuses newcomers but it gave a false illusion of a bug within the code :/ It doesn't confuse newcomers only, also

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-08 Thread thebluepandabear via Digitalmars-d-learn
: create a copy of the value that is currently present in one particular iteration of the `foreach` by creating a function literal that takes your `struct` as a paramter. This works because structs are value types, so passing it to the function creates a copy. Thanks, your solution seems to

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-08 Thread thebluepandabear via Digitalmars-d-learn
Fixing this will improve the quality of the language for newcomers such as myself greatly. This not only confuses newcomers but it gave a false illusion of a bug within the code :/

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-08 Thread thebluepandabear via Digitalmars-d-learn
A nested function (or perhaps an inline lambda) is needed to force the allocation of a dynamic context for the capture. This is an embarrassment. Why hasn't this been fixed yet? :-( T I agree that this needs to get fixed immediately, it seems to be bugging me another time as well. I

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread Tejas via Digitalmars-d-learn
On Thursday, 5 January 2023 at 22:49:01 UTC, thebluepandabear wrote: Have fun reading this : https://issues.dlang.org/show_bug.cgi?id=21929 Thanks for the code suggestion although it still doesn't fix the bug. I am curious as to what those brackets do as well. Okay, my bad for writing the

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
> _boardSizeRow.addChild(button); > > } > > ``` [...] > Your code with the variable capture doesn't seem to work. Argh, apparently locals inside the loop body are subject to the same quirky behaviour. >:-( Here's a workaround that works: foreach (BoardSize boardSiz

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
``` These two solutions should compile to approximately the same runtime code, with optimizations enabled. So, it's really down to personal preference; the former is more explicit about what the computer is to do, while the latter is more concise. Thanks! Works great.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
Have fun reading this : https://issues.dlang.org/show_bug.cgi?id=21929 Thanks for the code suggestion although it still doesn't fix the bug. I am curious as to what those brackets do as well.

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
oardSize[1]); button.onButtonClick = { eventHandler.settingsWindow_onBoardSizeButtonClick(boardSize); }; button.onButtonClick(); _boardSizeRow.addChild(button); } ``` This is a classic D trap: the loop variable is only allocated once, and the closure captures the single loca

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread H. S. Teoh via Digitalmars-d-learn
button.onButtonClick = { > eventHandler.settingsWindow_onBoardSizeButtonClick(boardSize); > }; > button.onButtonClick(); > _boardSizeRow.addChild(button); > } > ``` This is a classic D trap: the loop variable is only allocated once, and the closure captures the single location where the loop

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread Tejas via Digitalmars-d-learn
On Thursday, 5 January 2023 at 11:55:33 UTC, thebluepandabear wrote: I am using CSFML D bindings and I have created my own sort of UI library for drawing elements onto the screen. One of the classes I've created is a `Button` class, which contains a delegate called `onButtonClick` which is

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread tsbockman via Digitalmars-d-learn
= format("%sx%s", boardSize[0], boardSize[1]); b[indx].onButtonClick = { eventHandler.settingsWindow_onBoardSizeButtonClick(boardSize); }; _boardSizeRow.addChild(b[indx]); } ``` This is semantically equivalent to copying and pasting the loop body `arr.length` numbe

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread tsbockman via Digitalmars-d-learn
ch is gets iterated last. The problem is twofold: 1. Closures in D capture their environment by reference. 2. D (incorrectly, in my opinion) considers loop-local variables to have the same identity across each iteration of the loop within a single function call. So, `boardSize` in your eve

Re: How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
Update some time later: the only way (oof!) around this seems to be using a `static foreach` with arrays: ```D Button[3] b; static foreach (indx, BoardSize boardSize; arr) { b[indx] = new Button(); b[indx].text = format("%sx%s", boardSize[0], boardSize[1]); b[indx].onButtonClick

How to avoid variable capturing in `foreach` loop with lambdas?

2023-01-05 Thread thebluepandabear via Digitalmars-d-learn
I am using CSFML D bindings and I have created my own sort of UI library for drawing elements onto the screen. One of the classes I've created is a `Button` class, which contains a delegate called `onButtonClick` which is called when the button is clicked on by the user. Up until now,

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread rikki cattermole via Digitalmars-d-learn
On 30/08/2022 8:16 AM, Gavin Ray wrote: It must have been the "writing at end of file" bit? I don't know. It read like it should work. The offsets were correct, it just didn't work *shrug*.

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread Gavin Ray via Digitalmars-d-learn
On Monday, 29 August 2022 at 15:52:31 UTC, rikki cattermole wrote: After a bunch of playing around I managed to determine that it is as simple as the mode. exists(dbFileName) ? "r+" : "w+" Will fix it. Of course you shouldn't delete the file like that method is doing. It should probably

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 29 August 2022 at 16:21:53 UTC, ag0aep6g wrote: You never change `pageId`. So as far as I can tell, you're always `seek`-ing to the same position, and you just overwrite the same piece of the file again and again. Whoops. I guess I missed the point of the question there.

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 28 August 2022 at 22:46:17 UTC, Gavin Ray wrote: I've put the code, stripped to a minimal example here: - https://ldc.godbolt.org/z/fzsx3Tnnn [...] But if the same code is placed inside of a `for` loop, suddenly no writes occur: [...] Does anyone know what is happening here

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread rikki cattermole via Digitalmars-d-learn
After a bunch of playing around I managed to determine that it is as simple as the mode. exists(dbFileName) ? "r+" : "w+" Will fix it. Of course you shouldn't delete the file like that method is doing. It should probably reinitialize the FILE* descriptor.

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread Gavin Ray via Digitalmars-d-learn
On Monday, 29 August 2022 at 07:04:49 UTC, bauss wrote: Does anyone know what is happening here? It's really puzzling. You probably need to flush the output. That's a good idea. I gave it a shot, and the following doesn't seem to change anything unfortunately: ```d void writePage(PageId

Re: Disk write in a "for" loop with RwMutex never happens

2022-08-29 Thread bauss via Digitalmars-d-learn
] ``` Where here, `pageData` is the data to be written to a file, and `readData` is the result of trying to read the freshly written file data. But if the same code is placed inside of a `for` loop, suddenly no writes occur: ```d pageData[0..4] = [0, 0, 0, 0] readData[0..4] = [0, 0, 0, 0

Disk write in a "for" loop with RwMutex never happens

2022-08-28 Thread Gavin Ray via Digitalmars-d-learn
to a file, and `readData` is the result of trying to read the freshly written file data. But if the same code is placed inside of a `for` loop, suddenly no writes occur: ```d pageData[0..4] = [0, 0, 0, 0] readData[0..4] = [0, 0, 0, 0] pageData[0..4] = [0, 0, 0, 1] readData[0..4] = [0, 0, 0, 0

Re: Main foreach loop fails when another foreach is added

2022-08-07 Thread ikelaiah via Digitalmars-d-learn
On Monday, 8 August 2022 at 02:45:54 UTC, Steven Schveighoffer wrote: And now, you tried to read it again! Which means you are trying to read more data from an empty stream. You need to either a) reopen the file, or b) do both in the same loop. -Steve Steve! You are Legend! **Thank you

Re: Main foreach loop fails when another foreach is added

2022-08-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/7/22 10:11 PM, ikelaiah wrote: Hi, I'm writing a program that reads a text file and launch my work URLs in it. It worked fine, and very happy. Then I added another `foreach` loop to count total number of lines. After this, the main `foreach` won't work. Does anyone know as to why

Main foreach loop fails when another foreach is added

2022-08-07 Thread ikelaiah via Digitalmars-d-learn
Hi, I'm writing a program that reads a text file and launch my work URLs in it. It worked fine, and very happy. Then I added another `foreach` loop to count total number of lines. After this, the main `foreach` won't work. Does anyone know as to why this happens? I might have missed

Re: vectorization of a simple loop -- not in DMD?

2022-07-14 Thread max haughton via Digitalmars-d-learn
On Thursday, 14 July 2022 at 13:00:24 UTC, ryuukk_ wrote: On Thursday, 14 July 2022 at 05:30:58 UTC, Siarhei Siamashka wrote: On Tuesday, 12 July 2022 at 13:23:36 UTC, ryuukk_ wrote: I wonder if DMD/LDC/GDC have built in tools to profile and track performance Linux has a decent system wide

Re: vectorization of a simple loop -- not in DMD?

2022-07-14 Thread ryuukk_ via Digitalmars-d-learn
On Thursday, 14 July 2022 at 05:30:58 UTC, Siarhei Siamashka wrote: On Tuesday, 12 July 2022 at 13:23:36 UTC, ryuukk_ wrote: I wonder if DMD/LDC/GDC have built in tools to profile and track performance Linux has a decent system wide profiler: https://perf.wiki.kernel.org/index.php/Main_Page

Re: vectorization of a simple loop -- not in DMD?

2022-07-14 Thread z via Digitalmars-d-learn
: https://godbolt.org/z/GcznbjEaf From what I gather at the view linked above, DMD does not use XMM registers for speedup, and does not unroll the loop either. Switching between 32bit and 64bit doesn't help either. However, I recall in the past it was capable of at least some

Re: vectorization of a simple loop -- not in DMD?

2022-07-13 Thread Siarhei Siamashka via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:23:36 UTC, ryuukk_ wrote: I wonder if DMD/LDC/GDC have built in tools to profile and track performance Linux has a decent system wide profiler: https://perf.wiki.kernel.org/index.php/Main_Page And there are other useful tools, such as callgrind. To take

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread ryuukk_ via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 12:47:26 UTC, bauss wrote: Of course if you're alone it doesn't matter, but if it's a larger project that will have multiple maintainers then it will never work and will tarnish the project entirely. That's true, i work solo on my project so it doesn't bother me

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread bachmeier via Digitalmars-d-learn
On Monday, 11 July 2022 at 21:46:10 UTC, IGotD- wrote: Just depreciate the the DMD backend, it's just not up to the task anymore. Just deprecate LDC and GDC. They compile slowly and are unlikely to ever deliver fast compile times, due to their design. Some people say they like it because

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread bauss via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 10:32:36 UTC, ryuukk_ wrote: How do i achieve fast compile speed (results above were on windows, on linux i get much faster results): I maintain healthy project management: This - Imports of std: i simply don't, and i copy/paste functions i need - I avoid

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread ryuukk_ via Digitalmars-d-learn
How do i achieve fast compile speed (results above were on windows, on linux i get much faster results): I maintain healthy project management: - Templates ONLY when necessary and when the cost is worth the time saved in the long term - this is why i try to lobby for builtin tagged union

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread Siarhei Siamashka via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 07:58:44 UTC, bauss wrote: You don't think this difference is huge? DMD is over 2x as fast. I think that DMD having more than 10x faster compilation speed in ryuukk_'s project shows that there is likely either a misconfiguration in DUB build setup or some other

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread bauss via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 07:06:37 UTC, Siarhei Siamashka wrote: ``` real0m34.371s user0m32.883s sys 0m1.488s ``` ``` real0m14.078s user0m12.941s sys 0m1.129s ``` Is there an open source DUB package, which can be used to reproduce a huge build time difference

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread Siarhei Siamashka via Digitalmars-d-learn
On Monday, 11 July 2022 at 22:16:05 UTC, ryuukk_ wrote: LDC clean full rebuild ``` $ time dub build -f --compiler=ldc2 Performing "debug" build using ldc2 for x86_64. game ~master: building configuration "desktop"... Linking... real0m18.033s user0m0.000s sys 0m0.015s ``` DMD clean

Re: vectorization of a simple loop -- not in DMD?

2022-07-12 Thread Siarhei Siamashka via Digitalmars-d-learn
On Monday, 11 July 2022 at 22:16:05 UTC, ryuukk_ wrote: I use D because DMD compiles my huge project in ~1 second (full clean rebuild) It is a competitive advantage that many languages doesn't have The other programming languages typically use an interpreter for quick iterations and rapid

Re: vectorization of a simple loop -- not in DMD?

2022-07-11 Thread ryuukk_ via Digitalmars-d-learn
On Monday, 11 July 2022 at 21:46:10 UTC, IGotD- wrote: On Monday, 11 July 2022 at 18:19:41 UTC, max haughton wrote: The dmd backend is ancient, it isn't really capable of these kinds of loop optimizations. I've said it several times before. Just depreciate the the DMD backend, it's just

Re: vectorization of a simple loop -- not in DMD?

2022-07-11 Thread IGotD- via Digitalmars-d-learn
On Monday, 11 July 2022 at 18:19:41 UTC, max haughton wrote: The dmd backend is ancient, it isn't really capable of these kinds of loop optimizations. I've said it several times before. Just depreciate the the DMD backend, it's just not up to the task anymore. This is not criticism against

Re: vectorization of a simple loop -- not in DMD?

2022-07-11 Thread Bruce Carneal via Digitalmars-d-learn
: https://godbolt.org/z/GcznbjEaf From what I gather at the view linked above, DMD does not use XMM registers for speedup, and does not unroll the loop either. [snip] Specifying a SIMD capable target will reveal an even wider gap in capability. (LDC -mcpu=x86-64-v3 or gdc -march=x86-64-v3).

Re: vectorization of a simple loop -- not in DMD?

2022-07-11 Thread max haughton via Digitalmars-d-learn
: https://godbolt.org/z/GcznbjEaf From what I gather at the view linked above, DMD does not use XMM registers for speedup, and does not unroll the loop either. Switching between 32bit and 64bit doesn't help either. However, I recall in the past it was capable of at least some

vectorization of a simple loop -- not in DMD?

2022-07-11 Thread Ivan Kazmenko via Digitalmars-d-learn
linked above, DMD does not use XMM registers for speedup, and does not unroll the loop either. Switching between 32bit and 64bit doesn't help either. However, I recall in the past it was capable of at least some of these optimizations. So, how do I enable them for such a function? Ivan

Re: vibe.d requestHTTP in static this causes infinite loop?

2022-05-20 Thread Vijay Nayar via Digitalmars-d-learn
uld cause an infinite loop? I am not experienced with vibe.d. 'static this' is executed per thread. If requestHTTP starts a new thread, then I can see how you would be in an infinite loop. I wonder whether it should be 'shared static this' (which is executed once per program, not per thread). Ali Ve

Re: vibe.d requestHTTP in static this causes infinite loop?

2022-05-20 Thread bauss via Digitalmars-d-learn
uld cause an infinite loop? I am not experienced with vibe.d. 'static this' is executed per thread. If requestHTTP starts a new thread, then I can see how you would be in an infinite loop. I wonder whether it should be 'shared static this' (which is executed once per program, not per thread). Ali

Re: vibe.d requestHTTP in static this causes infinite loop?

2022-05-19 Thread Ali Çehreli via Digitalmars-d-learn
On 5/19/22 16:44, Vijay Nayar wrote: > If I remove the call from `static this()`, then the web call works as > normal. Any idea why calling vibe.d's `requestHTTP` function inside of a > module's static construction would cause an infinite loop? I am not experienced with vibe.d.

vibe.d requestHTTP in static this causes infinite loop?

2022-05-19 Thread Vijay Nayar via Digitalmars-d-learn
ot;); string data = res.bodyReader.readAllUTF8(); logInfo("Log point 2: %s", data); }); } static this() { logInfo("--- static this() ---"); doThing(); } void main(string[] args) { logInfo("--- main() ---"); doThing(); } ``` The output of

Re: While loop on global variable optimised away?

2022-05-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Wednesday, 11 May 2022 at 10:01:18 UTC, Johan wrote: Any function call (inside the loop) for which it cannot be proven that it never modifies your memory variable will work. That's why I'm pretty sure that mutex lock/unlock will work. I think the common semantics ought

Re: While loop on global variable optimised away?

2022-05-11 Thread ichneumwn via Digitalmars-d-learn
On Wednesday, 11 May 2022 at 10:01:18 UTC, Johan wrote: Any function call (inside the loop) for which it cannot be proven that it never modifies your memory variable will work. That's why I'm pretty sure that mutex lock/unlock will work. Thank you, in C I would not have been surprised

Re: While loop on global variable optimised away?

2022-05-11 Thread Johan via Digitalmars-d-learn
impact on multithreading. Instead, single-thread execution is assumed, and thus the optimization is valid. Your solution with `volatileLoad` is correct. Access through atomic function would prevent the compiler from optimising this away as well, but if I were to use a Mutex inside the loop, there is

Re: While loop on global variable optimised away?

2022-05-11 Thread rikki cattermole via Digitalmars-d-learn
Compiler optimizations should not be defined by a programming language specification. This will be on LLVM.

While loop on global variable optimised away?

2022-05-11 Thread ichneumwn via Digitalmars-d-learn
Access through atomic function would prevent the compiler from optimising this away as well, but if I were to use a Mutex inside the loop, there is no way for the compiler to tell *what* that Mutex is protecting and it might still decide to optimise the test away (assuming that is what is hap

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Jack Stouffer via Digitalmars-d-learn
On Thursday, 20 January 2022 at 01:14:51 UTC, Adam Ruppe wrote: On Thursday, 20 January 2022 at 00:55:33 UTC, Jack Stouffer wrote: static foreach(member; __traits(allMembers, Manager)) member here is a string, not the member. I prefer to call it memberName. Then you

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Adam Ruppe via Digitalmars-d-learn
On Thursday, 20 January 2022 at 00:55:33 UTC, Jack Stouffer wrote: static foreach(member; __traits(allMembers, Manager)) member here is a string, not the member. I prefer to call it memberName. Then you __traits(getMember, Manager, memberName) to actually get the alias you can

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Jack Stouffer via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 21:49:12 UTC, Adam D Ruppe wrote: I never use most of std.traits, they just complicate things. Bleh idk, I wouldn't bother with it and loop through the __traits instead. Unless I'm missing something obvious this has to be a DMD bug, because this prints

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Adam D Ruppe via Digitalmars-d-learn
with it and loop through the __traits instead.

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Jack Stouffer via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 20:53:29 UTC, Adam D Ruppe wrote: So you want to `__traits(child, system, this).run()` and it should work - the traits child will re-attach a this value. The error is actually coming from trying to use the result of getSymbolsByUDA in the right part of the

Re: Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Adam D Ruppe via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 20:46:17 UTC, Jack Stouffer wrote: static foreach(system; getSymbolsByUDA!(Manager, Runnable)) { system.run(); onlineapp.d(16): Error: value of `this` is not known at compile time The getSymbols returns aliases, meaning you hit

Using getSymbolsByUDA in a static foreach loop

2022-01-19 Thread Jack Stouffer via Digitalmars-d-learn
I'm trying to use getSymbolsByUDA in order to loop over all of the members in a struct with a certain UDA, and then call a function on the member. The plan is to use this to avoid looping over an array of function pointers. However, the compiler is giving a strange error

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

2021-12-23 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 23 December 2021 at 16:13:49 UTC, Stanislav Blinov wrote: You're comparing apples and oranges. When benchmarking, at least look at the generated assembly first. I looked now and you're right. Insomuch that it should be eggplant not apple, banana not orange...:) Because it's an

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

2021-12-23 Thread rumbu via Digitalmars-d-learn
On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote: It seems faster than algorithms in Phobos. We would love to see this in our new Phobos. Replace: 436 msecs Malloc : 259 msecs */ It seems because MallocReplace is cheating a lot: - it is not called through another function

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

2021-12-23 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote: It seems faster than algorithms in Phobos. We would love to see this in our new Phobos. ```d void mallocReplace() void normalReplace() string result = str.replace(';',""); }/* Console Out: Replace: 436 msecs Malloc

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

2021-12-22 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:36:57 UTC, Ola Fosheim Grøstad wrote: ```d @safe: string prematureoptimizations(string s, char stripchar) @trusted { import core.memory; immutable uint flags = GC.BlkAttr.NO_SCAN|GC.BlkAttr.APPENDABLE; char* begin =

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

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

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

2021-12-12 Thread bauss via Digitalmars-d-learn
On Monday, 13 December 2021 at 05:46:06 UTC, forkit wrote: 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

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: How to loop through characters of a string in D language?

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
Of course, since it is easy to mess up and use ranges in the wrong way, you might want to add ```assert```s. That is most likely *helpful* to newbies that might want to use your kickass library function: ``` auto helpfuldeatheater(char stripchar)(string str) { struct voldemort {

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

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Sunday, 12 December 2021 at 08:58:29 UTC, Ola Fosheim Grøstad wrote: this(string s)@trusted{ begin = s.ptr; end = s.ptr + s.length; } } Bug, it fails if the string ends or starts with ';'. Fix: ``` this(string s)@trusted{

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

2021-12-12 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 19:50:55 UTC, russhy wrote: you need to import a 8k lines of code module that itself imports other modules, and then the code is hard to read I agree. ``` @safe: auto deatheater(char stripchar)(string str) { struct voldemort { immutable(char)*

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

2021-12-11 Thread russhy via Digitalmars-d-learn
On Saturday, 11 December 2021 at 18:51:12 UTC, Rumbu wrote: On Saturday, 11 December 2021 at 14:42:53 UTC, russhy wrote: Here is mine - 0 allocations - configurable - let's you use it how you wish - fast You know that this is already in phobos? ``` "abc;def;ghi".splitter(';').joiner

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

2021-12-11 Thread Rumbu via Digitalmars-d-learn
On Saturday, 11 December 2021 at 14:42:53 UTC, russhy wrote: Here is mine - 0 allocations - configurable - let's you use it how you wish - fast You know that this is already in phobos? ``` "abc;def;ghi".splitter(';').joiner ```

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

2021-12-11 Thread russhy via Digitalmars-d-learn
Here is mine - 0 allocations - configurable - let's you use it how you wish - fast ```D import std; void main() { string a = "abc;def;ab"; writeln("a => ", a); foreach(item; split(a, ';')) writeln("\t", item); string b = "abc;def ;ab"; writeln("a => ",

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:40:47 UTC, Stanislav Blinov wrote: On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: void donttrythisathome(string s, char stripchar) @trusted { import core.stdc.stdlib; char* begin = cast(char*)alloca(s.length); A

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

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: void donttrythisathome(string s, char stripchar) @trusted { import core.stdc.stdlib; char* begin = cast(char*)alloca(s.length); A function with that name, and calling alloca to boot, cannot be @trusted ;)

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:34:17 UTC, Ola Fosheim Grøstad wrote: @system Shouldn't be there. Residual leftovers… (I don't want to confuse newbies!)

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:46:32 UTC, forkit wrote: 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

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 09:26:06 UTC, Stanislav Blinov wrote: What you're showing is... indeed, don't do this, but I fail to see what that has to do with my suggestion, or the original code. You worry too much, just have fun with differing ways of expressing the same thing.

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

2021-12-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 23:53:47 UTC, Ola Fosheim Grøstad wrote: ```d char[] dontdothis(string s, int i=0, int skip=0){ if (s.length == i) return new char[](i - skip); if (s[i] == ';') return dontdothis(s, i+1, skip+1); auto r = dontdothis(s, i+1, skip); r[i-skip] =

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 08:46:32 UTC, forkit wrote: 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

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-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;)

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

2021-12-11 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 11 December 2021 at 00:39:15 UTC, forkit wrote: 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(";", ""); Using

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 Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Oooh, finally someone suggested to preallocate storage for all these reinventions of the wheel :D ``` import std.stdio; char[] dontdothis(string s, int i=0, int skip=0){ if (s.length == i) return new char[](i - skip);

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

2021-12-10 Thread Arjan via Digitalmars-d-learn
On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu 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 character I want to skip: `;` Expected result: ``` abcdefab ``` Since it seems there is a contest here: ```d

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-10 Thread Luís Ferreira via Digitalmars-d-learn
Yes it will. You can use lazy templates instead, like splitter and joiner, which splits and joins lazily, respectively. LDC can optimize those templates fairly well and avoid too much lazy calls and pretty much constructs the logic equivalent to for loop. On 10 December 2021 11:06:21 WET

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

2021-12-10 Thread Rumbu via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Be interesting to see if this thread does evolve into a SIMD http://lemire.me/blog/2017/01/20/how-quickly-can-you-remove-spaces-from-a-string/

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

2021-12-10 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 10 December 2021 at 13:22:58 UTC, Matheus wrote: My C way of thinking while using D: import std; string stripsemicolons(string input){ char[] s = input.dup; int j=0; for(int i=0;i Oooh, finally someone suggested to preallocate storage for all these reinventions of

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

2021-12-10 Thread Matheus via Digitalmars-d-learn
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote: ... The character I want to skip: `;` My C way of thinking while using D: import std; string stripsemicolons(string input){ char[] s = input.dup; int j=0; for(int i=0;i

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

2021-12-10 Thread Rumbu via Digitalmars-d-learn
On Friday, 10 December 2021 at 11:06:21 UTC, IGotD- wrote: On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote: Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :) Would that become two for loops or not? I thought it's a beauty contest. ```d string

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

2021-12-10 Thread IGotD- via Digitalmars-d-learn
On Friday, 10 December 2021 at 06:24:27 UTC, Rumbu wrote: Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :) Would that become two for loops or not?

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

2021-12-09 Thread Rumbu via Digitalmars-d-learn
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 character I want to skip: `;` Expected result: ``` abcdefab ``` Since it seems there is a contest here: ```d "abc;def;ghi".split(';').join(); ``` :)

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

  1   2   3   4   5   6   >