Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using Google's CityHash hash functions for strings


Google's CityHash hash functions are optimized for hashing strings, but are not meant to be cryptographically secure. CityHash is ideal for implementing a hash table dealing with strings. We will use it in this recipe to produce both 64-bit and 128-bit digests.

Getting ready

Install the cityhash package from Cabal as follows:

$ cabal install cityhash

How to do it…

  1. Import the relevant packages as follows:

    import Data.Digest.CityHash
    import Data.ByteString.Char8 (pack)
    import Data.Word (Word64)
    import Data.LargeWord (Word128)
  2. Test the various hashing function on an input string using the following code snippet:

    main = do
     (pack str) (1 :: Word128)  let str = "cityhash"
      print $ cityHash64 (pack str)
      print $ cityHash64WithSeed (pack str) (1 :: Word64)
      print $ cityHash64WithSeed (pack str) (2 :: Word64)
      print $ cityHash128 (pack str)
      print $ cityHash128WithSeed
      print $ cityHash128WithSeed (pack str) (2 :: Word128)
  3. Display the output as follows...