WhileTruu / elm-one-to-one / OneToOne

A one-to-one mapping between values. The values can be any comparable type. This includes Int, Float, Time, Char, String, and tuples or lists of comparable types.

Insert, remove, and query operations all take O(log n) time.

One-to-one correspondence


type OneToOne a b

Represents a one-to-one mapping between values. So a OneToOne String Int is a one-to-one mapping that lets you look up a String and find the associated Int and vice versa.

Build

empty : OneToOne a b

Create an empty one-to-one mapping.

empty --> fromList []

singleton : comparableA -> comparableB -> OneToOne comparableA comparableB

insert : comparableA -> comparableB -> OneToOne comparableA comparableB -> OneToOne comparableA comparableB

Insert a pair of values into a one-to-one mapping. Removes overlapping pairs when there is a collision.

oneToOne : OneToOne Int Int
oneToOne =
    empty
        |> insert 1 1
        |> insert 2 1

oneToOne --> fromList [ ( 2, 1 ) ]

removeFirst : comparableA -> OneToOne comparableA comparableB -> OneToOne comparableA comparableB

Remove a pair from a one-to-one mapping by the first value. If the pair is not found, no changes are made.

animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
    fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

removeFirst "Tom" animalNamesToNumbers --> fromList [ ( "Jerry", 2 ) ]

removeFirst "Quacker" animalNamesToNumbers --> fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

removeSecond : comparableB -> OneToOne comparableA comparableB -> OneToOne comparableA comparableB

Remove a pair from a one-to-one mapping by the second value. If the pair is not found, no changes are made.

animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
    fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

removeSecond 1 animalNamesToNumbers --> fromList [ ( "Jerry", 2 ) ]

removeSecond 3 animalNamesToNumbers --> fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

Query

isEmpty : OneToOne a b -> Basics.Bool

Determine if a one-to-one mapping is empty.

isEmpty empty --> True

isEmpty (singleton 1 1) --> False

memberFirst : comparableA -> OneToOne comparableA b -> Basics.Bool

Determine if a first value is in a one-to-one mapping.

memberFirst 1 (singleton 1 2) --> True

memberFirst 2 (singleton 1 2) --> False

memberSecond : comparableB -> OneToOne a comparableB -> Basics.Bool

Determine if a second value is in a one-to-one mapping.

memberSecond 2 (singleton 1 2) --> True

memberSecond 1 (singleton 1 2) --> False

first : comparableB -> OneToOne a comparableB -> Maybe a

Extract the first value associated with a second value. If the second value is not found, return Nothing.

animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
    fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

first 1 animalNamesToNumbers --> Just "Tom"
first 2 animalNamesToNumbers --> Just "Jerry"
first 3 animalNamesToNumbers --> Nothing

second : comparableA -> OneToOne comparableA b -> Maybe b

Extract the second value associated with a first value. If the first value is not found, return Nothing.

animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
    fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

second "Tom" animalNamesToNumbers --> Just 1
second "Jerry" animalNamesToNumbers --> Just 2
second "Quacker" animalNamesToNumbers --> Nothing

size : OneToOne first second -> Basics.Int

Determine the number of pairs in a one-to-one mapping.

animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
   fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]

size animalNamesToNumbers --> 2

Lists

toList : OneToOne a b -> List ( a, b )

Convert a one-to-one mapping into an association list of pairs, sorted by the first value.

toList (singleton "tomato" "cabbage") --> [ ( "tomato", "cabbage" ) ]

fromList : List ( comparableA, comparableB ) -> OneToOne comparableA comparableB

Convert an association list into a one-to-one mapping.

fromList [ ( "tomato", "cabbage" ) ] --> (singleton "tomato" "cabbage")