Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pzp1997/assoc-list
Dictionary with custom keys implemented using association lists
https://github.com/pzp1997/assoc-list
association-list dictionary elm
Last synced: about 1 month ago
JSON representation
Dictionary with custom keys implemented using association lists
- Host: GitHub
- URL: https://github.com/pzp1997/assoc-list
- Owner: pzp1997
- License: bsd-3-clause
- Created: 2018-12-31T02:55:52.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-19T15:41:39.000Z (5 months ago)
- Last Synced: 2024-08-05T20:31:48.057Z (5 months ago)
- Topics: association-list, dictionary, elm
- Language: Elm
- Homepage: https://package.elm-lang.org/packages/pzp1997/assoc-list/latest/
- Size: 176 KB
- Stars: 30
- Watchers: 1
- Forks: 4
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Association lists
[![Build Status](https://travis-ci.com/pzp1997/assoc-list.svg?branch=master)](https://travis-ci.com/pzp1997/assoc-list)
An [association list](https://en.wikipedia.org/wiki/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.
## Usage
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,
```elm
import AssocList as Dict exposing (Dict)type Character
= Ariel
| Simba
| Mufasa
| Woodytype Movie
= LittleMermaid
| LionKing
| ToyStorycharacterToMovie : 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!)
## Performance
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](https://github.com/pzp1997/assoc-list/blob/master/Performance.md).
## Comparison to existing work
### Dictionary with non-comparable keys
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:
1. stores a function for converting keys to `comparable` within the data structure itself
- can cause runtime errors should you ever use the `==` operator to compare the structures
- makes serialization trickier (for this reason, conventional wisdom states that you should "never put functions in your `Model` or `Msg` types")
- see this [Discourse post](https://discourse.elm-lang.org/t/consequences-of-functions-in-the-model-with-0-19) and this [Elm Discuss thread](https://groups.google.com/forum/#!topic/elm-discuss/bOAHwSnklLc) for more information2. 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
- suffers from problems (1) and (2)
rtfeldman/elm-sorter-experiment
- suffers from problem (1)
jjant/elm-dict
- similar to problem (1), the data structure itself is actually a function
- does not support the entire `Dict` API from elm/coreeeue56/elm-all-dict
- suffers from problems (1) and (2)
- some parts of the library rely on Kernel code, making it non-trivial to update to 0.19robertjlooby/elm-generic-dict
- suffers from problem (1)
- has not been updated to 0.19 as of time of writing### Ordered dictionary
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
- requires the keys to be `comparable`
wittjosiah/elm-ordered-dict
- requires the keys to be `comparable`
- has not been updated to 0.19 as of time of writingrnons/ordered-containers
- requires the keys to be `comparable`
- has not been updated to 0.19 as of time of writingeliaslfox/orderedmap
- requires the keys to be `comparable`
- has not been updated to 0.19 as of time of writing