Christian Sturm wrote: > do you know a short and easy way to iterate a block X times?
Aye ;-) > I know, one could use while, for, ... but there are some reasons, > why they don't fit. > > All these versions are long. I really like Perl, because you can > do thing with shorter code. In all these loops you must define a > variable which gets increased or decreased in each iteration. No. for ^5 { say "hi" } I don't define any variable to be incremented, and it's not really that many keystrokes. > Here is the next problem. To be honest I don't know much about > how to make a program efficient, but for me it seems to be > inefficient to have a calculation, a comparison each time and two > variables (or one variable, which changes every time and a > constant) only for doing something X times. Especially if I know > how often I want to run the block before I start it and if I > don't need to know the value of $_ in > > for 1..5 { > $value = $_; > } In a perfect world the optimizer finds out by static analysis that $_ is not needed in the block, and doesn't generate it. In the real world we don't have any optimizing Perl 6 compilers yet. > So where is this needed? > > I know Perl isn't the best language when I want to do big > calculations, but the Lucas?Lehmer primality test[1] would be one > example. > > I'm not sure how this should look like. Ruby allows something > like this. So maybe one should have look there. Here a few > suggestions: > > > $X.times { > $s = $s * $s; > } It always felt weird to me to delegate control flow to integers - but maybe that's just me. > $X * { > $s = $s * $s > } > > iterate ( $X ) > { > $s = $s * $s; > } > > > I guess something always has to be increased/decreased if it > comes down to C/Assembly, but even if it doesn't make it faster > it would make to code smaller and easier to understand. Would it? given that Perl 6 has at least 4 loop constructs today (for, while, map, loop), and that * is already taken both as an infix operator and a term, I'm not sure about that one. Cheers, Moritz