Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a set of unique records in Elm

Tags:

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?

like image 817
Holger L Avatar asked Apr 23 '19 09:04

Holger L


1 Answers

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

like image 164
Simon H Avatar answered Nov 05 '22 00:11

Simon H