Re: Key and value with ranges

2023-10-02 Thread Joel via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: ```d import std; auto data=“I went for a walk, and fell down a hole.”; ``` [snip] How can I improve this code? Like avoiding using foreach. This works for me: ```d import std; auto data="I went for a walk, and fell down a hole.";

Re: Key and value with ranges

2023-10-02 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 2 October 2023 at 18:46:14 UTC, christian.koestlin wrote: On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: How can I improve this code? Like avoiding using foreach. You could fold into a hash that counts the occurrences like that: ```d import std.uni : toLower; import

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

Re: Key and value with ranges

2023-10-02 Thread Joel via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: ```d import std; auto data=“I went for a walk, and fell down a hole.”; void main(string[] args) { int[string] dic; struct WordCnt { string word; ulong count; string toString() const { return

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: Define a new custom operator in D Language.

2023-10-02 Thread bachmeier via Digitalmars-d-learn
On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote: Overloading seems to only overload behaviour of existing operator, like: ``` + - * / % ^^ & | ^ <<>>>>>~ in ``` I'm unable to see how the operator overloading

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

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:34:11 UTC, Salih Dincer wrote: In an old version (for example, v2.0.83), the code you implemented in the places where Slice is written above works as desired. In the most current versions, the parameterized opIndexAssign(T value) gives the error:

Re: Define a new custom operator in D Language.

2023-10-02 Thread Imperatorn via Digitalmars-d-learn
On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote: On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote: On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote: [...] https://dlang.org/spec/operatoroverloading.html#binary Overloading seems to only overload behaviour of

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: Key and value with ranges

2023-10-02 Thread Joel via Digitalmars-d-learn
On Monday, 2 October 2023 at 06:19:29 UTC, Imperatorn wrote: On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: ```d import std; auto data=“I went for a walk, and fell down a hole.”; You can improve it further by inlining ```d import std; auto data = "I went for a walk, and fell down a

Re: Define a new custom operator in D Language.

2023-10-02 Thread BoQsc via Digitalmars-d-learn
On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote: On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote: Here is my issue: I've found a formula on Wikipedia. It's called **Hashing by division**. ![](https://i.imgur.com/UJPAWIW.png) As you can see it uses **mod** keyword to achieve

Re: Key and value with ranges

2023-10-02 Thread christian.koestlin via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: How can I improve this code? Like avoiding using foreach. You could fold into a hash that counts the occurrences like that: ```d import std.uni : toLower; import std.array : split, array; import std.stdio : writeln; import std.algorithm :

Re: Define a new custom operator in D Language.

2023-10-02 Thread Imperatorn via Digitalmars-d-learn
On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote: Here is my issue: I've found a formula on Wikipedia. It's called **Hashing by division**. ![](https://i.imgur.com/UJPAWIW.png) As you can see it uses **mod** keyword to achieve the modulus operation. In D language we use modulus

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

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? `T[] opSlice()` is the D1 version and exists only for backwards compatibility. You should use `T[] opIndex()` in new code.

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: Key and value with ranges

2023-10-02 Thread Imperatorn via Digitalmars-d-learn
On Monday, 2 October 2023 at 02:47:37 UTC, Joel wrote: ```d import std; auto data=“I went for a walk, and fell down a hole.”; You can improve it further by inlining ```d import std; auto data = "I went for a walk, and fell down a hole."; void main(string[] args) { int[string] dic;