PedroHLC / elm-uuid-dict / UUID.Dict

Fork of elm/core's Dict, bypassing the "comparable"-key limitation for UUIDs.

Also includes some extras.

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

Dictionaries


type Dict v

A dictionary of UUIDs and values.

See elm/core's Dict.Dict for the original version.

import UUID
import UUID.Dict

initUsers : Seeds -> List User -> ( Seeds, UUID.Dict User )
initUsers seed1 =
    let
        ( id1, seed2 ) =
            UUID.step seed1

        ( id2, seed3 ) =
            UUID.step seed2

        ( id3, seed4 ) =
            UUID.step seed2
    in
    UUID.Dict.fromList
        [ ( id1, User "Alice" 28 1.65 )
        , ( id2, User "Bob" 19 1.82 )
        , ( id3, User "Chuck" 33 1.75 )
        ]
        |> Tuple.pair seed4

type alias User =
    { name : String
    , age : Int
    , height : Float
    }

Build

empty : Dict v

Create an empty dictionary.

singleton : UUID -> v -> Dict v

Create a dictionary with one key-value pair.

insert : UUID -> v -> Dict v -> Dict v

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

update : UUID -> (Maybe v -> Maybe v) -> Dict v -> Dict v

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

remove : UUID -> Dict v -> Dict v

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

Query

isEmpty : Dict v -> Basics.Bool

Determine if a dictionary is empty. isEmpty empty == True

member : UUID -> Dict v -> Basics.Bool

Determine if a key is in a dictionary.

get : UUID -> Dict 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.

See elm/core's Dict.get for the original version.

size : Dict v -> Basics.Int

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

Lists

keys : Dict v -> List UUID

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

See elm/core's Dict.keys for the original version.

values : Dict v -> List v

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

See elm/core's Dict.values for the original version.

toList : Dict v -> List ( UUID, v )

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

fromList : List ( UUID, v ) -> Dict v

Convert an association list into a dictionary.

Transform

map : (UUID -> a -> b) -> Dict a -> Dict b

Apply a function to all values in a dictionary.

foldl : (UUID -> v -> b -> b) -> b -> Dict v -> b

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

See elm/core's Dict.foldl for the original version.

foldr : (UUID -> v -> b -> b) -> b -> Dict v -> b

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

See elm/core's Dict.foldr for the original version.

filter : (UUID -> v -> Basics.Bool) -> Dict v -> Dict v

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

partition : (UUID -> v -> Basics.Bool) -> Dict v -> ( Dict v, Dict v )

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

union : Dict v -> Dict v -> Dict v

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

intersect : Dict v -> Dict v -> Dict v

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

diff : Dict a -> Dict b -> Dict a

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

merge : (UUID -> a -> result -> result) -> (UUID -> a -> b -> result -> result) -> (UUID -> b -> result -> result) -> Dict a -> Dict b -> result -> result

Combines two dictionaries.

See elm/core's Dict.merge for the original version.

Extra

getFirst : Dict v -> Maybe ( UUID, v )

Retrieve the node with the smallest key.

findl : (UUID -> v -> Basics.Bool) -> Dict v -> Maybe ( UUID, v )

Retrieve the first node that matches a predicate.

findr : (UUID -> v -> Basics.Bool) -> Dict v -> Maybe ( UUID, v )

Retrieve the last node that matches a predicate.

toggle : UUID -> v -> Dict v -> Dict v

Like insert, but removes if the key already exists in the dictionary, discarding the new value too.