digitalmars.D.learn - iota access in foreach loop
- Alex (22/22) Jun 04 2016 Hi all!
- Alex (2/2) Jun 04 2016 PPS: The error shown is in line with the iota inside foreach:
- Brad Anderson (10/32) Jun 04 2016 Check out enumerate() in std.range;
- Brad Anderson (3/14) Jun 04 2016 How could I have forgotten the UFCS rox version...
- Alex (4/21) Jun 04 2016 :D
Hi all! Could you help me clearify why a iota can't be accessed with two arguments in a foreach loop? following tests show my problem: What does work: int[] ku = [0, 1, 2, 3, 4]; foreach(i, el; ku) writeln("index: ", i, " element: ", el); What does not work: counter = 5; foreach(i, el; iota(counter)) writeln("index: ", i, " element: ", el); Motivation: In real I want to have: counter = 5; foreach(i, el; randomCover(iota(counter))) writeln("index: ", i, " element: ", el); so, I could follow a random permutation. Maybe there is another simple way to achieve this? PS: needed imports: import std.random : randomCover; import std.range : iota; import std.stdio : writeln;
Jun 04 2016
PPS: The error shown is in line with the iota inside foreach: Error: cannot infer argument types, expected 1 argument, not 2
Jun 04 2016
On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote:Hi all! Could you help me clearify why a iota can't be accessed with two arguments in a foreach loop? following tests show my problem: What does work: int[] ku = [0, 1, 2, 3, 4]; foreach(i, el; ku) writeln("index: ", i, " element: ", el); What does not work: counter = 5; foreach(i, el; iota(counter)) writeln("index: ", i, " element: ", el); Motivation: In real I want to have: counter = 5; foreach(i, el; randomCover(iota(counter))) writeln("index: ", i, " element: ", el); so, I could follow a random permutation. Maybe there is another simple way to achieve this? PS: needed imports: import std.random : randomCover; import std.range : iota; import std.stdio : writeln;Check out enumerate() in std.range; int counter = 5; foreach(i, el; enumerate(randomCover(iota(counter)))) writeln("index: ", i, " element: ", el); index: 0 element: 3 index: 1 element: 1 index: 2 element: 0 index: 3 element: 2 index: 4 element: 4
Jun 04 2016
On Saturday, 4 June 2016 at 18:55:09 UTC, Brad Anderson wrote:On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote:How could I have forgotten the UFCS rox version... foreach(i, el; iota(counter).randomCover.enumerate)[...]Check out enumerate() in std.range; int counter = 5; foreach(i, el; enumerate(randomCover(iota(counter)))) writeln("index: ", i, " element: ", el); index: 0 element: 3 index: 1 element: 1 index: 2 element: 0 index: 3 element: 2 index: 4 element: 4
Jun 04 2016
On Saturday, 4 June 2016 at 18:58:51 UTC, Brad Anderson wrote:On Saturday, 4 June 2016 at 18:55:09 UTC, Brad Anderson wrote:Ah! thanks!On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote:[...]Check out enumerate() in std.range;:Dint counter = 5; foreach(i, el; enumerate(randomCover(iota(counter)))) writeln("index: ", i, " element: ", el); index: 0 element: 3 index: 1 element: 1 index: 2 element: 0 index: 3 element: 2 index: 4 element: 4How could I have forgotten the UFCS rox version...foreach(i, el; iota(counter).randomCover.enumerate)Thanks!
Jun 04 2016