On Saturday, 22 January 2022 at 19:32:07 UTC, forkit wrote:
trying to make sense of the below:
// ---
module test;
import std;
void main()
{
auto rnd = Random(unpredictableSeed);
int howManyTimes = 5;
// ok - using 'e =>' makes sense
writeln(howManyTimes.iota.map!(e => rnd.dice(0.6,
1.4)).format!"%(%s,%)");
// ok - though using 'howManyTimes =>' doesn't make much
sense??
writeln(howManyTimes.iota.map!(howManyTimes =>
rnd.dice(0.6, 1.4)).format!"%(%s,%)");
// NOT ok - using '5 =>' - but isn't this effectively the
same as above line?
//writeln(howManyTimes.iota.map!(5 => rnd.dice(0.6,
1.4)).format!"%(%s,%)");
}
// ---
No, it's not the same. 'Tis not really a "map question", looks
more like a question about
https://dlang.org/spec/expression.html#function_literals (see
#10).
In the second case, you're defining a lambda with single
parameter named `howManyTimes`, which is not at all related to
your local variable of the same name. Third case is invalid, as
you're effectively trying to do this:
auto func(T)(T 5) { return rnd.dice(0.6, 1.4); }
Which, of course, doesn't make any sense, does it? :)
Given your use case (call a function N times), I think `generate`
would be more appropriate here:
```d
import std.random;
import std.stdio;
import std.range : generate, take;
void main()
{
auto rnd = Random(unpredictableSeed);
int howManyTimes = 5;
generate!(() => rnd.dice(0.6,
1.4)).take(howManyTimes).writeln;
}
```