There's no contract, but strings, keywords, and symbols should hash the same, 
and collections of these (vectors, lists, maps, sets) should hash the same.

It's difficult to hash numbers the same between Clojure and Clojurescript.

Clojurescript numbers are all doubles (because JS), so they should in theory 
hash the same as Clojure doubles. Clojure hashes doubles using Java's 
Double.hashCode(), which relies on knowing the exact bits of the double. These 
bits are not available in Javascript (at least not easily or without using 
typedarrays). I'm also not sure if this particular implementation of hashCode 
is part of the Java spec (i.e. implemented the same by all JDKs and JVMs.)

Of course in practice the same clojure form (as read) will not hash the same in 
clj and cljs because clj uses longs most of the time.

You could take a hybrid approach where integers in cljs are hashed like longs 
in Clojure. This works up to 52 bits, but longs with more bits than that are 
not representable in Clojurescript. This is an approach I was pursuing in my 
murmur3 hashing implementation for cljs:

http://dev.clojure.org/jira/browse/CLJS-754
https://github.com/favila/clojurescript/blob/murmur3/src/cljs/cljs/core.cljs#L1111
https://github.com/favila/clojurescript/blob/murmur3/src/cljs/cljs/murmur3.cljs#L61


In practice you will hash the same most of the time if you most deal with 
integer numbers, but any doubles, bigdecimals, or large integers will still 
hash differently.

This is what happens in clojurescript now:

    (js-mod (Math/floor o) 2147483647)




On Sunday, April 19, 2015 at 11:03:08 PM UTC-5, Peter Taoussanis wrote:
> Hi there,
> 
> Am running Clojure 1.7.0-beta1, ClojureScript 0.0-3196.
> 
> Just noticed:
> (hash 1) ; 1, ClojureScript
> (hash 1 ) ; 1392991556, Clojure
> (.hashCode 1) ; 1, Clojure
> 
> i.e. numeric hashes aren't consistent between Clojure and ClojureScript.
> 
> I'm assuming that's intentional?
> 
> This got me wondering: is there an official contract somewhere describing 
> hash behaviour similarities we _can_ safely depend on?
> 
> Keywords, strings, and collections of these seem to produce matching hashes 
> (?) - but is that dependable behaviour or subject to change?
> 
> Thanks a lot , cheers! :-)

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to