On 01/22/2015 11:26 AM, ddos wrote:

> i want to create 100 uniform distributed numbers and print them
> my first attempt, just written by intuition:
> [0 .. 100].map!(v => uniform(0.0, 1.0).writeln);
>
> i found out i can't write [0 .. 100] to define a simple number range,

As currently being discussed in another thread, the a..b syntax does not correspond to a first-class D language construct.

> but is there a function to do so?

Yes, std.range.iota:

    auto numbers = iota(100).map!(_ => uniform(0.0, 1.0));

Note that, 'numbers' is lazy, the definition above does not call uniform() yet.

To make an array out of it, call std.array.array at the end:

    auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)).array;

> second attempt, replacing the range with an simple array
> [0,1,2].map!(v => uniform(0.0,1.0).writeln);
> this does compile and run, but doesn't print anything, just an empty
> string, why is that?

What I said above: it is just a range waiting to be used.

> finally i got it working with this:
> auto t = [0,1,2].map!(v => uniform(0.0,1.0));
> writeln(t);
>
> seems pretty easy eh?

writeln() consumes any range to print it on the standard output. There is no array to speak of though: writeln() consumes a copy of 't' and your 't' is still a lazy range waiting to be consumed.

Ali

Reply via email to