How can I get a sha1 of an entire ref in Clojure?
I would like to compare two copies of parts of a ref t开发者_如何学Goo see if they have changed. How can I get a sha1 of a map and any sub leaves?
Depending on exactly what you want to do, you may find just using the normal Java hashCode() via the "hash" function is simpler than trying to use SHA:
(hash {:a "hoho" :b "hehe"})
=> 2025831869
(hash {:a "hoho" :b "hihi"})
=> 2025836181
This is enough in most cases to determine if two maps are different.
Note that neither SHA or any other hashcode guarantee equality when two objects have the same hash - many objects can potentially have the same hash. As a consequence, if the hash is the same you will still have to check whether the two objects are equal in value
Also, be aware that computing hashes is more expensive than a simple comparison using =. So it only makes sense to use a hashing technique if you are able to store the hash value and re-use it for many comparisons.
are you sure you actually want something as low-level as that? Identity in clojure, at least wrt values, is kinda built in:
user> (= {:a 1, :b {:c 2}} (hash-map :b {:c 2} :a 1))
true
精彩评论