Re: How to generate a random number from system clock as seed

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 8 June 2024 at 18:25:20 UTC, drug007 wrote: ```d { const seed = castFrom!long.to!uint(Clock.currStdTime); auto rng = Random(seed); auto result = generate!(() => uniform(0, 10, rng))().take(7); // new random numbers sequence every time

Re: How to generate a random number from system clock as seed

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote: I just want the number of seconds elapsed since jan 1st 1970. In other words, the internal system clock value. #unix #time @SDB79

Re: modInverse & powMod

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 7 June 2024 at 13:43:29 UTC, Salih Dincer wrote: SDB@79 I have only one question: Is there a modInverse() function in the standard library for use in RSA? Apparently not, it fell to lot :) I already had such a function... ```d auto modInverse(T)(T m, T n) pure { T q, ilk = n;

modInverse & powMod

2024-06-07 Thread Salih Dincer via Digitalmars-d-learn
I know there is modular exponentiation [std.math.exponential.powmod](https://dlang.org/library/std/math/exponential/powmod.html) in the standard library.While it works great in Pyrhon (even with very large numbers), it doesn't work with signed numbers in D. That's why I turned to the alternative

Re: FIFO

2024-05-14 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 13 May 2024 at 15:07:39 UTC, Andy Valencia wrote: On Sunday, 12 May 2024 at 22:03:21 UTC, Ferhat Kurtulmuş wrote: https://dlang.org/phobos/std_container_slist.html This is a stack, isn't it? LIFO? Ahh yes. Then use dlist Thank you. I read its source, and was curious so I wrote

Re: FIFO

2024-05-13 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 13 May 2024 at 15:07:39 UTC, Andy Valencia wrote: Representing the FIFO as a linked list clearly has its cost, but I found the increased system time interesting. OS memory allocations maybe? I know you want FIFO, I usually keep this on hand for fixed size LIFO; It can easily

Re: Challenge Tuples

2024-05-02 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 1 May 2024 at 14:15:19 UTC, Andrey Zherikov wrote: Shorter and without allocations: ```d import std.typecons : tuple; import std.algorithm : sum, each; auto sum(int i) => i; void main() { auto t = tuple(1, 2, 3, [1, 3], 5); int res=0; t.each!(e => res += sum(e));

Re: Challenge Tuples

2024-04-27 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 27 April 2024 at 15:36:40 UTC, Nick Treleaven wrote: On Saturday, 27 April 2024 at 15:32:40 UTC, Nick Treleaven wrote: On Saturday, 27 April 2024 at 11:55:58 UTC, Basile B. wrote: foreach const e in u do if echo(is, e, T) do result += e;

Challenge Tuples

2024-04-26 Thread Salih Dincer via Digitalmars-d-learn
You have a 5-item data tuples as Tuple(1, 2, 3, [1, 3], 5) and implement the sum (total = 15) with the least codes using the sum() function of the language you are coding... Let's start with D: ```d import std.typecons : tuple; import std.algorithm : sum; void main() { auto t = tuple(1,

Re: Adapting foreign iterators to D ranges

2024-04-23 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 23 April 2024 at 06:02:18 UTC, cc wrote: Just to offer an alternative solution (since it sometimes gets overlooked), there is also the `opApply` approach. You don't get full forward range status, and checking whether it's empty essentially requires doing something like

Re: Inconsistent chain (implicitly converts to int)

2024-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 6 April 2024 at 09:21:34 UTC, rkompass wrote: I checked: ```d import std.stdio, std.range, std.algorithm; struct N(T) { T last, step, first; bool empty() => first >= last; T front() => first; auto popFront() => first += step; } void main() { auto r1 =

Re: CTFE write message to console

2024-04-05 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 5 April 2024 at 14:41:12 UTC, Carl Sturtivant wrote: On Friday, 5 April 2024 at 07:37:20 UTC, Paolo Invernizzi wrote: pragma(msg, x) ? No. `__ctfeWrite(x)` is executed inside an executing function like any other statement in it, and can have an argument `x` computed during that

Re: Inconsistent chain (implicitly converts to int)

2024-04-05 Thread Salih Dincer via Digitalmars-d-learn
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 you say! I even tried it on an older version and got the same

Re: Inconsistent chain (implicitly converts to int)

2024-04-05 Thread Salih Dincer 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 int

Inconsistent chain (implicitly converts to int)

2024-04-05 Thread Salih Dincer via Digitalmars-d-learn
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 int? ```d import std.stdio, std.range; void main() { auto r1 = N!size_t(10, 1, 1); auto r2 = N!real(15, .5, 10);

Re: How to add a character literal to a string without ~ operator?

2024-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 4 April 2024 at 19:56:50 UTC, Ferhat Kurtulmuş wrote: My favorite d feature is lazy ranges. No allocation here. ```d auto s = chain("as ", "df ", "j"); // s is lazy writeln(s); ``` ```d import std.range : chain; void main() { string word = "hello"; auto noError = chain(word,

Re: How to add a character literal to a string without ~ operator?

2024-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 4 April 2024 at 18:14:54 UTC, BoQsc wrote: I'm looking for more readable standard function to add a **character** literal to a **string**. The `~` operator is clearly not great while reading a source code. I'm not here to discuss that. I'm looking for a function inside standard

Re: CTFE write message to console

2024-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 4 April 2024 at 15:47:53 UTC, Richard (Rikki) Andrew Cattermole wrote: Oh hey! https://github.com/dlang/dmd/pull/16250 It was implemented literally 2 weeks ago! Nightly should have it https://github.com/dlang/dmd/releases/tag/nightly Good news, thanks... SDB@79

Re: Setting up a final switch from a list

2024-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 29 March 2024 at 00:37:21 UTC, Jonathan M Davis wrote: On Thursday, March 28, 2024 4:21:03 PM MDT Salih Dincer via Digitalmars-d- learn wrote: How can we add all members of an enum type to a list without duplicating code? As the documentation for EnumMembers explains, you can use

Re: Why is this code slow?

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 29 March 2024 at 00:04:14 UTC, Serg Gini wrote: On Thursday, 28 March 2024 at 23:15:26 UTC, Salih Dincer wrote: There is no such thing as parallel programming in D anyway. At least it has modules, but I didn't see it being works. Whenever I use toys built in foreach() it always ends

Re: Two chunks but No allocation

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 28 March 2024 at 23:08:54 UTC, rkompass wrote: You can drop and take from the folded values range. I got `[1, 0.67, 0.625, 0.619048, 0.618182, 0.618056, 0.618037, 0.618034, 0.618034, 0.618034]` from the above code. Thank you so much... I solved the problem: r.back doesn't

Re: range.chunks(2) error

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 28 March 2024 at 17:50:17 UTC, Salih Dincer wrote: Hi, When I use the chunks() template with iota(), for instance, with chunks(2), I can access both r.front and r.back. However, in a range of my own type (named iras in the code below), only r.front is working. I think the error

Re: Why is this code slow?

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 28 March 2024 at 20:18:10 UTC, rkompass wrote: I didn't know that OpenMP programming could be that easy. Binary size is 16K, same order of magnitude, although somewhat less. D advantage is gone here, I would say. There is no such thing as parallel programming in D anyway. At

Re: Setting up a final switch from a list

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 10 August 2023 at 08:33:13 UTC, Christian Köstlin wrote: I think one part of the original question (now fanished in the nntp backup) was how to get all enum members into a list without duplicating code. ```d import std.traits : EnumMembers; import std.stdio : writeln; import

range.chunks(2) error

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
Hi, When I use the chunks() template with iota(), for instance, with chunks(2), I can access both r.front and r.back. However, in a range of my own type (named iras in the code below), only r.front is working. I think the error given by r.back is not a bug related to chunks, is it? ```d

Re: Why is this code slow?

2024-03-28 Thread Salih Dincer via Digitalmars-d-learn
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 OpenMP, which provides a set of compiler directives and runtime

Re: Two chunks but No allocation

2024-03-27 Thread Salih Dincer via Digitalmars-d-learn
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 ugly and I'm sure there must be a better way? ```d import

Re: Why is this code slow?

2024-03-27 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 27 March 2024 at 08:22:42 UTC, rkompass wrote: I apologize for digressing a little bit further - just to share insights to other learners. 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

Two chunks but No allocation

2024-03-27 Thread Salih Dincer via Digitalmars-d-learn
Is it possible to process both chunks without requiring memory allocation (not using an array)? For example: ```d import std; void main() { auto fib = (real a, real b) => recurrence!"a[n-1] + a[n-2]"(a, b); auto myFib = fib(1, 1).take(48).array; auto goldenRadio =

Re: Why is this code slow?

2024-03-26 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 March 2024 at 14:02:08 UTC, rkompass wrote: Of course you may also combine the up(+) and down(-) step to one: 1/i - 1/(i+2) = 2/(i*(i+2)) ```d double leibniz(int iter) { double n = 0.0; for (int i = 1; i < iter; i+=4) n += 2.0 / (i * (i+2.0)); return n * 4.0; } ```

Re: Why is this code slow?

2024-03-24 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 24 March 2024 at 22:16:06 UTC, Kdevel wrote: The term containing the `pow` invocation computes the alternating sequence -1, 1, -1, ..., which can be replaced by e.g. ```d immutable int [2] sign = [-1, 1]; n += sign [i & 1] / (i * 2.0 - 1.0); ``` This saves the expensive call

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-13 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 13 March 2024 at 10:27:49 UTC, Basile B. wrote: The semantics of the operators are actually not as clear as that. What if you define ```d enum Direction { N = 1, NE, S = 45, SW } ``` ? Certainly! EnumMembers; can be used. The EnumMembers template from the std.traits

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-12 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 12 March 2024 at 05:38:03 UTC, Liam McGillivray wrote: Perhaps this would be a good programming challenge for someone more experienced than me. Make a data type (probably a struct) for holding one of 8 directional values using 3 bits. It should accept the use of increment operators

Re: struct initializer

2023-12-01 Thread Salih Dincer via Digitalmars-d-learn
Hi All, I feel lonely, just as those who come from C++ find it strange, because I think it makes it difficult to read code. On Friday, 1 December 2023 at 14:53:16 UTC, Paul Backus wrote: Technically you don't *have* to repeat the type. You can write the return type as `auto`: ```d auto

Re: Convert String to Date and Add ±N Hours

2023-11-07 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 4 November 2023 at 21:10:38 UTC, Steven Schveighoffer wrote: On Saturday, 4 November 2023 at 18:11:53 UTC, Vahid wrote: Hi, I have a date string with the format of "2023-11-04 23:10:20". I want to convert this string to Date object and also, add ±N hours to it. For example:

Re: bigEndian in std.bitmanip

2023-11-02 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 31 October 2023 at 14:43:43 UTC, Imperatorn wrote: It might make sense to change since little endian is the most common when it comes to hardware. But big endian is most common when it comes to networking. So I guess it depends on your view of what is most common. Interacting with

Re: Function Overloading

2023-11-02 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote: Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct. Yeah, it works! Thanks...:) SDB@79

Function Overloading

2023-10-31 Thread Salih Dincer via Digitalmars-d-learn
```d struct Calculate {   int memory; string result;    auto toString() => result;    this(string str)    {        add(str);    }    this(int num) {        add(num); } import std.string : format;    void add(string str)    {        result ~= str.format!"%s + ";  

Re: bigEndian in std.bitmanip

2023-10-31 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 31 October 2023 at 10:24:56 UTC, Jonathan M Davis wrote: On Tuesday, October 31, 2023 4:09:53 AM MDT Salih Dincer via Digitalmars-d- learn wrote: Hello, Why isn't Endian.littleEndian the default setting for read() in std.bitmanip? Why would you expect little endian

bigEndian in std.bitmanip

2023-10-31 Thread Salih Dincer via Digitalmars-d-learn
Hello, Why isn't Endian.littleEndian the default setting for read() in std.bitmanip? Okay, we can easily change this if we want (I could use enum LE in the example) and I can also be reversed with data.retro.array(). ```d void main() { import std.conv : hexString; string helloD =

Re: SlicedString Length

2023-10-20 Thread Salih Dincer via Digitalmars-d-learn
Good news! I collected them in a template with the condition of running split() once at compile time. The result seems perfect? What are your thoughts? ```d void main() { auto slc = sliceOff!"abc def ghi"; auto len = slc.length; slc.index = len; import std.stdio, std.algorithm:

SlicedString Length

2023-10-20 Thread Salih Dincer via Digitalmars-d-learn
Hi, I have a string wrapper called SlicedString. What it does is very simple: Returning each piece as a slice with a assured separator at compile time by used indexOf() ! Here it is: ```d struct SlicedString(string E) { long index, back, next = -1; string data; auto length() const {

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-17 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 15 October 2023 at 07:22:53 UTC, Imperatorn wrote: You already got a lot of good answers, I thought I'd just share this for anyone searching for nogc string formatting compatible with betterC: https://code.dlang.org/packages/bc-string Doesn't it make more sense to use

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 9 October 2023 at 16:33:32 UTC, rempas wrote: I'm trying to create a series of function. There will be ten of them, and they will be called `function_0`, `function_1`, etc. However, in my example, "stringof" returns the character "i" itself and turns that into a string instead of

Re: Type constraint

2023-10-09 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 14:35:31 UTC, Joel wrote: Oh, I found, ```d static if (isIntegral!T) ``` seems to work. If you are using a struct, another way to affect the whole is as follows: ```d struct S(T) {  invariant() {      static assert(isIntegral!T); }  auto addUp() { /**/ }

Re: Type constraint

2023-10-09 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 8 October 2023 at 10:09:02 UTC, IchorDev wrote: On Wednesday, 4 October 2023 at 01:46:42 UTC, Joel wrote: I think the if without static is still static, since it's part of the function name part, or so (outside of the curly bracket scope). You can't have regular if-statements

Re: how to assign multiple variables at once by unpacking array?

2023-10-08 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 9 October 2023 at 01:15:21 UTC, Salih Dincer wrote: the staticMapN() template implemented by Ali Çehreli, which is not in the standard library, is needed: It would be great if the unlimited version was added to std.meta. This template seeded/sprouted in here:

Re: how to assign multiple variables at once by unpacking array?

2023-10-08 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 7 October 2023 at 16:12:47 UTC, mw wrote: Interesting: in terms of easy of coding, clarity and future maintenance, which one is superior? The one liner in Python, or your "solution" with dozen lines of code? BTW, is that a solution at all? Did it achieved what the original goal

Re: how to assign multiple variables at once by unpacking array?

2023-10-07 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 7 October 2023 at 07:31:45 UTC, mw wrote: https://stackoverflow.com/questions/47046850/is-there-any-way-to-assign-multiple-variable-at-once-with-dlang How to do this Python code in D: ``` s = "1 2 3" A,B,C = map(int, s.split(" ")) A,B,C (1, 2, 3) ``` Is there a better way

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-05 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 5 October 2023 at 16:40:49 UTC, Gaurav Negi wrote: Well, in the D programming language, both opIndex and opSlice are two different operators used to access elements of a custom type. Yeah, D is on its way to becoming a near-perfect programming language... ```d enum

Re: opIndexAssign

2023-10-05 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 5 October 2023 at 12:00:22 UTC, Timon Gehr wrote: void opIndexAssign(int value, int index){ i[index] = value; } In this case I need to define many operator overloads. For example (+=), this won't work without returns ref: ```d s[1] += 3;  assert(s.i == [2, 45]); // Error:

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 18:09:55 UTC, Imperatorn wrote: At the very least, the spec should do a better job of documenting when the compiler will try a fallback and when it won't. Who will be the hero and add the documentation?  More importantly, is there a priority order? Because

Re: Key and value with ranges

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:20:44 UTC, Joel wrote: I want the output sorted by value. Look: https://forum.dlang.org/post/qjlmiohaoeolmoavw...@forum.dlang.org ```d struct SIRALA(T) { } ``` You can use SIRALA(T). Okay, his language is Turkish but our common language is D. His feature

opIndexAssign

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
Hi, opIndexAssign, which is void, cannot compromise with opIndex, which is a ref! Solution: Using opSliceAssign. Could this be a bug? Because there is no problem in older versions (e.g. v2.0.83). ```d struct S {  int[] i;  ref opIndex(size_t index) => i[index];  auto opSliceAssign/*

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:42:14 UTC, Paul Backus wrote: I don't know what's wrong in your example but this works for me: I found the reason for the error: https://forum.dlang.org/thread/vckvftkdzcrnikudu...@forum.dlang.org SDB@79

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 16:05:39 UTC, Paul Backus wrote: `T[] opSlice()` is the D1 version and exists only for backwards compatibility. You should use `T[] opIndex()` in new code. Forgive me for asking again, I think opSliceAssign(T value) has also been improved, right? ```d //

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:01:34 UTC, Jonathan M Davis wrote: For most code, you'd just write an opIndex with a single parameter for indexing an element, opSlice with two parameters for slicing the range or container, and then either opIndex or opSlice with no parameters to return a

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: Also, is it correct to use [] when returning? Thanks... [Here](https://dlang.org/spec/operatoroverloading.html#slice), the opIndex() is proposed and an example of parameterized the opSlice() is given for multidimensional

The difference between T[] opIndex() and T[] opSlice()

2023-10-01 Thread Salih Dincer via Digitalmars-d-learn
Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? ```d struct S(T) {    T[] arr;        T[] opIndex() => arr[];/*    T[] opSlice() => arr;//*/ } alias Type = int; void main() {    auto s = S!Type([1,2,3]);    auto arr = s[]; // calls

Re: Dinamyc arrays

2023-09-17 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 17 September 2023 at 17:15:34 UTC, Timofey wrote: I`ve just started learning d and have a question. What should I write to set dinamyc rectangular array length in both dimentions? For example, I have declareted an array: ``` int[][] matrix;``` and want set it as n*n matrix. Thanks

Re: Setting struct as default parameter of a function using struct literal?

2023-09-12 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 11 September 2023 at 23:47:33 UTC, H. S. Teoh wrote: Since the type of the parameter is already known, the compiler does not need me to repeat the type name. It already knows enough to figure it out on its own. "Don't Repeat Yourself" (DRY). I think there are 3 possibilities,

Re: Setting struct as default parameter of a function using struct literal?

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 11 September 2023 at 22:13:25 UTC, H. S. Teoh wrote: Because sometimes I want a specific type. it's possible... ```d alias ST = Options; void specificType(ST option = ST()) { if(option) { assert(false); } else assert(true); } void main() { specificType(); // No

Re: Setting struct as default parameter of a function using struct literal?

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 11 September 2023 at 20:17:09 UTC, H. S. Teoh wrote: Someone should seriously come up with a way of eliminating the repeated type name in default parameters. Why not allow it to be flexible enough by using a template parameter? ```d enum Options : bool { silenceErrorsOff,

Re: Setting struct as default parameter of a function using struct literal?

2023-09-11 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 11 September 2023 at 17:51:04 UTC, BoQsc wrote: Here is an example of what I would hope for to work but it surely does not work: If I were you I would use enum, look at my code: ```d enum Options { silenceErrors = false } void someFunction (Options option =

Re: class Object with Dependency Injection

2023-06-18 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 18 June 2023 at 16:58:15 UTC, Ali Çehreli wrote: The problem is with the deduced type of 'services'. I don't know the mechanism behind it but the common type of 'truck' and 'ship' are deduced to be Object. Apparently, their interfaces don't take part in that decision. I don't know

class Object with Dependency Injection

2023-06-18 Thread Salih Dincer via Digitalmars-d-learn
Hi, below is an example of DI-dependency injection with 3 versions nested in the code. If you remove the leading // characters, you will not get the "no property `deliver` for `service` of type `object.Object`" error. Because version-2I with interface wants its methods to depend on Object..

Re: byte and short data types use cases

2023-06-10 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 9 June 2023 at 23:51:07 UTC, Basile B. wrote: Yes, a classsic resource is http://www.catb.org/esr/structure-packing/ So you can optimize memory usage by using arrays of things smaller than `int` if these are enough for your purposes, So, is the sorting correct in a structure like

Re: Proper way to handle "alias this" deprecation for classes

2023-05-17 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 17 May 2023 at 08:00:17 UTC, Dom DiSc wrote: If you want auto-conversion, you should be more explicit: ```d float opCast() { } ``` because if you return "auto" it is not the highest-prio fit and therefore not chosen. If you have multiple choices, you still don't need to use

Re: Proper way to handle "alias this" deprecation for classes

2023-05-12 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 7 May 2023 at 21:04:05 UTC, Inkrementator wrote: Open question to everybody: What you're opinion on using opCast for this? Since it's a type conversion, it seems fitting to me. Can't converting without explicitly specifying in D is a big shortcoming in my opinion. There is such a

Re: Getting a total from a user defined variable

2023-04-21 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 21 April 2023 at 05:23:14 UTC, Joel wrote: Or: p.map!"a.age".sum; works too. If it was me I would use each(). But D is such a powerful language that you have many alternatives: ```d import std.algorithm, std.range, std.stdio; struct Person { string name; int age; } auto

Re: Variable length arrays under -betterC?

2023-04-17 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 17 April 2023 at 19:12:20 UTC, user456 wrote: ... but you'll have to implement - ctors - concat assign - slice assign - copy construction - value type elem alignment - default elem construction - default elem destruction - etc. to make that usable in on trivial cases. I understand

Re: vibe.db.postgresql: Example not working in Windows

2023-04-14 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 14 April 2023 at 17:06:36 UTC, Vino wrote: I was just trying the new package vibe.db.postgresql and the example provided is not working, can some one provide me an working example. Step performed ``` dub init dub add vibe-d-postgresql copy the example program to source/app.d dub

Re: Unresolvable dependencies to package

2023-04-14 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 14 April 2023 at 20:30:56 UTC, el machine code wrote: so my question why am i'm getting this error and how do i fix this? As far as I know imGUI works with SFML. First, please appear a Japanese flag on your screen:

Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-14 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 14 April 2023 at 12:26:25 UTC, Ki Rill wrote: Yay! That worked! Now I have a working example. Though it's strange that it does not work with shared libs. Good luck, I'm really happy for you... Ki Rill, wait a minute! Actually, i've been very happy for the D community. Because I

Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-13 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 14 April 2023 at 00:28:53 UTC, Ki Rill wrote: ``` LINK : fatal error LNK1104: cannot open file 'libucrt.lib' Error: linker exited with status 1104 ``` Why does it require this library and where can I find it? Since this library is a component of the Microsoft C Runtime (CRT)

Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 13:09:07 UTC, Ali Çehreli wrote: Not every type is null'able but nullable. ;) So, a design may use the following: https://dlang.org/library/std/typecons/nullable.html I implemented Handler into the Voldermort build, which Walter loved so much. For convenience,

Re: Assocative array lookup for object

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 04:57:58 UTC, Salih Dincer wrote: I think you want to do an encapsulation like below. ```d auto opIndex(string key)    => *(key in data); ``` I made a little mistake and I'll fix it before someone rub nose in it :) ```d auto opIndex(string key) {

Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-12 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 11 April 2023 at 10:24:09 UTC, Ki Rill wrote: My `dub.json`: ```Json { "authors": [ "rillki" ], "copyright": "Copyright © 2023, rillki", "dependencies": { "bindbc-sfml": "~>1.0.2" }, "description":

Re: Assocative array lookup for object

2023-04-11 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 01:16:17 UTC, Chris Katko wrote: Should I be using opEquals? Or something different? The problem with 'alias this' here is I want to wrap access to the insides with getter functions that do various things like logging and error checking. I think you want to do

Re: How to setup D with SFML? (using bindbc-sfml)

2023-04-08 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 8 April 2023 at 23:40:32 UTC, Mike Parker wrote: Not without error messages. The first thing you should do is use the error API in bindbc.loader to print them out. That should tell you what the problem is. I installed it on my Linux system without using a loader and with static

Re: member func is best choice for pointers?

2023-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 6 April 2023 at 14:26:01 UTC, a11e99z wrote: member func has invariant that **this** is not null (programmer thinks so). global func hasn't the one and programmer should check for it before doing something ... I understand what you mean. When you try to access the last node of

Re: constant pointer failing to compile

2023-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 6 April 2023 at 00:46:26 UTC, Josh Holtrop wrote: ``` constarrptr.d(5): Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(ubyte)*)data` ``` Why? And how can I do the equivalent to what I have been doing in C? I want these pointers to be generated at

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 4 April 2023 at 16:22:29 UTC, Steven Schveighoffer wrote: On 4/4/23 11:34 AM, Salih Dincer wrote: On Tuesday, 4 April 2023 at 14:20:20 UTC, Steven Schveighoffer wrote: parallel is a shortcut to `TaskPool.parallel`, which is indeed a foreach-only construct, it does not return a

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 4 April 2023 at 14:20:20 UTC, Steven Schveighoffer wrote: parallel is a shortcut to `TaskPool.parallel`, which is indeed a foreach-only construct, it does not return a range. I think what you want is `TaskPool.map`: ```d // untested, just looking at the taskPool.map!(/* your map

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 3 April 2023 at 22:24:18 UTC, Steven Schveighoffer wrote: So for example, if you have: ```d foreach(i; iota(0, 2_000_000).parallel) { runExpensiveTask(i); } ``` The foreach is run on the main thread, gets a `0`, then hands off to a task thread `runExpensiveTask(0)`. Then it gets

Re: short guide on getting started with D

2023-04-03 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 3 April 2023 at 07:29:01 UTC, cgenie wrote: Thanks, you got my attention... It has come to my attention that you talked about this: https://mesonbuild.com/Dlang-module.html SDB@79

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-02 Thread Salih Dincer via Digitalmars-d-learn
On Sunday, 2 April 2023 at 04:34:40 UTC, Salih Dincer wrote: I haven't seen rsFirst256 until now... **Edit:** I saw, I saw :) I am struck with consternation! I've never seen these results before. Interesting, there is such a thing as parallel threading :) Here are my skipPoints: ```d

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-01 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 1 April 2023 at 22:48:46 UTC, Ali Çehreli wrote: On 4/1/23 15:30, Paul wrote: > Is there a way to verify that it split up the work in to tasks/threads > ...? It is hard to see the difference unless there is actual work in the loop that takes time. I always use the Rowland

Re: InputRange in arrays

2023-03-30 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 31 March 2023 at 04:15:24 UTC, Salih Dincer wrote: ...has it always been possible for arrays to be used as if they were ranges? Playground is a very useful tool, it responds right away :) Up to 2.078.3: Failure with output: - onlineapp.d(4): Error: no property

Re: InputRange in arrays

2023-03-30 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 31 March 2023 at 03:39:51 UTC, Salih Dincer wrote: May be somewhere D compiler between v2.087 and v2.096 (works) but definitely not before v2.087 (including)... Edit: The Phobos library I'm using is corrupted. That's why the last line was giving an error. But has it always been

InputRange in arrays

2023-03-30 Thread Salih Dincer via Digitalmars-d-learn
Hi, there is a feature when working with the InputRange. I don't know when it released available. May be somewhere D compiler between v2.087 and v2.096 (works) but definitely not before v2.087 (including)... Anyone know this? ```d import std.range.primitives; void main() { auto arr = [0,

Re: The Phobos Put

2023-03-30 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 30 March 2023 at 13:27:33 UTC, Steven Schveighoffer wrote: But you can do `dig.copy(buf[])` since a dynamic array is. Apparently, we will not be able to get rid of the necessity of using slices.  Neither with "copy" nor with "put"... ```d import std.algorithm.mutation : copy;

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:50:04 UTC, Steven Schveighoffer wrote: On 3/29/23 4:29 PM, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked

Re: How to debug and watch globals in windows debugger?

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 21:09:57 UTC, ryuukk_ wrote: Also it's more of an accumulation of problems that makes me very annoyed https://issues.dlang.org/show_bug.cgi?id=20737 This for example shouldn't be a thing in 2023, period I agree with you because it's such a simple problem and

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:29:24 UTC, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked about originally. Yes, that was it, but even

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 19:49:47 UTC, Ali Çehreli wrote: On 3/29/23 12:21, ag0aep6g wrote: > As far as I understand, you're saying that we cannot overload on `ref`. > But we can. Salih's code demonstrates just that. > > void f(ref int x) {} > void f(int x) {} > void main() { int x; f(x);

Re: The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 15:01:27 UTC, Ali Çehreli wrote: On 3/29/23 04:48, Dennis wrote: On the other hand, Phobos's put() works with any OutputRange so it has to take a 'ref' to advance to know where it is left off. This behavior makes its use with slices weird but sometimes such is

The Phobos Put

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
Why does my `put` work but the Phobos `put` doesn't work with a slice? onlineapp.d(11): Error: none of the overloads of template `std.range.primitives.put` are callable using argument types `!()(int[], int[])` /dlang/dmd/linux/bin64/../../src/phobos/std/range/primitives.d(386):

The Phobos put()

2023-03-29 Thread Salih Dincer via Digitalmars-d-learn
Why does my `put` work but the Phobos `put` doesn't work with a slice? onlineapp.d(11): Error: none of the overloads of template `std.range.primitives.put` are callable using argument types `!()(int[], int[])` /dlang/dmd/linux/bin64/../../src/phobos/std/range/primitives.d(386):

Re: Step by step tutorials for using bindings in D

2023-03-26 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 27 March 2023 at 00:06:36 UTC, Inkrementator wrote: PS: To really understand what is happening, you might want to try manually compiling a hello world program that depends on a library instead of using dub. Some pointers: `dub build -v` will print out the compiler and linkflags

  1   2   3   4   >