Whenever I define a hash set in Clojure with the literal notation it scrambles the values around like such:
user=> #{1 2 4 6 5}
It returned this:
#{1 4 6 2 5}
But when I put
user=> #{1 4 6 2 5}
It returned:
user=> #{1 4 6 2 5}
What's the logic behind this?
Sets are, by definition, unordered. So as a matter of interface, the order is arbitrary. However, a hash set will undoubtedly store its members in some sort of hash table. So the natural way to enumerate the members of a hash set would be to iterate over the internal hash table. Therefore, the order in which the elements are produced will depend on the hash values of the elements, and how those values map to the underlying hash table.
That is why the "ordering" of elements appears to be random, but repeatable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With