How can I use UFCS for a loop

Q. Schroll qs.il.paperinik at gmail.com
Tue Jan 26 01:36:26 UTC 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?
>
>>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);

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.




More information about the Digitalmars-d-learn mailing list