for more information visit the package's GitHub page
Package contains the following modules:
An association list is a list of tuples that map unique keys to values. The keys can be of any type (so long as it has a reasonable definition for equality). This includes pretty much everything except for functions and things that contain functions.
This library is intended to be used as a drop-in replacement for the Dict
module in elm/core. You might use it like so,
import AssocList as Dict exposing (Dict)
type Character
= Ariel
| Simba
| Mufasa
| Woody
type Movie
= LittleMermaid
| LionKing
| ToyStory
characterToMovie : Dict Character Movie
characterToMovie =
Dict.fromList
[ ( Ariel, LittleMermaid )
, ( Simba, LionKing )
, ( Mufasa, LionKing )
, ( Woody, ToyStory )
]
Dict.get Simba characterToMovie --> Just LionKing
(Note the use of a custom type as the dictionary key, which is not possible with the Dict
module in elm/core!)
Since this library does not require your keys to be comparable
, some operations are asymptotically slower than those in the Dict
module in elm/core. The good news is that if you are working with small-ish dictionaries, this is likely not a problem. Furthermore, the bottleneck point in most Elm programs is DOM manipulation, so slower data structure operations are unlikely to cause a noticeable difference in how your app performs. For a detailed comparison of the performance characteristics of the two implementations, see Performance.md.
All the existing libraries that I have found that attempt to solve the dictionary with non-comparable keys problem suffer from at least one of the following issues:
stores a function for converting keys to comparable
within the data structure itself
==
operator to compare the structuresModel
or Msg
types")does not provide full type-level safety against operating on two dictionaries with different comparators, e.g. union (singleton identity 0 'a') (singleton (\x -> x + 1) 1 'b')
Here is a detailed analysis of all the relevant libraries I could find:
turboMaCk/any-dict
rtfeldman/elm-sorter-experiment
jjant/elm-dict
Dict
API from elm/coreeeue56/elm-all-dict
robertjlooby/elm-generic-dict
Although not the primary problem that this library aims to solve, assoc-list can also be used as an ordered dictionary, i.e. a dictionary that keeps track of the order in which entries were inserted. This functionality is similar to the following libraries:
y0hy0h/ordered-containers
comparable
wittjosiah/elm-ordered-dict
comparable
rnons/ordered-containers
comparable
eliaslfox/orderedmap
comparable