Book Image

Haskell Data Analysis cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis cookbook

By: Nishant Shukla

Overview of this book

Step-by-step recipes filled with practical code samples and engaging examples demonstrate Haskell in practice, and then the concepts behind the code. This book shows functional developers and analysts how to leverage their existing knowledge of Haskell specifically for high-quality data analysis. A good understanding of data sets and functional programming is assumed.
Table of Contents (14 chapters)
13
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...