Too complicated code for generating a random string?
bearophile
bearophileHUGS at lycos.com
Fri Feb 22 17:07:21 PST 2013
Ali Çehreli:
> auto pickOne(R)(R range)
> // insert template constraints here ... :)
> {
> return range[uniform(0, range.length)];
> }
That's the function choice() I'd like in Phobos:
http://d.puremagic.com/issues/show_bug.cgi?id=4851
Note that:
range[uniform(0, range.length)];
is written more compactly as:
range[uniform(0, $)];
> void main()
> {
> writeln(iota(10).map!((_) => pickOne(letters)));
> }
Instead of "(_)" I think it's better to use a simpler "_".
That's another commonly useful function, often named table(),
similar to map() but doesn't pass an index to the callable:
http://reference.wolfram.com/mathematica/ref/Table.html
So it becomes something like:
10.table!({ return letters.choice; }).writeln;
Bye,
bearophile
More information about the Digitalmars-d
mailing list