fill array using a lambda function

bearophile bearophileHUGS at lycos.com
Thu Oct 10 07:04:29 PDT 2013


dominic jones:

> I want to fill an array with random numbers without resorting 
> to loops, i.e. by doing something like the following, if it 
> were possible:
>
>   fill!(function double(){ return uniform(0.0, 1.0);})(x[]);
>
> Is there a simple way of doing this?

Generally it's a good idea to use only pure functions inside the 
higher order functions of Phobos. using impure functions like 
uniforms could lead to bugs or performance problems.

This is a way to do it (untested):

x.copy(x.length.iota.map!(_ => uniform(0.0, 1.0));

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list