Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

literal hash set in clojure

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?

like image 475
Kevin Behan Avatar asked Jan 29 '26 22:01

Kevin Behan


1 Answers

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.

like image 177
Nathan Davis Avatar answered Jan 31 '26 23:01

Nathan Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!