Search code examples
elm

How to make a set of unique records in Elm


I need a set of unique records. But Elm Core's Set restricts set members to comparable:

import Set exposing (Set)

mySet = Set.empty
Set.insert {name="Foo"} mySet
-- TYPE MISMATCH ----------------------------------------------------------- elm

The 1st argument to `insert` is not what I expect:

6|   Set.insert {name="Foo"} mySet
                ^^^^^^^^^^^^
This argument is a record of type:

    { name : String }

But `insert` needs the 1st argument to be:

    comparable

Hint: Only ints, floats, chars, strings, lists, and tuples are comparable.

How do you make a set of records in Elm?


Solution

  • With the standard library you can't. As noted in the "Hint" you could use a tuple, but these are limited to 3-tuples in 0.19.

    So, I think your best bet is to use https://package.elm-lang.org/packages/Gizra/elm-all-set/latest/EverySet