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
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
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
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
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
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
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
r syntax as well:
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, r is already a
range. And there is choice():
return r.choice(rnd);
So
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
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
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
quot;, "typeC") );
}
// --
`r` is not input range, try this:
```d
module test;
import std;
string RandomChoice1(R...)(R r)
{
auto rnd = MinstdRand0(unpredictableSeed);
return only(r).randomSample(1, rnd).front;
}
string RandomChoice2(R...)(R r)@nogc
{
auto rnd = Min
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 `
On Friday, 28 December 2018 at 03:59:52 UTC, Ali Çehreli wrote:
It's because randomSample returns either an input range or a
forward range depending both on the kind of range that it gets
and the random number generator used. Documented here:
Ali
Thank you very much, now it all makes sense.
On 12/27/2018 06:06 PM, Murilo wrote:
> Why is it that when I type "auto choice = randomSample(array);" and
> later when I try to index choice as in choice[1] it gives an error
message?
It's because randomSample returns either an input range or a forward
range dependin
Why is it that when I type "auto choice = randomSample(array);"
and later when I try to index choice as in choice[1] it gives an
error message?
Meta:
You need to use the function array from std.array.
import std.array;
int[] source = [ ... ];
int[] sample = randomSample(source, 3).array();
In some cases it's also useful to use std.algorithm.copy:
void main() {
import std.stdio, std.algorithm, std.random, std.
On Sunday, 18 May 2014 at 04:19:05 UTC, David Held wrote:
How do I get an array from randomSample()?
int[] source = [ ... ];
int[] sample = randomSample(source, 3);
src\main.d(30): Error: cannot implicitly convert expression
(randomSample(source, 3u)) of type RandomSample!(int[], void)
to
On 5/17/2014 9:18 PM, David Held wrote:
How do I get an array from randomSample()?
int[] source = [ ... ];
int[] sample = randomSample(source, 3);
src\main.d(30): Error: cannot implicitly convert expression
(randomSample(source, 3u)) of type RandomSample!(int[], void) to int[]
[...]
Even
How do I get an array from randomSample()?
int[] source = [ ... ];
int[] sample = randomSample(source, 3);
src\main.d(30): Error: cannot implicitly convert expression
(randomSample(source, 3u)) of type RandomSample!(int[], void) to int[]
I get that RandomSample is a struct which implements
On 04/12/2012 01:27 PM, Joseph Rushton Wakeling wrote:
> What I _still_ don't understand is the statement in the constructor:
>
> // we should skip some elements initially so we don't always
> // start with the first
>
> ... as it seems to me that doing so would bugger up the selection
> algorith
On 12/04/12 20:39, Ali Çehreli wrote:
That's misleading. RandomSample is a lazy range. The output seems to be elements
of an array only as you pull data out of this range.
Ahhh, it's lazy evaluation. That would explain why you can set ret.gen = gen in
the randomSample functions a
On 04/12/2012 11:30 AM, Joseph Rushton Wakeling wrote:
> What gets output at the end is clearly an array containing a subset of
> the original input.
That's misleading. RandomSample is a lazy range. The output seems to be
elements of an array only as you pull data out of this ran
Hello all,
I'm trying to understand the internal operations of the RandomSample struct in
std.random.
What gets output at the end is clearly an array containing a subset of the
original input. But I can't understand how this is constructed.
The constructor sets various initial
24 matches
Mail list logo