What you do is essentially the standard Haskell QuickCheck way of
generating recursive trees, right?

http://www.cse.chalmers.se/%7Erjmh/QuickCheck/manual_body.html#16

Except that you bound the number of levels, whereas they bound the size
(number of nodes).

The “pattern” here would be the sized combinator.

http://hackage.haskell.org/package/QuickCheck-2.9.1/docs/Test-QuickCheck-Gen.html#v:sized

Maybe that is something that would be valuable to add to elm-test for such
situations?

Possibly along with resize and scale.

http://hackage.haskell.org/package/QuickCheck-2.9.1/docs/Test-QuickCheck-Gen.html#v:resize

http://hackage.haskell.org/package/QuickCheck-2.9.1/docs/Test-QuickCheck-Gen.html#v:scale

Note that the size parameter in QuickCheck also affects other generators.
So maybe that would motivate a general overhaul of elm-test‘s Fuzz module.
​

2016-08-13 10:01 GMT+02:00 Max Goldstein <[email protected]>:

> 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.
>

-- 
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