Try this implementation:

fuzzMusic : Int -> Fuzzer Music
fuzzMusic depth =
    let
        note =
            Fuzz.map Note Fuzz.int

        rest =
            Fuzz.map Rest Fuzz.int

        nextLevel =
            if depth <= 0 then
                note
            else
                fuzzMusic (depth - 1)

        seq =
            Fuzz.tuple ( nextLevel, nextLevel ) |> Fuzz.map (uncurry Seq)

        par =
            Fuzz.tuple ( nextLevel, nextLevel ) |> Fuzz.map (uncurry Par)
    in
        if depth > 1 then
            Fuzz.frequencyOrCrash
                [ ( 1, note )
                , ( 1, rest )
                , ( 1, seq )
                , ( 1, par )
                ]
        else
            Fuzz.frequencyOrCrash
                [ ( 1, note )
                , ( 1, rest )
                ]

The trick here is the if-statement when defining nextLevel. All of these 
definitions are eagerly evaluated, so it keeps trying to make more fuzzers 
that it doesn't need and blows the stack -- if you don't have the 
if-statement. Instead we supply a base case that will be hit eventually. 
The writer of the fuzz test decides on the maximum depth when they call the 
function to obtain a fuzzer.

I wonder if there's an extractable pattern here?

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to