Janiczek/elm-bidict/2.0.0

NOTE: the latest version is 3.1.1

MultiBiDict

A dictionary mapping unique keys to multiple values, which maintains a mapping from the values back to keys, allowing for modelling many-to-many relationships.

Example usage:

manyToMany : MultiBiDict String Int
manyToMany =
    MultiBiDict.empty
        |> MultiBiDict.insert "A" 1
        |> MultiBiDict.insert "B" 2
        |> MultiBiDict.insert "C" 3
        |> MultiBiDict.insert "A" 2


MultiBiDict.get "A" manyToMany
--> Set.fromList [1, 2]


MultiBiDict.getReverse 2 manyToMany
--> Set.fromList ["A", "B"]

Dictionaries

type MultiBiDict comparable1 comparable2

The underlying data structure. Think about it as

type alias MultiBiDict comparable1 comparable2 =
    { forward : Dict comparable1 (Set comparable2) -- just a normal Dict!
    , reverse : Dict comparable2 (Set comparable1) -- the reverse mappings!
    }

Differences from Dict

MultiBiDict comparable1 comparable2
-> Dict comparable1 (Set comparable2)

Convert MultiBiDict into a Dict. (Throw away the reverse mapping.)

Dict comparable1 (Set comparable2)
-> MultiBiDict comparable1 comparable2

Convert Dict into a MultiBiDict. (Compute the reverse mapping.)

comparable2
-> MultiBiDict comparable1 comparable2
-> Set comparable1

Get the keys associated with a value. If the value is not found, return an empty set.

uniqueValues : MultiBiDict comparable1 comparable2 -> List comparable2

Get a list of unique values in the dictionary.

uniqueValuesCount : MultiBiDict comparable1 comparable2 -> Int

Get a count of unique values in the dictionary.

MultiBiDict comparable1 comparable2
-> List ( comparable2, Set comparable1 )

Convert a dictionary into a reverse association list of value-keys pairs.

Build

empty : MultiBiDict comparable1 comparable2

Create an empty dictionary.

singleton : comparable1 -> comparable2 -> MultiBiDict comparable1 comparable2

Create a dictionary with one key-value pair.

comparable1
-> comparable2
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Insert a key-value pair into a dictionary. Replaces value when there is a collision.

comparable1
-> (Set comparable2 -> Set comparable2)
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Update the value of a dictionary for a specific key with a given function.

comparable1
-> comparable2
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Remove a single key-value pair from a dictionary. If the key is not found, no changes are made.

comparable1
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Remove all key-value pairs for the given key from a dictionary. If the key is not found, no changes are made.

Query

isEmpty : MultiBiDict comparable1 comparable2 -> Bool

Determine if a dictionary is empty.

isEmpty empty == True
member : comparable1 -> MultiBiDict comparable1 comparable2 -> Bool

Determine if a key is in a dictionary.

get :
comparable1
-> MultiBiDict comparable1 comparable2
-> Set comparable2

Get the value associated with a key. If the key is not found, return Nothing. This is useful when you are not sure if a key will be in the dictionary.

animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]


get "Tom"   animals == Just Cat
get "Jerry" animals == Just Mouse
get "Spike" animals == Nothing
size : MultiBiDict comparable1 comparable2 -> Int

Determine the number of key-value pairs in the dictionary.

Lists

keys : MultiBiDict comparable1 comparable2 -> List comparable1

Get all of the keys in a dictionary, sorted from lowest to highest.

keys (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ 0, 1 ]
values : MultiBiDict comparable1 comparable2 -> List comparable2

Get all of the values in a dictionary, in the order of their keys.

values (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ "Alice", "Bob" ]
MultiBiDict comparable1 comparable2
-> List ( comparable1, Set comparable2 )

Convert a dictionary into an association list of key-value pairs, sorted by keys.

List ( comparable1, Set comparable2 )
-> MultiBiDict comparable1 comparable2

Convert an association list into a dictionary.

Transform

map :
(comparable1 -> comparable21 -> comparable22)
-> MultiBiDict comparable1 comparable21
-> MultiBiDict comparable1 comparable22

Apply a function to all values in a dictionary.

(comparable1 -> Set comparable2 -> acc -> acc)
-> acc
-> MultiBiDict comparable1 comparable2
-> acc

Fold over the key-value pairs in a dictionary from lowest key to highest key.

getAges users =
    Dict.foldl addAge [] users


addAge _ user ages =
    user.age :: ages


-- getAges users == [33,19,28]
(comparable1 -> Set comparable2 -> acc -> acc)
-> acc
-> MultiBiDict comparable1 comparable2
-> acc

Fold over the key-value pairs in a dictionary from highest key to lowest key.

getAges users =
    Dict.foldr addAge [] users


addAge _ user ages =
    user.age :: ages


-- getAges users == [28,19,33]
(comparable1 -> Set comparable2 -> Bool)
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Keep only the key-value pairs that pass the given test.

(comparable1 -> Set comparable2 -> Bool)
-> MultiBiDict comparable1 comparable2
-> ( MultiBiDict comparable1 comparable2
, MultiBiDict comparable1 comparable2
)

Partition a dictionary according to some test. The first dictionary contains all key-value pairs which passed the test, and the second contains the pairs that did not.

Combine

MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Combine two dictionaries. If there is a collision, preference is given to the first dictionary.

MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Keep a key-value pair when its key appears in the second dictionary. Preference is given to values in the first dictionary.

MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2
-> MultiBiDict comparable1 comparable2

Keep a key-value pair when its key does not appear in the second dictionary.

(comparable1 -> Set comparable21 -> acc -> acc)
-> (comparable1 -> Set comparable21 -> Set comparable22 -> acc -> acc)
-> (comparable1 -> Set comparable22 -> acc -> acc)
-> MultiBiDict comparable1 comparable21
-> MultiBiDict comparable1 comparable22
-> acc
-> acc

The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:

  1. Only in the left dictionary.
  2. In both dictionaries.
  3. Only in the right dictionary.

You then traverse all the keys from lowest to highest, building up whatever you want.