Please explain the Lazy type differences

I wrote this little clip, and haven't been able to figure out why the L.ByteString is different type than Data.ByteString.Internal.ByteString, or if I am just doing everything wrong here. import System.FilePath import System.Environment import qualified Data.ByteString.Lazy as L import qualified Crypto.Hash.SHA1 as SH import Data.Hex (hex) hashFile :: FilePath -> IO L.ByteString hashFile f = do content <- L.readFile f let hasher = SH.init bhsh = SH.update hasher content return (hex (SH.finalize bhsh)) I really just want to get the digest of a file as a [Char], but then of course, SH.update is not very happy about the type I am passing to it either. Can't wait till this becomes easy! Roderick

On 17 January 2011 14:39, Roderick Ford
I wrote this little clip, and haven't been able to figure out why the L.ByteString is different type than Data.ByteString.Internal.ByteString, or if I am just doing everything wrong here.
Because the lazy and the strict variants might have the same name and API, but are actually two different types. Either use Data.ByteString or Data.ByteString.Lazy, but don't mix them. In the case of your code example, it appears that Crypto.Hash.SHA1 is using strict ByteStrings. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On Sun, 2011-01-16 at 21:39 -0700, Roderick Ford wrote:
I wrote this little clip, and haven't been able to figure out why the L.ByteString is different type than Data.ByteString.Internal.ByteString, or if I am just doing everything wrong here.
This is definitely confusing. Types called ByteString are exported from all of the following modules: Data.ByteString Data.ByteString.Char8 Data.ByteString.Internal Data.ByteString.Lazy Data.ByteString.Lazy.Char8 Data.ByteString.Lazy.Internal IIRC, the first three are all the same type, and the last three are all the same type. But the types from the first and last groups are NOT the same. You typically import ByteString qualified anyway... and it's normal to import any of the first set with qualfied name "B", "S", or "SB". The last set is typically imported qualified as "L" or "LB". Here, the S and L stand for "strict" and "lazy". If you need to convert between them, there is: L.toChunks :: L.ByteString -> [S.ByteString] L.fromChunks :: [S.ByteString] -> L.ByteString S.concat :: [S.ByteString] -> S.ByteString Keep in mind that concatenating very large, or a very large number of, strict ByteStrings can be expensive. -- Chris Smith
participants (3)
-
Chris Smith
-
Ivan Lazar Miljenovic
-
Roderick Ford