Is there an idiomatic way D to create an array containing integers from 1 to n?

Often in D I want to do something like:

uint n; foreach(uint i; parallel(1..n)){ somefunc(i); } 

That is, I want to make n function calls (somefunc) in parallel, using integers from 1 to n as arguments.

However, dmd does not seem to like 1..n here, so I end up doing goofy things like:

 uint n; int[] nums = new int[n]; foreach(ulong index, int value; parallel(nums)){ sumfunc(index); } 

Is there an idiomatic way to write this in D? Something not related to creating unnecessary extra variables?

+6
source share
1 answer

Take a look at std.range.iota . This is better than an array because it does not allocate.

 void main() { import std.parallelism, std.range; foreach(i; parallel(iota(1, 100))){ somefunc(i); } } void somefunc(uint i) { import std.stdio; writeln(i); } 
+8
source

Source: https://habr.com/ru/post/973561/


All Articles