# Introducing assoc-list, a Dict substitute with custom keys

**URL:** <https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901>\
**Category:** Request Feedback\
**Created:** [January 7, 2019, 10:27pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901 "2019-01-07T22:27:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![palmer](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/palmer/32/1675_2.png) [@palmer](https://discourse.elm-lang.org/u/palmer)\
**Post date:** [January 7, 2019, 10:27pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/1 "2019-01-07T22:27:14Z")

</div>

I recently published the [pzp1997/assoc-list](https://package.elm-lang.org/packages/pzp1997/assoc-list/1.0.0/) package, which aims to address the problem that the `Dict` module in elm/core does not work with custom keys.

While there are already several packages that attempt to solve this problem, all of the ones I have found store a function for converting the keys to `comparable` within the data structure itself. (The downside here is that storing functions in your data can cause runtime errors and makes serialization more challenging.) I decided to approach the problem from a different angle by using a data structure called an [association list](https://en.wikipedia.org/wiki/Association_list) which only needs keys to be equatable rather than comparable. For better or worse, all types in Elm are equatable, so pretty much any type can be used as a key “right out of the box” in my library.

Intrigued? Check out [https://package.elm-lang.org/packages/pzp1997/assoc-list/1.0.0/](https://package.elm-lang.org/packages/pzp1997/assoc-list/1.0.0/) for more information! Over there I explain how to use the library (spoiler: it’s a drop-in replacement for elm/core `Dict`), performance considerations, and take a deeper look at existing alternatives.

(And as an added bonus, you can also use the library as an ordered dictionary!)

---

<div class="post-metadata">

**Author:** ![kevinyank](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/kevinyank/32/241_2.png) [@kevinyank](https://discourse.elm-lang.org/u/kevinyank)\
**Post date:** [January 8, 2019, 11:54am UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/2 "2019-01-08T11:54:13Z")

</div>

Super cool! I love a well-explained pragmatic solution to a theoretically difficult problem like this. Thanks for releasing this and putting the extra effort into the documentation.

---

<div class="post-metadata">

**Author:** ![Dan\_Abrams](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dan_abrams/32/3171_2.png) [@Dan\_Abrams](https://discourse.elm-lang.org/u/Dan_Abrams)\
**Post date:** [January 8, 2019, 4:28pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/3 "2019-01-08T16:28:09Z")

</div>

You continue to impress, Palmer.

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [January 8, 2019, 4:52pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/4 "2019-01-08T16:52:55Z")

</div>

Simple and effective, thank you very much ❤  
It’s also great how you took care of the documentation  
([performance.md](https://github.com/pzp1997/assoc-list/blob/master/Performance.md)), tests and implementation (Tail Call Elimination).

Now we need a `Set`, but it is not hard from your work.  
[Chadtech/unique-list](https://package.elm-lang.org/packages/Chadtech/unique-list/latest/List-Unique) was actually quite close.

---

<div class="post-metadata">

**Author:** ![madasebrof](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/madasebrof/32/363_2.png) [@madasebrof](https://discourse.elm-lang.org/u/madasebrof)\
**Post date:** [January 9, 2019, 9:56pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/5 "2019-01-09T21:56:38Z")

</div>

Wow, epic! Could totally use this!

We have massive forms that we store user input field values in a `Dict String AnswerValue`. Would be super awesome to swap that for this so we can use a `custom type` aka `union type` instead of a `String` to keep track of the field names (to guarantee type)!

Is it just a wrapper around a list? E.g. `List ( CustomTypeKey, CustomTypeValue )`, then to find a `key`, you just iterate through the list, e.g. O(n) and return if the keys match?

Just looked at the code. Yup.

Rock on,

Adam

---

<div class="post-metadata">

**Author:** ![madasebrof](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/madasebrof/32/363_2.png) [@madasebrof](https://discourse.elm-lang.org/u/madasebrof)\
**Post date:** [January 9, 2019, 10:49pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/6 "2019-01-09T22:49:49Z")

</div>

What would be interesting to see is the performance with specific sized lists, e.g. 10, 100, 500, 1000, 2500, 10000 just to see where the “slowdown” vs. a regular `Dict` occurs.

In Elixir, we use the list structure all over the place as most of the time you are iterating through a relatively small list (e.g. sub 500 items max, mostly much fewer like 10 to 100 items). Generally speaking, if it’s too big to work with a list, the data is probably in the database.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [January 19, 2019, 10:49pm UTC](https://discourse.elm-lang.org/t/introducing-assoc-list-a-dict-substitute-with-custom-keys/2901/7 "2019-01-19T22:49:51Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
