[Haskell-cafe] two problems with Data.Binary and Data.ByteString

Krasimir Angelov kr.angelov at gmail.com
Wed Nov 5 10:20:52 EST 2008


On Wed, Aug 13, 2008 at 1:18 AM, Don Stewart <dons at galois.com> wrote:
>    instance Binary a => Binary [a] where
>        put l  = put (length l) >> mapM_ put l
>        get    = do n <- get :: Get Int
>                    replicateM n get

Of course I changed this as well. Now it is:

instance (Ord k, Binary k, Binary e) => Binary (Map.Map k e) where
    put m = put (Map.size m) >> mapM_ put (Map.toAscList m)
    get   = liftM Map.fromDistinctAscList get

You don't have to convert the map to list just to compute its size.
The Map.size is a O(1) function.


More information about the Haskell-Cafe mailing list