Search code examples
setelixirets

Elixir - ETS with custom equals/hash function


In Java, the equals/hash functions can be customised simply by overriding/implementing methods on a class.

This is very useful when you want to customise uniqueness of your class - so that you can check for 'duplicates' in a set easily.

How would you do the same thing in Elixir, specifically with ETS?


Solution

  • One way to do what I need to do is by making a unique hash function (that can return any type). There should only be one unique output of this hash function per unique input.

    Then you can store the {hash, val} tuples:

    table = :ets.create(:table, [])
    :ets.insert(table, {hash(val), val})
    :ets.lookup(table, hash(val))