Skip to content

Commit

Permalink
improved integral shrink trees to match behavior of binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyson Williams committed Nov 10, 2020
1 parent 333d331 commit f70daff
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Hedgehog/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,27 @@ module Gen =
/// Generates a random number in the given inclusive range.
[<CompiledName("Integral")>]
let inline integral (range : Range<'a>) : Gen<'a> =
create (Shrink.towards <| Range.origin range) (Random.integral range)
let mapFirstDifferently f g = function
| [] -> []
| x :: xs -> (f x) :: (xs |> List.map g)
let rec createTree (destination : ^a) (x : ^a) =
let childrenValues =
match Shrink.towards destination x |> Seq.toList with
| [] -> []
| x :: [] -> List.singleton x
| _ :: xs -> xs
let xs =
childrenValues
|> Seq.cons destination
|> Seq.pairwise
|> Seq.toList
|> mapFirstDifferently id (fun (d, x) -> (d + LanguagePrimitives.GenericOne, x))
|> Seq.map (fun (d, x) -> createTree d x)
Node (x, xs)
range
|> Random.integral
|> Random.map (range |> Range.origin |> createTree)
|> ofRandom

//
// Combinators - Choice
Expand Down

0 comments on commit f70daff

Please sign in to comment.