diff --git a/CHANGELOG.md b/CHANGELOG.md index 8913aace..200efb12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Version ?.?.? - Add `Tree.apply`. Change `Gen.apply` from monadic to applicative. Revert runtime optimization of `Gen.integral`. ([#398][398], [@TysonMN][TysonMN]) +- Change `ListGen.traverse` from monadic to applicative. ([#399][399], [@TysonMN][TysonMN]) ## Version 0.12.0 (2021-12-12) @@ -189,6 +190,8 @@ [porges]: https://github.com/porges +[399]: + https://github.com/hedgehogqa/fsharp-hedgehog/pull/399 [398]: https://github.com/hedgehogqa/fsharp-hedgehog/pull/398 [386]: diff --git a/src/Hedgehog/ListGen.fs b/src/Hedgehog/ListGen.fs index 43a9b0ed..d7c3a234 100644 --- a/src/Hedgehog/ListGen.fs +++ b/src/Hedgehog/ListGen.fs @@ -1,15 +1,15 @@ module Hedgehog.ListGen + let traverse (f: 'a -> Gen<'b>) (ma: list<'a>) : Gen> = - let rec loop input output = - match input with - | [] -> output |> List.rev |> Gen.constant - | a :: input -> - gen { - let! b = f a - return! loop input (b :: output) - } - loop ma [] + List.empty + |> Gen.constant + |> List.fold (fun glb a -> + (fun list b -> List.append list [b]) + |> Gen.constant + |> Gen.apply glb + |> Gen.apply (f a)) + <| ma let sequence (gens : List>) : Gen> = gens |> traverse id