lamdera/containers/1.0.0

A Lamdera package. It is not on package.elm-lang.org: the Lamdera compiler provides it, from static.lamdera.com, so lamdera install works and elm install does not.

SeqDict

A dictionary mapping unique keys to values where insertion order is preserved for functions like foldl and toList.

Seq Dictionary

type SeqDict k v

A dictionary of keys and values. So a (Dict String User) is a dictionary that lets you look up a String (such as user names) and find the associated User.

This implementation differs from the one in core in that keys don't have to be comparable, and that operations like foldl and foldr iterates the key-value pairs in insertion order instead of sorted order.

Build

The empty dictionary.

singleton : k -> v -> SeqDict k v

Create a dictionary with one key-value pair.

insert : k -> v -> SeqDict k v -> SeqDict k v

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

update : k -> (Maybe v -> Maybe v) -> SeqDict k v -> SeqDict k v

Update the value of a dictionary for a specific key with a given function. The given function gets the current value as a parameter and its return value determines if the value is updated or removed. New key-value pairs can be inserted too.

updateIfExists : k -> (v -> v) -> SeqDict k v -> SeqDict k v

Update the value of a dictionary only if the specified key is present in the dictionary.

remove : k -> SeqDict k v -> SeqDict k v

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

Query

get : k -> SeqDict k v -> Maybe v

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
isEmpty : SeqDict k v -> Bool

Determine if a dictionary is empty.

isEmpty empty == True
member : k -> SeqDict k v -> Bool

Determine if a key is in a dictionary.

size : SeqDict k v -> Int

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

unorderedEquals : SeqDict k v -> SeqDict k v -> Bool

Check if two dictionaries are equal regardless what order items were inserted. Note that this is not the same as writing dict1 == dict2. Insertion order matters when using ==.

dict1 = SeqDict.fromList [ ( "A", 1 ), ( "B", 2 ) ]
dict2 = SeqDict.fromList [ ( "B", 2 ), ( "A", 1 ) ]


SeqDict.unorderedEquals dict1 dict2 -- True
dict1 == dict2 -- False

Combine

union : SeqDict k v -> SeqDict k v -> SeqDict k v

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

intersect : SeqDict k v -> SeqDict k v1 -> SeqDict k v

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

diff : SeqDict k v -> SeqDict k v1 -> SeqDict k v

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

(k -> a -> result -> result)
-> (k -> a -> b -> result -> result)
-> (k -> b -> result -> result)
-> SeqDict k a
-> SeqDict k b
-> result
-> result

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 by insertion order, starting with the left dictionary and then the right dictionary (keys that appear in both are skipped for the right dictionary)

Lists

toList : SeqDict k v -> List ( k, v )

Convert a dictionary into an association list of key-value pairs.

fromList : List ( k, v ) -> SeqDict k v

Convert an association list into a dictionary.

keys : SeqDict k v -> List k

Get all of the keys in a dictionary.

   keys (fromList [(0,"Alice"),(1,"Bob")]) == [0,1]
values : SeqDict k v -> List v

Get all of the values in a dictionary as a List.

   values (fromList [(0,"Alice"),(1,"Bob")]) == ["Alice", "Bob"]

Transform

foldl : (k -> v -> b -> b) -> b -> SeqDict k v -> b

Fold over the key-value pairs in a dictionary in the order the key was first inserted.

foldr : (k -> v -> b -> b) -> b -> SeqDict k v -> b

Fold over the key-value pairs in a dictionary in the reverse order the key was first inserted.

map : (k -> a -> b) -> SeqDict k a -> SeqDict k b

Apply a function to all values in a dictionary.

filter : (k -> v -> Bool) -> SeqDict k v -> SeqDict k v

Keep a key-value pair when it satisfies a predicate.

filterMap : (k -> v -> Maybe v2) -> SeqDict k v -> SeqDict k v2

Filter out certain key-value pairs while also mapping over the values.

partition : (k -> v -> Bool) -> SeqDict k v -> ( SeqDict k v, SeqDict k v )

Partition a dictionary according to a predicate. The first dictionary contains all key-value pairs which satisfy the predicate, and the second contains the rest.

Internal

encodeDict : (key -> Encoder) -> (value -> Encoder) -> SeqDict key value -> Encoder

The Lamdera compiler relies on this function, it is not intended to be used directly. Vendor this function in your own codebase if you want to use it, as the encoding can change without notice.

decodeDict : Decoder k -> Decoder value -> Decoder (SeqDict k value)

The Lamdera compiler relies on this function, it is not intended to be used directly. Vendor this function in your own codebase if you want to use it, as the encoding can change without notice.