HashMap equality calculated incorrectly
See original GitHub issueThe following hashCodes should not be equal, but they are:
expect(HashMap.of(["a", 1], ["b", 0]).hashCode()).not.toBe(HashMap.of(["a", 0], ["b", 1]).hashCode());
The reason for this is that the HashMap hash code is calculated for the pairs in isolation, combining the hash codes of the kv-pairs and summing them up.
Calculating the pairs in isolation is correct because I think the order is not guaranteed in a HashMap (otherwise you could just send the whole list entries to fieldsToHashCode instead).
Adding up the hash code of the key and the value is not, however, because that produces the false match as demonstrated above, as adding doesn’t guarantee the uniqueness of the pairs of keys and values.
Something like this should fix it in the HashMap implementation:
HashMap.prototype.hashCode = function () {
return this.hamt.fold(function (acc, value, key) {
return acc + 37 * (Comparison_1.getHashCode(key) * Comparison_1.getHashCode(value));
}, 0);
};
(Thank you for creating prelude-ts, by the way, our team has gotten a lot of value out of this library and I absolutely love it).
Issue Analytics
- State:
- Created a year ago
- Comments:14 (14 by maintainers)
Top GitHub Comments
Hi again! I contributed a while ago but have a new github account, so if you see a ghost commit somewhere, that’s me
I wanted to see if I could get the tests to run with the given instructions, but with a fresh install of nodejs Gallium, simply running
npm i
fails for me as well 😃 could you maybe provide your lockfile, just to make sure its not an upstream package issue?published 1.0.5 on npm with the changes, that should be it! thank you again!