Can't figure out how to make a generator from a generator

85 Views Asked by At

I'm having trouble figuring out how to make fscheck generators WITHOUT using the gen workflow syntax and assume there must be a good way to do it. I end up with a nested Gen<Gen<... and want a flattened version. With option I'd use Option.bind to eliminate the nesting. With seq I'd use SelectMany. But I don't see that kind of method appear in the Gen module.

// return a Gen<char list> as expected
// first makes a random number from 5 to 8
// and then creates a random list of characters that long
let randomListLengths =
    gen {
        let! len = Gen.choose(5,8)
        let! item = Gen.elements ['a';'b';'c'] |> Gen.listOfLength len
        return item
    }

// return a Gen<Gen<char list>>
let anotherWayThatDoesNotWork =
    Gen.choose(5, 8)
    |> Gen.map (fun len -> Gen.elements['a';'b';'c'] |> Gen.listOfLength len)
1

There are 1 best solutions below

2
On BEST ANSWER

You can use the >>= operator:

let randomListLengths =
    Gen.choose(5,8)
    >>= (fun len -> Gen.elements ['a';'b';'c'] |> Gen.listOfLength len)