https://github.com/whiletruu/elm-one-to-one
A data structure for one-to-one mapping between values.
https://github.com/whiletruu/elm-one-to-one
Last synced: about 1 month ago
JSON representation
A data structure for one-to-one mapping between values.
- Host: GitHub
- URL: https://github.com/whiletruu/elm-one-to-one
- Owner: WhileTruu
- License: bsd-3-clause
- Created: 2021-04-25T18:35:38.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-25T18:42:29.000Z (about 5 years ago)
- Last Synced: 2025-01-04T15:15:16.357Z (over 1 year ago)
- Language: Elm
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OneToOne
A data structure for one-to-one mapping between values. A 'OneToOne' is essentially
a [bijection](https://en.wikipedia.org/wiki/Bijection) between subsets of
its two argument types.
Every `first` value is associated with exactly one `second` value and vice versa.
Compare this to a [Dict](https://package.elm-lang.org/packages/elm/core/latest/Dict)
, where every key is associated with exactly one value, but a value can be associated
with more than one key.
Internally, a `OneToOne` one is composed of two dictionaries, one for the first-to-second direction
and one for second-to-first. As such, insert, remove, and query operations all
have the same big-O performance as dictionaries.
```elm
import OneToOne exposing (OneToOne)
myOneToOne : OneToOne String Int
myOneToOne =
OneToOne.empty
|> OneToOne.insert "potato" 100
|> OneToOne.insert "cabbage" 200
|> OneToOne.insert "tomato" 300
|> OneToOne.insert "tomato" 100
OneToOne.toList myOneToOne
--> [ ( "cabbage", 200 ), ( "tomato", 100 ) ]
```
## Tests
This package uses
[elm-test](https://github.com/elm-explorations/test) and
[elm-verify-examples](https://github.com/stoeffel/elm-verify-examples)