Re: bool passed by ref, safe or not ?

2024-06-04 Thread rkompass via Digitalmars-d-learn
On Tuesday, 4 June 2024 at 16:58:50 UTC, Basile B. wrote: question in the header, code in the body, execute on a X86 or X86_64 CPU ```d module test; void setIt(ref bool b) @safe { b = false; } void main(string[] args) { ushort a = 0b; bool* b = cast(bool*)

Re: Inconsistent chain (implicitly converts to int)

2024-04-06 Thread rkompass via Digitalmars-d-learn
On Friday, 5 April 2024 at 21:26:10 UTC, Salih Dincer wrote: On Friday, 5 April 2024 at 21:16:42 UTC, rkompass wrote: In the first example the int's are converted to doubles (also common type). But they appear as int's because writeln does not write a trailing .0. But it doesn't work as

Re: Inconsistent chain (implicitly converts to int)

2024-04-05 Thread rkompass via Digitalmars-d-learn
On Friday, 5 April 2024 at 16:05:20 UTC, H. S. Teoh wrote: On Fri, Apr 05, 2024 at 03:18:09PM +, Salih Dincer via Digitalmars-d-learn wrote: Hi everyone, Technically r1 and r2 are different types of range. Isn't it inconsistent to chain both? If not, why is the char type converted to

Re: Two chunks but No allocation

2024-03-28 Thread rkompass via Digitalmars-d-learn
On Thursday, 28 March 2024 at 03:54:05 UTC, Salih Dincer wrote: On Wednesday, 27 March 2024 at 20:50:05 UTC, rkompass wrote: This works: I decided to give the full code. Maybe then it will be better understood what I mean. I actually pointed out the indirect solution above but it's a bit

Re: Why is this code slow?

2024-03-28 Thread rkompass via Digitalmars-d-learn
On Thursday, 28 March 2024 at 14:07:43 UTC, Salih Dincer wrote: On Thursday, 28 March 2024 at 11:50:38 UTC, rkompass wrote: Turning back to this: Are there similarly simple libraries for C, that allow for parallel computation? You can achieve parallelism in C using libraries such as

Re: Why is this code slow?

2024-03-28 Thread rkompass via Digitalmars-d-learn
On Thursday, 28 March 2024 at 01:09:34 UTC, Salih Dincer wrote: Good thing you're digressing; I am 45 years old and I still cannot say that I am finished as a student! For me this is version 4 and it looks like we don't need a 3rd variable other than the function parameter and return value:

Re: Two chunks but No allocation

2024-03-27 Thread rkompass via Digitalmars-d-learn
On Wednesday, 27 March 2024 at 13:38:29 UTC, Salih Dincer wrote: So, not works this: ```d fib(1, 1).take(48) //.array .chunks(2) .map!"a[1] / a[0]" .back .writeln; // 1.61803 ``` Thanks... SDB@79 This works: ```d import std.stdio; import

Re: Why is this code slow?

2024-03-27 Thread rkompass via Digitalmars-d-learn
I apologize for digressing a little bit further - just to share insights to other learners. I had the question, why my binary was so big (> 4M), discovered the `gdc -Wall -O2 -frelease -shared-libphobos` options (now >200K). Then I tried to avoid GC, just learnt about this: The GC in the

Re: Why is this code slow?

2024-03-25 Thread rkompass via Digitalmars-d-learn
On Sunday, 24 March 2024 at 23:02:19 UTC, Sergey wrote: On Sunday, 24 March 2024 at 22:16:06 UTC, rkompass wrote: Are there some simple switches / settings to get a smaller binary? 1) If possible you can use "betterC" - to disable runtime 2) otherwise ```bash --release --O3 --flto=full

Re: Why is this code slow?

2024-03-24 Thread rkompass via Digitalmars-d-learn
The term containing the `pow` invocation computes the alternating sequence -1, 1, -1, ..., which can be replaced by e.g. ``` immutable int [2] sign = [-1, 1]; n += sign [i & 1] / (i * 2.0 - 1.0); ``` This saves the expensive call to the pow function. I used the loop: ```d for

Re: How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-18 Thread rkompass via Digitalmars-d-learn
@bachmeier You're not the first one. There's no technical reason for the restriction. It's simply a matter of being opposed by those who make these decisions on the basis that it's the wrong way to program or something like that. Here is a recent thread:

Re: How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-18 Thread rkompass via Digitalmars-d-learn
To solve the problem with the 1-variable and 2-variable versions of foreach I tried opApply and found that the compiler prefers it over opSlice and opIndex() (the latter without argument). My code: ```d int opApply(int delegate(Variant) foreachblock) const { int result

Re: How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-15 Thread rkompass via Digitalmars-d-learn
On Friday, 15 March 2024 at 17:15:56 UTC, monkyyy wrote: On Friday, 15 March 2024 at 09:03:25 UTC, rkompass wrote: @Monkyyy: I adopted your solution, it is perfect. I only have one problem left: The foreach loop with associative arrays has two cases: `foreach(key, val; arr)` and `foreach(x;

Re: How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-15 Thread rkompass via Digitalmars-d-learn
@Monkyyy: I adopted your solution, it is perfect. I only have one problem left: The foreach loop with associative arrays has two cases: `foreach(key, val; arr)` and `foreach(x; arr)`. In the second case only the values are iterated. With the present solution the iteration delivers (key, val)

Re: How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-14 Thread rkompass via Digitalmars-d-learn
Hello @monkyyy, thank you for your help. I will study and try your code. Meanwhile I have found that I can add this function into the struct: ```d // postblit constructor, see // https://stackoverflow.com/questions/38785624/d-struct-copy-constructor this(this) {

How to make a struct containing an associative array to deeply copy (for repeated usage in foreach) ?

2024-03-13 Thread rkompass via Digitalmars-d-learn
I want to make a custom dictionary that I may iterate through with foreach. Several times. What I observe so far is that my dict as a simple forward range is exhausted after the first foreach and I have to deeply copy it beforehand. With a simple associative array the exhaustion is not