elm-community / elm-test

moved to elm-explorations/test
https://github.com/elm-explorations/test
BSD 3-Clause "New" or "Revised" License
339 stars 35 forks source link

Add listOfLength for fuzzing lists with a specific length #216

Open rhofour opened 6 years ago

rhofour commented 6 years ago

I was thinking something like: listOfLength: Int -> Fuzzer a -> Fuzzer (List a)

This looks like it shouldn't be too hard to generalize from the existing list function. The major benefit would be to allow users to decide their own probability distribution over the generated list length (using something like Fuzz.andThen). This is what motivated me to suggest this and it would limit the severity of #168 .

This is already possible with what's provided, but it could be easier:

listOfLength : Int -> Fuzzer a -> Fuzzer (List a)
listOfLength listLen fuzzer =
    List.foldl
        (Fuzz.map2 (\elem -> \list -> elem :: list))
        (Fuzz.constant [])
        (List.repeat listLen fuzzer)
MartinKavik commented 6 years ago

Hi, it would be nice to have even more flexibility:

listRange: Int -> Int -> Fuzzer a -> Fuzzer (List a)
listRange lo hi fuzzer =
   ...

It can favour shorter lists like the current Fuzz.list.


I'm creating combinatorics library, so my use-case is:

        , fuzz
            (( char, intRange 0 3 )
                |> tuple
                |> listOfLength 3
             -- listRange 0 3 would be better
            )
            "permutations with repetitions"
          <|
            \setWithCounts ->
                -- setWithCounts = e.g. [("a",2), ("x",5)]
        ...

Thanks!

mgold commented 6 years ago

If we're going to do this, and I'm not saying we are, why not: Fuzzer Int -> Fuzzer a -> Fuzzer (List a)?