On 02/22/2013 05:07 PM, bearophile wrote:
> 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

Agreed.

> Note that:
> range[uniform(0, range.length)];
>
> is written more compactly as:
> range[uniform(0, $)];

Good point. I would think that $ must be "out in the open" to work that way.

>> void main()
>> {
>> writeln(iota(10).map!((_) => pickOne(letters)));
>> }
>
> Instead of "(_)" I think it's better to use a simpler "_".

Mine evolved from the earlier buggy ()=> then I inserted an underscore without realizing to remove the parentheses.

> 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

I did need such a function in the past. I have been thinking about the name 'generate' but if 'table' is common already, why not. :)

> So it becomes something like:
>
> 10.table!({ return letters.choice; }).writeln;
>
> Bye,
> bearophile

Ali

Reply via email to