Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 07:44, Ali Çehreli wrote: > the instantiations could be too many for R... I am still wrong there. It is inconceivable to instantiate the following template for the same type (e.g. string) from "too many" places in a program: auto RandomChoice(R...)(R r) { // ... } There may be

Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 02:20, Stanislav Blinov wrote: > On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: >> On 1/25/22 13:55, forkit wrote: >> >> > auto RandomChoice(R...)(R r) >> >> Watch out though: The compiler will compile a different function per >> set of values. For example, there will b

Re: passing a variadic parameter to randomSample

2022-01-26 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: On 1/25/22 13:55, forkit wrote: > auto RandomChoice(R...)(R r) Watch out though: The compiler will compile a different function per set of values. For example, there will be separate RandomChoice instances for ("hello") vs. ("wor

Re: passing a variadic parameter to randomSample

2022-01-25 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 25, 2022 at 10:48:26PM +, forkit via Digitalmars-d-learn wrote: [...] > ... but my main focus here, was learning about variadic template > functions. D has several flavors of variadics: 1) C-style variadics (not type-safe, not recommended): int func(int firstArgc, ...) 2

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:35:29 UTC, forkit wrote: I should point out (to anyone looking at that code I posted), that it's easier, and makes more sense, to just write: writeln( ["typeA", "typeB", "typeC"].choice ); ... but my main focus here, was learning about variadic template f

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: thanks. makes it even shorter and simpler :-) // -- module test; @safe: import std; auto RandomChoice(R...)(R r) { auto rnd = MinstdRand0(unpredictableSeed); return only(r).choice(rnd); } void main() { writeln( Ra

Re: passing a variadic parameter to randomSample

2022-01-25 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 25, 2022 at 02:07:43PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: [...] > auto RandomChoice(R)(R[] r...) > > > { > > auto rnd = MinstdRand0(unpredictableSeed); > > return only(r).randomSample(1, rnd).front; > > Which makes that simpler as well because being a slice,

Re: passing a variadic parameter to randomSample

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 13:55, forkit wrote: > auto RandomChoice(R...)(R r) Watch out though: The compiler will compile a different function per set of values. For example, there will be separate RandomChoice instances for ("hello") vs. ("world"). D has a simple variadic parameter syntax as well: auto R

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 11:50:08 UTC, vit wrote: thanks. problem solved (providing all parameters are of the same type). // --- module test; import std; auto RandomChoice(R...)(R r) { auto rnd = MinstdRand0(unpredictableSeed); return only(r).randomSample(1, rnd).front; } v

Re: passing a variadic parameter to randomSample

2022-01-25 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

Re: passing a variadic parameter to randomSample

2022-01-25 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

Re: passing a variadic parameter to randomSample

2022-01-25 Thread vit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.randomSample` cannot deduce function from argument types `