digitalmars.D.learn - Most convenient way to write a loop with fixed length and no need for
- Martin Tschierschke (12/12) Jun 30 2017 What do I have to do, to make this work?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (18/30) Jun 30 2017 Seems easy to put together: :)
- Anton Fediushin (15/27) Jun 30 2017 You can use it like this:
- Martin Tschierschke (5/24) Sep 09 2017 Thank You! Ali and Anton!
What do I have to do, to make this work?
iota(number).each!...command_x(a...);command_y(b...);command_z(c..))
^^^^^?
how to write the lambda?
Similar to the ruby (1..number).each{ commands...}
Don't want to write the following, because the index i is not
used inside the loop
and this should be clear when reading it:
for (i=0;i<number;i++){commands...}
or
foreach(i;iota(number)){commands...}
Or is there something like number.times!{....} possible?
Jun 30 2017
On 06/30/2017 12:44 AM, 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..))
^^^^^?
how to write the lambda?
Similar to the ruby (1..number).each{ commands...}
Don't want to write the following, because the index i is not used
inside the loop
and this should be clear when reading it:
for (i=0;i<number;i++){commands...}
or
foreach(i;iota(number)){commands...}
Or is there something like number.times!{....} possible?
Seems easy to put together: :)
import std.stdio;
auto times(alias Func, T)(T number) {
import std.range : iota;
import std.algorithm : each;
return number.iota.each!(_ => Func());
}
void foo() {
writeln("foo");
}
void bar() {
writeln("bar");
}
void main() {
3.times!({ foo(); bar(); });
}
Ali
Jun 30 2017
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..)) ^^^^^? how to write the lambda? Similar to the ruby (1..number).each{ commands...} Don't want to write the following, because the index i is not used inside the loop and this should be clear when reading it: for (i=0;i<number;i++){commands...} or foreach(i;iota(number)){commands...}You can use it like this: iota(10).each!((x) { command1(); command2(); ... }); 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"); });
Jun 30 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:I missed this syntax!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(); ... });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.Thank You! Ali and Anton! D is so cool! :D ps. This post was written but not send... on June 30...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"); });
Sep 09 2017









=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> 