gextra
gextra

Reputation: 8895

Are there any libraries or functions in erlang that generate the hashcode of a Term or String?

In Java you can generate an integer hashcode for a string, such as:

Java:
int hc = "My Phrase".hashCode();

Is there any library capable of generating String hashes or even better, Term hashes ?

The problem I am trying to solve is that I want to cache a Term that is generated from another system. This Term will be string-ified and transmitted together with a hashcode, which will be used for future comparisons that will determine if the Term/String has changed. I am not using time-stamps for this purpose.

Upvotes: 2

Views: 1101

Answers (2)

3lectrologos
3lectrologos

Reputation: 9662

How about erlang:phash/2 or erlang:phash2/1?

Upvotes: 12

James Black
James Black

Reputation: 41858

For a cryptographic hash, using the SHA family you can look at the code from this individual:

http://steve.vinoski.net/blog/2009/01/03/more-sha-in-erlang/

You can also look at records (http://20bits.com/article/erlang-an-introduction-to-records) as they will allow you to create an associative array functionality to begin creating a hashmap, if that is your goal.

But the best solution, depending on your need, is to look at this pdf for hashes and hashmaps.

http://www.erlang-factory.com/upload/presentations/468/EUC_Hashes2011.pdf

And for a simple example of creating your own immutable hash library you can look at

http://www.trapexit.org/Hash_with_Immutable_Keys_or_Values

It would be helpful if you could explain why you need hashes. What problem are you trying to solve?

Upvotes: 1

Related Questions