[RFC FS-1135] Random functions for collections #731
Replies: 5 comments 28 replies
-
FSharp.Core does not target .NET 6 or higher though. |
Beta Was this translation helpful? Give feedback.
-
Right now there is a main issue - how to call newly functions, right now we have two main options
Please vote for the prefixed version with 🚀, for the short version with 🎉 |
Beta Was this translation helpful? Give feedback.
-
Hey @Lanayx, I'm reviewing your proposal atm (and was hoping to un-stale this RFC, it is good work and should go in!). I was wondering if you've considered to use a function as argument, as opposed to a I know it would increase the surface area of the functions, and it would require an extra check (i.e., if the returned value is out of range). But considering we're in a functional language, it stands to reason that we allow users to use a function to provide the random values. Alternatively, |
Beta Was this translation helpful? Give feedback.
-
@Lanayx, putting this in a different discussion thread, @nkeenan38 pointed out the following point in an offline discussion:
My take on this is that these will fail for infinite sequences (similar to |
Beta Was this translation helpful? Give feedback.
-
@Lanayx, you raised a point about what should happen when the shuffling algorithm you provide is not really shuffling. I wasn't entirely sure wrt my own answer, so I figured, let's look at the source. You may or may not want to mention this in the RFC. It may help with adding corner cases. Here are two examples (remember that max is exclusive, min is inclusive): type MyRandom() =
inherit System.Random()
override this.Next(minv, maxv) =
if minv = maxv then minv
else maxv - 1
let testShuffle() =
let xs = [|1;2;3;4;5;6;7;8|]
MyRandom().Shuffle(xs)
xs
> testShuffle();;
val it: int array = [|8; 1; 2; 3; 4; 5; 6; 7|] type MyRandom() =
inherit System.Random()
override this.Next(minv, maxv) =minv
let testShuffle() =
let xs = [|1;2;3;4;5;6;7;8|]
MyRandom().Shuffle(xs)
xs
> testShuffle();;
val it: int array = [|1; 2; 3; 4; 5; 6; 7; 8|] Essentially, I'd suggest we just follow the .NET algorithm and translate where needed. I'm sure you've seen this before (for reference, here's the original implementation, not sure we pointed to it before: dotnet/runtime#78598): public void Shuffle<T>(Span<T> values)
{
int n = values.Length;
for (int i = 0; i < n - 1; i++)
{
int j = Next(i, n);
if (j != i)
{
T temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
} I don't think we can rely on |
Beta Was this translation helpful? Give feedback.
-
Let's discuss RFC FS-1135 as proposed in PR #732
Beta Was this translation helpful? Give feedback.
All reactions