Fix up frequency#338
Conversation
|
I could use another pair of eyes to make sure this produces precisely the right distribution. I believe it does, but I don't trust myself around fence-posts. |
* Make `frequency` detect negative frequencies, frequency total overflow, and zero total frequency. * Rewrite `frequency` to build an `IntMap` to represent the frequency list, which greatly improves efficiency for long lists. * Bump `containers` lower bound to 0.5.11. Fixes hedgehogqa#337.
|
Question: if no frequencies are positive, should we error out (as presently) or should we just discard in that case? |
|
I added a commit that discards when all the frequencies are zero. That feels more correct to me, but feel free to go with whichever you prefer. |
| | nk < 0 = error "Hedgehog.Gen.frequency: Frequency sum above maxBound :: Int" | ||
| | k > 0 = Just ((nk, x), (nk, xs)) | ||
| | otherwise = go (n, xs) | ||
| where !nk = n + fromIntegral k |
There was a problem hiding this comment.
The fromIntegral isn't needed. Let's clean that up in merging.
|
Ah, I just realized we can do a tiny bit better! We can make the |
* Make the `IntMap` one smaller and hold the last element as a fall-through. * Use a lazy `IntMap` instead of a strict one; there's no sense in forcing such polymorphic things.
|
While the In some cases, a hybrid mechanism may be best. If a few entries dominate, then it would make sense to store them in vectors, checked first, and the rest in an |
|
Ping? I haven't seen any comments on this yet. |
|
Personally I'm not convinced about this one. Almost always the input to frequency is hand written with a small number of choices and the time will be quite acceptable. It'll probably actually be faster as is for idiomatic use. |
4139585 to
c228279
Compare
Make
frequencydetect negative frequencies, frequencytotal overflow, and zero total frequency.
Rewrite
frequencyto build anIntMapto represent thefrequency list, which greatly improves efficiency for long
lists.
Fixes #337.