digitalmars.D.learn - How can I use UFCS for a loop
- Tim (4/10) Jan 25 2021 Hi all,
- Q. Schroll (8/18) Jan 25 2021 Use std.array.array (alias: std.range.array) to make the range
- Q. Schroll (4/8) Jan 25 2021 Unless you have a good reason, use a slice and not a static array:
- Tim (4/15) Jan 25 2021 Why would I need to use a slice instead of a static array? I'm
- Jesse Phillips (4/22) Jan 25 2021 In that case, maybe this untested code
Hi all, How can I change the following to a more D-like approach by using UFCS?double[3] result; Json json = res.readJson; for(int i = 0; i < json.length; i++){ result[i] = json[i].to!double; }I'd prefer to do something like:result = res.readJson[].map!(to!double);
Jan 25 2021
On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote:Hi all, How can I change the following to a more D-like approach by using UFCS?Use std.array.array (alias: std.range.array) to make the range returned my map!(to!double) into an array. Note that the result of map isn't actually evaluated until it is iterated. std.array.array will iterate and collect. https://dlang.org/phobos/std_array.html#array result = res.readJson[].map!(to!double).array; should work perfectly.double[3] result; Json json = res.readJson; for(int i = 0; i < json.length; i++){ result[i] = json[i].to!double; }I'd prefer to do something like:result = res.readJson[].map!(to!double);
Jan 25 2021
On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote:Hi all, How can I change the following to a more D-like approach by using UFCS?Unless you have a good reason, use a slice and not a static array: double[] result; The result of std.array.array will be a slice anyway.double[3] result;
Jan 25 2021
On Tuesday, 26 January 2021 at 01:38:45 UTC, Q. Schroll wrote:On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote:Why would I need to use a slice instead of a static array? I'm using a static array in this instance because I have and underlying 3d vector with double[3] as its base typeHi all, How can I change the following to a more D-like approach by using UFCS?Unless you have a good reason, use a slice and not a static array: double[] result; The result of std.array.array will be a slice anyway.double[3] result;
Jan 25 2021
On Tuesday, 26 January 2021 at 02:19:10 UTC, Tim wrote:On Tuesday, 26 January 2021 at 01:38:45 UTC, Q. Schroll wrote:In that case, maybe this untested code double[3] result; res.readJson[].map!(to!double).copy(result[]);On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote:Why would I need to use a slice instead of a static array? I'm using a static array in this instance because I have and underlying 3d vector with double[3] as its base typeHi all, How can I change the following to a more D-like approach by using UFCS?Unless you have a good reason, use a slice and not a static array: double[] result; The result of std.array.array will be a slice anyway.double[3] result;
Jan 25 2021