diff --git a/src/Hedgehog/Gen.fs b/src/Hedgehog/Gen.fs index a4e9995e..a5afa473 100644 --- a/src/Hedgehog/Gen.fs +++ b/src/Hedgehog/Gen.fs @@ -309,7 +309,10 @@ module Gen = let list (range : Range) (g : Gen<'a>) : Gen> = Random.sized (fun size -> random { let! k = Random.integral range - let! xs = Random.replicate k (toRandom g) + let! xs = + toRandom g + |> List.replicate k + |> ListRandom.sequence return Shrink.sequenceList xs |> Tree.filter (atLeast (Range.lowerBound size range)) }) @@ -488,7 +491,8 @@ module Gen = let sampleTree (size : Size) (count : int) (g : Gen<'a>) : List> = let seed = Seed.random () toRandom g - |> Random.replicate count + |> List.replicate count + |> ListRandom.sequence |> Random.run seed size let sample (size : Size) (count : int) (g : Gen<'a>) : List<'a> = diff --git a/src/Hedgehog/Hedgehog.fsproj b/src/Hedgehog/Hedgehog.fsproj index f5923a63..84439bf8 100644 --- a/src/Hedgehog/Hedgehog.fsproj +++ b/src/Hedgehog/Hedgehog.fsproj @@ -33,6 +33,7 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md + diff --git a/src/Hedgehog/ListRandom.fs b/src/Hedgehog/ListRandom.fs new file mode 100644 index 00000000..fe8c0142 --- /dev/null +++ b/src/Hedgehog/ListRandom.fs @@ -0,0 +1,16 @@ +[] +module Hedgehog.ListRandom + +let traverse (f: 'a -> Random<'b>) (list: List<'a>) : Random> = + let rec loop input output = + match input with + | [] -> output |> List.rev |> Random.constant + | a :: input -> + random { + let! b = f a + return! loop input (b :: output) + } + loop list [] + +let sequence (randoms : List>) : Random> = + randoms |> traverse id diff --git a/src/Hedgehog/Random.fs b/src/Hedgehog/Random.fs index ddb669e0..3d793d6c 100644 --- a/src/Hedgehog/Random.fs +++ b/src/Hedgehog/Random.fs @@ -51,17 +51,6 @@ module Random = let bind (f: 'a -> Random<'b>) (r: Random<'a>) : Random<'b> = r |> map f |> join - let replicate (times: int) (r: Random<'a>) : Random> = - Random (fun seed0 size -> - let rec loop seed k acc = - if k <= 0 then - acc - else - let seed1, seed2 = Seed.split seed - let x = unsafeRun seed1 size r - loop seed2 (k - 1) (x :: acc) - loop seed0 times []) - type Builder internal () = member __.Return(x : 'a) : Random<'a> = constant x