Skip to content

Commit d95c9ae

Browse files
committed
replaced Random.replicate with ListRandom traverse and sequence
1 parent 0efe16e commit d95c9ae

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/Hedgehog/Gen.fs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ module Gen =
309309
let list (range : Range<int>) (g : Gen<'a>) : Gen<List<'a>> =
310310
Random.sized (fun size -> random {
311311
let! k = Random.integral range
312-
let! xs = Random.replicate k (toRandom g)
312+
let! xs =
313+
toRandom g
314+
|> List.replicate k
315+
|> ListRandom.sequence
313316
return Shrink.sequenceList xs
314317
|> Tree.filter (atLeast (Range.lowerBound size range))
315318
})
@@ -488,7 +491,8 @@ module Gen =
488491
let sampleTree (size : Size) (count : int) (g : Gen<'a>) : List<Tree<'a>> =
489492
let seed = Seed.random ()
490493
toRandom g
491-
|> Random.replicate count
494+
|> List.replicate count
495+
|> ListRandom.sequence
492496
|> Random.run seed size
493497

494498
let sample (size : Size) (count : int) (g : Gen<'a>) : List<'a> =

src/Hedgehog/Hedgehog.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md
3333
<Compile Include="OptionTree.fs" />
3434
<Compile Include="Range.fs" />
3535
<Compile Include="Random.fs" />
36+
<Compile Include="ListRandom.fs" />
3637
<Compile Include="Shrink.fs" />
3738
<Compile Include="Gen.fs" />
3839
<Compile Include="ListGen.fs" />

src/Hedgehog/ListRandom.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[<RequireQualifiedAccess>]
2+
module Hedgehog.ListRandom
3+
4+
let traverse (f: 'a -> Random<'b>) (list: List<'a>) : Random<List<'b>> =
5+
let rec loop input output =
6+
match input with
7+
| [] -> output |> List.rev |> Random.constant
8+
| a :: input ->
9+
random {
10+
let! b = f a
11+
return! loop input (b :: output)
12+
}
13+
loop list []
14+
15+
let sequence (randoms : List<Random<'a>>) : Random<List<'a>> =
16+
randoms |> traverse id

src/Hedgehog/Random.fs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ module Random =
5151
let bind (f: 'a -> Random<'b>) (r: Random<'a>) : Random<'b> =
5252
r |> map f |> join
5353

54-
let replicate (times: int) (r: Random<'a>) : Random<List<'a>> =
55-
Random (fun seed0 size ->
56-
let rec loop seed k acc =
57-
if k <= 0 then
58-
acc
59-
else
60-
let seed1, seed2 = Seed.split seed
61-
let x = unsafeRun seed1 size r
62-
loop seed2 (k - 1) (x :: acc)
63-
loop seed0 times [])
64-
6554
type Builder internal () =
6655
member __.Return(x : 'a) : Random<'a> =
6756
constant x

0 commit comments

Comments
 (0)