Finding position of a value in an array
Reading documentation... Array, Algorithms, ... maybe I've been up too late... how does one obtain the index of, say, 55 in an array like this int[] a = [77,66,55,44]; I want to do something like: int i = a.find_value_returning_its_index(55); assert(i==2) I'm sure it's obvious but I'm not seeing it right now.
Re: Finding position of a value in an array
On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson wrote: Reading documentation... Array, Algorithms, ... maybe I've been up too late... how does one obtain the index of, say, 55 in an array like this int[] a = [77,66,55,44]; I want to do something like: int i = a.find_value_returning_its_index(55); assert(i==2) I'm sure it's obvious but I'm not seeing it right now. int i = a.countUntil!(v => v == 55); assert(i == 2); It might not be an obvious application of countUntil, but it's listed at https://dlang.org/phobos/std_algorithm_searching.html
Re: What type does byGrapheme() return?
On 2019-12-27 17:54:28 +, Steven Schveighoffer said: This is the rub with ranges. You need to use typeof. There's no other way to do it, because the type returned by byGrapheme depends on the type of Range. Hi, ok, thanks a lot and IMO these are the fundamental important information for people using D (beginners, causual programmers, etc.) to understand how things fit together. If you know what type Range is, it would be: struct S { typeof(string.init.byGrapheme()) gText; // or alias GRange = typeof(string.init.byGrapheme()); GRange gText; } Ah... I didn't know that I can use a basic type "string" combined with ".init" to manually build the type. Neat... Subbing in whatever your real range for `string`. Or if it's the result of a bunch of adapters, use the whole call chain with typeof. Ok, and these are good candidates for alias definitions to avoid re-typing it many times. Why not just declare the real range type? Because it's going to be ugly, especially if your underlying range is the result of other range algorithms. And really, typeof is going to be the better mechanism, even if it's not the best looking thing. I think I got it... thanks a lot. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: Finding position of a value in an array
On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote: On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson wrote: Reading documentation... Array, Algorithms, ... maybe I've been up too late... how does one obtain the index of, say, 55 in an array like this int[] a = [77,66,55,44]; I want to do something like: int i = a.find_value_returning_its_index(55); assert(i==2) I'm sure it's obvious but I'm not seeing it right now. int i = a.countUntil!(v => v == 55); assert(i == 2); A predicate isn’t required, countUntil accepts single elements: int i = a.countUntil(55);
Re: Finding position of a value in an array
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel wrote: On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote: On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson wrote: int i = a.countUntil!(v => v == 55); assert(i == 2); A predicate isn’t required, countUntil accepts single elements: int i = a.countUntil(55); I was just about to go looking for something like this. Thanks, guys.
Mergesort not working
This is not entirely a D question, but I'm not sure what about my mergesort implementation went wrong. T[] merge(T)(T[] arr1, T[] arr2) { T[] result; result.reserve(arr1.length + arr2.length); ulong arr1_idx = 0, arr2_idx = 0; while (arr1_idx < arr1.length && arr2_idx < arr2.length) result ~= arr1[arr1_idx] < arr2[arr2_idx] ? arr1[arr1_idx++] : arr2[arr2_idx++]; return result; } T[] sort(T)(T[] arr) { return arr.length < 2 ? arr : merge(sort(arr[0 .. $ / 2]), sort(arr[$ / 2 .. $])); } Given an array, it just returns a 1 length array. What's causing this?
Re: Mergesort not working
On Sunday, 29 December 2019 at 11:02:34 UTC, Adnan wrote: while (arr1_idx < arr1.length && arr2_idx < arr2.length) result ~= arr1[arr1_idx] < arr2[arr2_idx] ? arr1[arr1_idx++] : arr2[arr2_idx++]; Given an array, it just returns a 1 length array. What's causing this? This loop stops as soon as arr1 _or_ arr2 are exhausted. Then, merge() will wrongly discard the remainder of the array that is not yet exhausted. The templating is good! -- Simon
Re: What type does byGrapheme() return?
On 2019-12-27 19:44:59 +, H. S. Teoh said: Since graphemes are variable-length in terms of code points, you can't exactly *edit* a range of graphemes -- you can't replace a 1-codepoint grapheme with a 6-codepoint grapheme, for example, since there's no space in the underlying string to store that. Hi, my idea was that when I use a grapheme range, it will abstract away that graphemes consist of different sized code-points. And the docs at https://dlang.org/phobos/std_uni.html#byGrapheme show an example using this kind of range: auto gText = text.byGrapheme; gText.take(3); gText.drop(3); But maybe I need to get a better understanding of the ranges stuff too... If you want to add/delete/change graphemes, what you *really* want is to use an array of Graphemes: Grapheme[] editableGraphs; You can then splice it, insert stuff, delete stuff, whatever. When you're done with it, convert it back to string with something like this: string result = editableGraphs.map!(g => g[]).joiner.to!string; I played around with this approach... string r1 = "Robert M. Münch"; // Code-Units = 16 // Code-Points = 15 // Graphemes = 15 Grapheme[] gr1 = r1.byGrapheme.array; writeln(" Text = ", gr1.map!(g => g[]).joiner.to!string); // Text = obert M. Münch writeln("wText = ", gr1.map!(g => g[]).joiner.to!wstring); // wText = obert M. Münch writeln("dText = ", gr1.map!(g => g[]).joiner.to!dstring); // dText = obert M. Münch Why is the first letter missing? Is this a bug? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: What kind of Editor, IDE you are using and which one do you like for D language?
On Sat, 2019-12-28 at 22:01 +, p.shkadzko via Digitalmars-d-learn wrote: […] > p.s. I found it quite satisfying that D does not really need an > IDE, you will be fine even with nano. Java people said this and we got Eclipse, Netbeans, and IntelliJ IDEA, and many people were better Java (and Kotlin, Groovy, Clojure, Scala, etc.) programmers because they used them. C and C++ people said this and we got CLion, and many programmers were better C or C++ programmers because they used it. Go people said this and we got Goland, and many people were better programmers because they used it. Whilst many programmers are happy using 1970s approaches to programming using ed, ex, vi, vim, emacs, sublime text, atom, etc. Many programmers prefer using IDEs, and are better programmers for it. D programmers can use text editors if they want, but many D programmers would be much better D programmers for having a full IDE – which is, for me, why getting the D language plugin to CLion (and IntelliJ IDEA) is important for D traction. The more the D community advertise that IDEs are for wimps, the less likelihood that people will come to D usage. -- Russel. === Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Roadm: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk signature.asc Description: This is a digitally signed message part
Re: What kind of Editor, IDE you are using and which one do you like for D language?
On Sunday, 29 December 2019 at 14:41:46 UTC, Russel Winder wrote: The more the D community advertise that IDEs are for wimps, the less likelihood that people will come to D usage. It is so. And yet, I can't use Java or Scala without IDE and I tried. I believe the same is true for C++.
Mimicking a shell
Hi, Is there a way to forward all input and output from a shell? This implies that e.g. pressing the left arrow on the keyboard is immediately being forwarded to the shell and that the output from a shell would be *exactly* the same as output from my D program (that would include the prompt and VGA colouring). Kind regards, Jan
Re: Mimicking a shell
On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote: Is there a way to forward all input and output from a shell? yes, but it is platform specific and can be a decent amount of code. what OS are you on?
Re: What kind of Editor, IDE you are using and which one do you like for D language?
On Sunday, 29 December 2019 at 14:41:46 UTC, Russel Winder wrote: Whilst many programmers are happy using 1970s approaches Please. Have you actually spent the time to learn these systems in the last 40 years? My experience is IDEs are just different, not necessarily better or worse. Just different enough that people used to one find the others difficult to learn. why getting the D language plugin to CLion (and IntelliJ IDEA) is important for D traction. When I tried loading a D file in android studio, the IDE offered to auto install a D language plugin. I'm incompetent with IDEs so I can't speak to the quality of it, but it is there.
Re: Mimicking a shell
On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote: Hi, Is there a way to forward all input and output from a shell? This implies that e.g. pressing the left arrow on the keyboard is immediately being forwarded to the shell and that the output from a shell would be *exactly* the same as output from my D program (that would include the prompt and VGA colouring). Kind regards, Jan IMHO, this is kinda what you need: https://github.com/greenduck/shell-tunnel/blob/master/shell-tunnel.c Sorry, this code is in C, but converting it to D would be fairly straight forward.
Re: What kind of Editor, IDE you are using and which one do you like for D language?
On Sunday, 29 December 2019 at 14:41:46 UTC, Russel Winder wrote: Whilst many programmers are happy using 1970s approaches to programming using ed, ex, vi, vim, emacs, sublime text, atom, etc. Many programmers prefer using IDEs, and are better programmers for it. I don't think it's black and white as you make it out to be. When I was programming with JVM languages (Clojure, Scala, a bit of Java) I used Eclipse. I found that to be the right approach for those languages because everything is done in terms of projects. You can offload the management of your projects to the IDE. When I use D, I think in terms of individual files, and the cost-benefit analysis comes out in favor of opening the files in a text editor. This is not to say that there is anything wrong with using an IDE, it's simply a matter of preference. I have trouble seeing how an IDE is going to make anyone a better programmer. Perhaps a bit more productive, but carrying along the cost of using the IDE, as opposed to simply opening a file and typing. ed and vi have little in common with Emacs or Atom. Many C++ programmers have moved from using an IDE to using Vim, Emacs, and Atom the last few years. You might wish to check out this 10-minute presentation from the recent Emacs conf: https://media.emacsconf.org/2019/19.html It's definitely not 1970's technology.
Re: What kind of Editor, IDE you are using and which one do you like for D language?
On Sun, Dec 29, 2019 at 09:25:44PM +, Adam D. Ruppe via Digitalmars-d-learn wrote: > On Sunday, 29 December 2019 at 14:41:46 UTC, Russel Winder wrote: > > Whilst many programmers are happy using 1970s approaches > > Please. Have you actually spent the time to learn these systems in the > last 40 years? > > My experience is IDEs are just different, not necessarily better or > worse. Just different enough that people used to one find the others > difficult to learn. [...] Yeah, vim (and probably emacs -- I don't use it so can't speak for it) has come a *long* ways from its original vi roots. So has CLI development tools in general. I think it's very unfair to equate vim to the 1970's version of vi when comparing it to a GUI-based IDE. Generally, I find myself *much* more productive with CLI-based tools; IDEs are generally much heavier in terms of memory and CPU usage, and worst of all, require a GUI, which for me is a deal-breaker because I do a lot of work over SSH connections on not necessarily reliable networks. The amount of network traffic needed to operate a GUI over a remote desktop is just so much more than the much lighter weight of a few keystrokes that for me it's a very unproductive choice. That, plus the amount of RAM + CPU + disk investment needed just to get an IDE to even start, to me cannot even begin to compare to how few resources are needed to be highly productive with a bare-bones Vim installation. I just have a hard time justifying such an investment when what I get in return is so undesirable within my operational parameters. If I were forced to use an IDE, I would be tempted to just stop programming at all. It would certainly *not* make me a better programmer. On Mon, Dec 30, 2019 at 01:26:11AM +, bachmeier via Digitalmars-d-learn wrote: [...] > I have trouble seeing how an IDE is going to make anyone a better > programmer. [...] Yeah, I call BS on that statement. OTOH, it's certainly a valid point that IDE support needs to be good in order to appeal to that subset of programmers who prefer to work in an IDE. T -- Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.
Re: Concatenation/joining strings together in a more readable way
On Wednesday, 25 December 2019 at 13:07:44 UTC, mipri wrote: On Wednesday, 25 December 2019 at 12:39:08 UTC, BoQsc wrote: Are there any other ways to join two strings without Tilde ~ character? I can't seems to find anything about Tilde character concatenation easily, nor the alternatives to it. Can someone share some knowledge on this or at least point out useful links/resources? Huh? For clarity I'm going to respond to some potential rewrites of your question. I don't think "foo" ~ "bar" is very readable. Can I use a different syntax? No. That's the syntax. Even if you customize it in your own code, by overloading addition or the like, you'll still come across it in other people's code. So I can only recommend that you find some way to make it readable for you. Maybe use a different or larger font? Maybe just get more familiar with it? I don't think the syntax is likely to change. Where can I find documentation for the ~ operator? It's under 'expressions' in the spec: https://dlang.org/spec/expression.html#cat_expressions Is ~ the best way to join strings together? Can I use something else? You can use it to join arrays in general, including strings. Depending on your use case you might prefer joiner() https://dlang.org/phobos/std_algorithm_iteration.html#.joiner in usage like assert(["many", "strings"].joiner("").array == "manystrings"); Or Appender, for many mutating appends to the same array: https://dlang.org/phobos/std_array.html#Appender Use Python format() style: import std; import std: Format = format; // format() string format(T...)(T text){ string texto = text[0]; foreach(count, i; text[1..$]){ texto = texto.replaceFirst("{}", to!string(i)); texto = texto.replace("{%s}".Format(to!string(count)), to!string(i)); } return texto; } void main() { string name = "Marcone"; writeln("Helo {}".format(name)); // Helo Marcone writeln("Helo {0}".format(name)); // Helo Marcone }