Hashing a custom data type

Even a custom-defined data type can be hashed easily. Dealing with hashed digests is often useful when the data itself is too space consuming to manage directly. By referencing a data by its digest, we can easily skip the cost of carrying around whole data types. This is especially useful in data analysis.

Getting ready

Install the Data.Hashable package from Cabal as follows:

$ cabal install hashable

How to do it…

  1. Use the GHC language extension DeriveGeneric to autodefine the hash functions for our custom data types as follows:
    {-# LANGUAGE DeriveGeneric #-}
  2. Import the relevant packages using the following lines of code:
    import GHC.Generics (Generic)
    import Data.Hashable
  3. Create a custom data type and let GHC autodefine its ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.