Modified versions of Dict and Set from elm/core
. The keys can be any (non-function) type. A comparison function is used for every action that needs to compare keys.
- The comparison function is
k -> k -> Order
forDictAny k v
- and
a -> a -> Order
forSetAny a
.
import SetAny as Set exposing (Set)
items : Set Item
items = [ Brush, Soap, Towel ] |> Set.fromList comparer
type Item
= Brush
| Soap
| Shampoo
| Towel
itemToInt : Item -> Int
itemToInt Item =
case Item of
Brush -> 1
Soap -> 2
Shampoo -> 3
Towel -> 42
comparer : Item -> Item -> Order
comparer a b = compare (itemToInt a) (itemToInt b)
items |> Set.member comparer Shampoo --> False
items |> Set.member comparer Towel --> True
items |> Set.toList --> [Towel, Soap, Brush]
- No functions stored in the model.
- No extra
comparable
type in the signatures.
Some other any key type implementations use a k -> comparable
function, which requires the type signatures to include the comparable
type, this is not necessary with this package.
The code is cloned from elm/core
and the ordering function added. Performance depends on the complexity of the comparison function, but otherwise the same as the elm/core
versions.
The correct comparison function must be passed in every time, no compile time checking for this. If the Dict and Set code in elm/core
is updated in the future, this package will also need to be updated to reflect the changes.
I did try changing the internal names RBNode_elm_builtin
and RBEmpty_elm_builtin
, but the tests then failed. I found that these are magic values in the elm compiler, see here:
github.com/elm/compiler.