Skip to content

Commit 800ed44

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

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

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)