noordstar/elm-iddict/1.0.1

Iddict

The id-dict is a data type that lets us store values in a dictionary using unique identifiers. This can be used as a dictionary where the keys do not matter.

The benefit of the id-dict is that it generates the keys FOR you. This way, you do not need to generate identifiers yourself.

Id-dict

type Iddict a

The Iddict data type.

Build

Create an empty id-dict.

singleton : a -> ( Int, Iddict a )

Create an id-dict with a single value.

insert : a -> Iddict a -> ( Int, Iddict a )

Insert a new value into the id-dict. Given that the id-dict generates its key, the function returns both the updated id-dict as the newly generated key.

x = empty |> insert "hello" -- ( 0, <Iddict with value "hello"> )


case x of
    ( _, id-dict ) ->
        get 0 id-dict -- Just "hello"
update : Int -> (Maybe a -> Maybe a) -> Iddict a -> Iddict a

Update the value of an id-dict for a specific key with a given function. This is also the only option that allows you to insert values at a specific key, so be careful with this feature!

remove : Int -> Iddict a -> Iddict a

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

Query

isEmpty : Iddict a -> Bool

Determine if an id-dict is empty.

member : Int -> Iddict a -> Bool

Determine if a key is in an id-dict.

get : Int -> Iddict a -> Maybe a

Get a value from the id-dict using its key.

size : Iddict a -> Int

Determine the number of key-value pairs in the id-dict.

Lists

keys : Iddict a -> List Int

Get all of the keys from the id-dict, sorted from lowest to highest.

values : Iddict a -> List a

Get all of the values from an id-dict, in the order of their keys.

toList : Iddict a -> List ( Int, a )

Convert an id-dict into an association list of key-value pairs, sorted by keys.

fromList : List ( Int, a ) -> Iddict a

Convert an association list into an id-dict.

Transform

map : (Int -> a -> b) -> Iddict a -> Iddict b

Apply a function to all values in an id-dict.

foldl : (Int -> a -> b -> b) -> b -> Iddict a -> b

Fold over the key-value pairs in an id-dict from lowest key to highest key.

foldr : (Int -> a -> b -> b) -> b -> Iddict a -> b

Fold over the key-value pairs in an id-dict from highest key to lowest key.

filter : (Int -> a -> Bool) -> Iddict a -> Iddict a

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

partition : (Int -> a -> Bool) -> Iddict a -> ( Iddict a, Iddict a )

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

Combine

union : Iddict a -> Iddict a -> Iddict a

Combine two id-dicts. If there is a collision, preference is given to the first id-dict.

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

diff : Iddict a -> Iddict a -> Iddict a

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

(Int -> a -> result -> result)
-> (Int -> a -> b -> result -> result)
-> (Int -> b -> result -> result)
-> Iddict a
-> Iddict b
-> result
-> result

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

1. Only in the left id-dict.
2. In both id-dicts.
3. Only in the right id-dict.

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

JSON coders

encode : (a -> Value) -> Iddict a -> Value

Encode an id-dict to a JSON value.

decoder : Decoder a -> Decoder (Iddict a)

Decode an id-dict from a JSON value.