Re: Setting field of struct object
On Monday, 22 January 2024 at 08:54:21 UTC, Danilo wrote: On Monday, 22 January 2024 at 08:35:01 UTC, Joel wrote: [...] Nonetheless, this usually used with Objects (new class/struct instances), like so: ```d import std; [...] Fluent Interface
Re: Range handling difficulties
On Wednesday, 24 April 2024 at 14:22:10 UTC, H. S. Teoh wrote: On Wed, Apr 24, 2024 at 08:08:06AM +, Menjanahary R. R. via Digitalmars-d-learn wrote: [...] evenfib.until!(n => n > 4_000_000).sum.writeln; T Thanks a lot! You've made my day
Range handling difficulties
I tried to solve Project Euler [problem #2](https://projecteuler.net/problem=2) using [Recurrence/recurrence](https://dlang.org/library/std/range/recurrence.html). Assuming `genEvenFibonacci` is the appropriate funtion in Explicit form, I got what I need like so: ``` auto evenfib = recurrence!genEvenFibonacci(2uL, 8uL); writeln; evenfib.take(11).sum.writeln; ``` But that's like cheating because there is no prior knowledge of `11`. I just got it manually by peeking at the sequence `[2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, 14930352]`. `14930352` must be filtered out because beyond the limit set! How to fix that properly using all the standard library capabilities programatically? I'm thinking of Range and/or std.algorithm.
Re: Mutability issue
On Saturday, 23 March 2024 at 20:49:14 UTC, Nick Treleaven wrote: On Saturday, 23 March 2024 at 19:30:29 UTC, Menjanahary R. R. wrote: for (T candidate = T(5); candidate * candidate <= n; candidate += T(6)) { When T is `const int`, the above code declares and initializes a constant variable: ```d const int candidate = const int(5); ``` Then, at the end of each loop iteration, it does: ```d candidate += const int(6); ``` So you are trying to modify a constant. Constants can only be initialized, never assigned. T candidate = (n % T(2) == T(0)) ? n + T(1) : n + T(2); // Start from next Odd for (;; candidate += T(2)) { // Skip even Same here, you declare a constant then try to assign to it at the end of each loop iteration. Thanks for your prompt answer.
Re: Mutability issue
On Saturday, 23 March 2024 at 20:38:40 UTC, Jonathan M Davis wrote: On Saturday, March 23, 2024 1:30:29 PM MDT Menjanahary R. R. via Digitalmars- d-learn wrote: [...] Well, when nextPrime is instantiated, the type of T is inferred from the function argument. So, if num is int, then T is int, whereas if num is const int, then T is const int. The same with isPrime. [...] Thanks for your prompt answer. It works like a charm. It's always a pleasure to learn from the Dlang community.
Re: Mutate immutable inside shared static constructor
On Saturday, 23 March 2024 at 21:59:57 UTC, Nick Treleaven wrote: On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it should be treated as illegal to ever assign to it - or to do anything else which would mutate it. So, clearly, the logic in static constructors with regards to non-mutable variables is overly simple at the moment. Thanks, filed: https://issues.dlang.org/show_bug.cgi?id=24449 Thanks for your prompt answer.
Mutability issue
The next code works as is but ... ``` import std.stdio; import std.traits; bool isPrime(T)(T n) if (isIntegral!T) { if (n <= T(3)) return n > T(1); if (n % T(2) == T(0) || n % T(3) == T(0)) return false; for (T candidate = T(5); candidate * candidate <= n; candidate += T(6)) { if (n % candidate == T(0) || n % (candidate + T(2)) == T(0)) { return false; } } return true; } T nextPrime(T)(T n) if (isIntegral!T) { if (n < T(2)) return T(2); T candidate = (n % T(2) == T(0)) ? n + T(1) : n + T(2); // Start from next Odd for (;; candidate += T(2)) { // Skip even if (isPrime(candidate)) { return candidate; } } } void main() { int num = 10; // Example starting number writeln("\nNext prime after ", num, " is ", nextPrime(num)); } ``` ... it doesn't at all once I change `int num = 10;` to `const int num = 10;`. I'm confused How to fix it?
Re: How to unpack a tuple into multiple variables?
On Wednesday, 7 February 2024 at 05:03:09 UTC, Gary Chike wrote: ... But overall it's an elegant way to indirectly add names to tuples in D for ergonomic access. Nice find! --- Refactored it a bit to have a ready to run and easy to grasp code. Enjoy! ```d import std.stdio : writefln; import std.typecons: tuple; // Name a Tuple's fields post hoc by copying the original's fields into a new Tuple. template named(names...) { auto named(T)(ref auto T t) if (names.length <= T.Types.length) => tuple!names(t.expand[0..names.length]); } // Define variables corresponding to a Tuple's fields in the current scope, // whose identifiers are given by `names`. mixin template Unpack(alias t, names...) if (names.length <= t.Types.length) { static foreach (i, n; names) mixin("auto ", n, " = t[i];"); } void main() { auto getUser() => tuple("John Doe", 32); // auto u = getUser().named!("name", "age"); writefln("%s (%d)", u.name, u.age); // John Doe (32) // You could also do this. with (getUser().named!("name", "age")) writefln("%s (%d)", name, age); // John Doe (32) // mixin Unpack!(u, "name", "age"); writefln("%s (%d)", name, age); // John Doe (32) } ```
Re: how can I load html files and not .dt or diet files in vibe.d
On Thursday, 1 February 2024 at 03:20:31 UTC, dunkelheit wrote: this is my code, I'm a begginer on vibe, and I want to use html and not diet files `import vibe.vibe; void main() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; auto listener = listenHTTP(settings, ); scope (exit) { listener.stopListening(); } logInfo("Please open http://127.0.0.1:8080/ in your browser."); runApplication(); } void hello(HTTPServerRequest req, HTTPServerResponse res) { res.render!"hola.dt"; }` I tried to load my html through iframe but it not works !!! 5 html body h1 hello diet -iframe("hola.html") please help me :') Explore this link https://github.com/D-Programming-GDC/gdcproject for potential sources of inspiration.