Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread mw via Digitalmars-d-learn
On Monday, 21 June 2021 at 06:04:56 UTC, Mike Parker wrote: On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote: i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) print(x) # [1, 2, 3, 4, 5] ``` Thanks. ```d x ~= [4, 5]; ``` Ha! great. I didn't

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn
On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote: Ha! great. I didn't know `~` works for both single elements and array! `~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append arrays or single elements.

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn
On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote: i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) print(x) # [1, 2, 3, 4, 5] ``` Thanks. ```d x ~= [4, 5]; ```

Re: do I incur a penality on compile time if I explicitly declare default behavior ?

2021-06-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/21/21 12:12 AM, someone wrote: I mean, coding as following: ```d int intWhatever = 0; /// default being zero anyway foreach (classComputer objComputer, objComputers) { ... } /// explicitly declaring the type instead of letting the compiler to figure it out struc Whatever {    public

Re: is there a way to: import something = app-xyz-classes-something; ?

2021-06-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/21/21 4:55 AM, frame wrote: On Monday, 21 June 2021 at 03:32:58 UTC, someone wrote: Since memory serves I use to name files with - instead of the more common _ The module name has to be strict and "-" is not allowed. However, you should be able to import files with a "-" in the name.

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:35:19 UTC, frame wrote: An associative array is not a range but a struct, so it is extra work to create a range from the AA to apply range functions. You can get a range from it by using something like std.array.byPair() but for this usage you would be better

Re: is there a way to: import something = app-xyz-classes-something; ?

2021-06-21 Thread frame via Digitalmars-d-learn
On Monday, 21 June 2021 at 13:29:59 UTC, Steven Schveighoffer wrote: It does work. However, you have to tell the compiler the file to compile. Which completely ignores filenames if the compiler is satisified. You may assume by reading the manual that the compiler would make an automatic

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:40:47 UTC, ag0aep6g wrote: `~` works just fine with single elements: void main() { import std.stdio; int[] a = [2, 3, 4]; writeln(1 ~ a ~ 5); /* [1, 2, 3, 4, 5] */ } Cool. I've had it in my head for many years now that this

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread wjoe via Digitalmars-d-learn
On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: I often need to iterate through a filtered collection (associative array) as following: ```d string strComputerIDunwanted = "WS2"; /// associative array key to exclude foreach (strComputerID, udtComputer; udtComputers) { ///

Re: do I incur a penality on compile time if I explicitly declare default behavior ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:06:06 UTC, frame wrote: Even if it would have an impact ... Furthermore, regardless of the impact, one of the pros of explicitly coding like this is to help future-portings of the base code to another language if need so, the team porting the code won't be

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Elronnd via Digitalmars-d-learn
On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: Is there a way to filter the collection at the foreach-level to avoid the inner if ? Here's how I would do it: foreach (k, v; coll) { if (k == unwanted) continue; ... } You still have an if, but the actual loop body doesn't have

Re: is there a way to: import something = app-xyz-classes-something; ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 13:29:59 UTC, Steven Schveighoffer wrote: It does work. However, you have to tell the compiler the file to compile. When an import is used, the compiler does 2 stages: 1. Search through already-seen modules, and see if one matches 2. Search the filesystem to find

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/21/21 5:00 PM, Elronnd wrote: On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: Is there a way to filter the collection at the foreach-level to avoid the inner if ? Here's how I would do it: foreach (k, v; coll) {     if (k == unwanted) continue;     ... } You still have an if,

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 22:08:56 UTC, Steven Schveighoffer wrote: It's actually visually shorter than doing the filter. Indeed; in a few very-specific situations I usually write code like this since it allows me to concentrate on the task at hand and not on the details to access the

Re: How to use readText to read utf16 file?

2021-06-21 Thread Emil Perhinschi via Digitalmars-d-learn
On Tuesday, 17 November 2015 at 13:00:11 UTC, Gary Willoughby wrote: On Tuesday, 17 November 2015 at 02:40:14 UTC, Domain wrote: How to use readText to read utf16 file? Or other encoding file. Here's a helpful resource when working with text files in D.

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 15:32:09 UTC, wjoe wrote: something like this ? ``` D import std.array; import std.algorithm; udtComputers.byPair .filter!(p => p.key != strComputerIDunwanted) .each!( (p) { /* foreach body */ } ); ``` This seems really interesting :) Almost

Re: do I incur a penality on compile time if I explicitly declare default behavior ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 13:23:04 UTC, Steven Schveighoffer wrote: For sure there is a difference in what the compiler has to do. Indeed. But I think the check is likely trivial, and inconsequential as far as compiler runtimes. D is going to already figure out the types of expressions

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 21:00:42 UTC, Elronnd wrote: Here's how I would do it: foreach (k, v; coll) { if (k == unwanted) continue; ... } You still have an if, but the actual loop body doesn't have to be nested, so it's easy to follow the control flow. almost the same

Re: do I incur a penality on compile time if I explicitly declare default behavior ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:06:06 UTC, frame wrote: Even if it would have an impact - it may change with a new compiler release. I personally use explicit declaration in a foreach loop, because the IDEs don't get the type and it cost me more time to figure out the method signature on

Re: Display a random image with vibe.d

2021-06-21 Thread Christian Köstlin via Digitalmars-d-learn
On 2021-06-20 17:14, vnr wrote: On Sunday, 20 June 2021 at 14:28:26 UTC, jfondren wrote: On Sunday, 20 June 2021 at 13:58:22 UTC, vnr wrote: Thanks for the answers, I understand better what is going on. So, what should I do to make my server respond with a random image, and not the random

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/21 10:36 PM, mw wrote: i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) print(x)  # [1, 2, 3, 4, 5] ``` Thanks. There is also std.range.chain, which can visit multiple ranges in sequence without copying elements. This is a lifesaver when

Re: is there a way to: import something = app-xyz-classes-something; ?

2021-06-21 Thread frame via Digitalmars-d-learn
On Monday, 21 June 2021 at 03:32:58 UTC, someone wrote: Since memory serves I use to name files with - instead of the more common _ The module name has to be strict and "-" is not allowed. However, you should be able to import files with a "-" in the name. From the manual: If the file name

Re: do I incur a penality on compile time if I explicitly declare default behavior ?

2021-06-21 Thread frame via Digitalmars-d-learn
On Monday, 21 June 2021 at 04:12:55 UTC, someone wrote: I mean, coding as following: Even if it would have an impact - it may change with a new compiler release. I personally use explicit declaration in a foreach loop, because the IDEs don't get the type and it cost me more time to figure

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread frame via Digitalmars-d-learn
On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: I often need to iterate through a filtered collection (associative array) as following: ```d string strComputerIDunwanted = "WS2"; /// associative array key to exclude foreach (strComputerID, udtComputer; udtComputers) { ///

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.06.21 09:02, Mike Parker wrote: On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote: Ha! great. I didn't know `~` works for both single elements and array! `~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/21 8:59 PM, someone wrote: I often need to iterate through a filtered collection (associative array) as following: ```d string strComputerIDunwanted = "WS2"; /// associative array key to exclude foreach (strComputerID, udtComputer; udtComputers) { /// ..remove!(a => a ==