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

Reply via email to