haskell / random

Random number library
Other
53 stars 50 forks source link

Swap genShortByteString for genByteArray #142

Closed Bodigrim closed 9 months ago

Bodigrim commented 12 months ago

Since bytestring-0.12 ShortByteString is just a newtype over ByteArray, a common data type shared by base, text and primitive.

I think we can make class Random more useful if we swap

-genShortByteString :: Int -> g -> (ShortByteString, g)
+genByteArray Int -> g -> (ByteArray, g) 

This way we can shave off dependency on bytestring and do not impose it on our users.

There does not seem to be many users of genShortByteString in the wild. Potentially we can provide a compatibility shim:

genShortByteString :: Coercible a ByteArray => Int -> g -> (a, g)
genShortByteString = coerce genByteArray
lehins commented 12 months ago

I had the same idea the moment you released the byte-array package.

This way we can shave off dependency on bytestring and do not impose it on our users.

I don't think this is terribly important, in fat I'd be against it, especially considering that bytestring is one of the wired-in packages. I'd be against it because ability to generate ByteString and ShortByteString efficiently out of the box is a great feature. I've personally used it on multiple occasions, including at work (which is not gonna show up at hackage search).

That being said, I do agree with you 100% requiring in RandomGen:

genByteArray :: Int -> g -> (ByteArray, g)

instead of

https://github.com/haskell/random/blob/632b64ee0bedbd6c4d0687cf4dcfdf871e6dd42b/src/System/Random/Internal.hs#L183

and

uniformByteArray :: Int -> g -> m ByteArray

instead of

https://github.com/haskell/random/blob/632b64ee0bedbd6c4d0687cf4dcfdf871e6dd42b/src/System/Random/Internal.hs#L282

in StatefulGen

Makes a lot of sense.

ShortByteSring variants can just be extracted into standalone functions.

Shimuuar commented 12 months ago

+1 for switching to ByteArray. As for dropping dependency on bytestring I see no reason to do so. It's basically a part of a standard library