Most convenient way to write a loop with fixed length and no need for the index?

Martin Tschierschke via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 9 01:35:51 PDT 2017


On Friday, 30 June 2017 at 08:19:07 UTC, Anton Fediushin wrote:
> On Friday, 30 June 2017 at 07:44:45 UTC, Martin Tschierschke 
> wrote:
>> What do I have to do, to make this work?
>>
>> iota(number).each!...command_x(a...);command_y(b...);command_z(c..))
>
>
> You can use it like this:
> iota(10).each!((x) { command1(); command2(); ... });

I missed this syntax!

>
> Or there is a short syntax (lambda):
> iota(10).each!((x) => command1());
>


> See http://dlang.org/spec/expression.html#Lambda for more info 
> about lambdas.
>
>
>> Or is there something like number.times!{....} possible?
> You can write your own function. It is simple.
> void times(alias fun)(size_t i) {
> 	foreach(unused;0..i)
> 		fun();
> }
>
> and use it like this:
> 10.times!({ writeln("yaaay"); });

Thank You! Ali and Anton!
D is so cool! :D

ps. This post was written but not send... on June 30...



More information about the Digitalmars-d-learn mailing list