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.
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 ) ]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 ) ]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 ) ]Determine if a one-to-one mapping is empty.
isEmpty empty --> True
isEmpty (singleton 1 1) --> FalseDetermine if a first value is in a one-to-one mapping.
memberFirst 1 (singleton 1 2) --> True
memberFirst 2 (singleton 1 2) --> FalseDetermine if a second value is in a one-to-one mapping.
memberSecond 2 (singleton 1 2) --> True
memberSecond 1 (singleton 1 2) --> FalseExtract 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 --> NothingExtract 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 --> NothingDetermine the number of pairs in a one-to-one mapping.
animalNamesToNumbers : OneToOne String Int
animalNamesToNumbers =
fromList [ ( "Tom", 1 ), ( "Jerry", 2 ) ]
size animalNamesToNumbers --> 2Convert a one-to-one mapping into an association list of pairs, sorted by the first value.
toList (singleton "tomato" "cabbage") --> [ ( "tomato", "cabbage" ) ]
Represents a one-to-one mapping between values. So a
OneToOne String Intis a one-to-one mapping that lets you look up aStringand find the associatedIntand vice versa.