często można achieve the same effect as overloading with a Discriminated Union.
Oto propozycja opiera się na OP:
type Range = Default | Between of int * int
let randomSequence range =
let m, n =
match range with
| Default -> 1, 100
| Between (min, max) -> min, max
seq {
let rng = new Random()
while true do
yield rng.Next(m, n) }
Wskazówka wprowadzenie Range
dyskryminowanych Unii.
Oto kilka (FSI) przykłady użycia:
> randomSequence (Between(8, 39)) |> Seq.take 10 |> Seq.toList;;
val it : int list = [11; 20; 36; 30; 35; 16; 38; 17; 9; 29]
> randomSequence Default |> Seq.take 10 |> Seq.toList;;
val it : int list = [98; 31; 29; 73; 3; 75; 17; 99; 36; 25]
Innym rozwiązaniem jest zmiana randomSequence kiedykolwiek tak lekko wziąć krotki zamiast dwóch wartości:
let randomSequence (m, n) =
seq {
let rng = new Random()
while true do
yield rng.Next(m, n) }
Umożliwi to również zdefiniowanie domyślnej wartości :, podobnej do następującej:
let DefaultRange = 1, 100
Oto kilka (FSI) przykłady użycia:
> randomSequence (8, 39) |> Seq.take 10 |> Seq.toList;;
val it : int list = [30; 37; 12; 32; 12; 33; 9; 23; 31; 32]
> randomSequence DefaultRange |> Seq.take 10 |> Seq.toList;;
val it : int list = [72; 2; 55; 88; 21; 96; 57; 46; 56; 7]
Jest nieco trudne z powodu currying. –