Cindiary / elm-not-empty / NotEmpty.Dict


type alias Dict key value =
( ( key, value ), Dict key value )

Not empty version of Dict

Create

singleton : key -> value -> Dict key value

Create a non empty dictionary with one key-value pair.

withPair : comparable -> value -> Dict comparable value -> Dict comparable value

Create a non empty dictionary from a regular dictionary and a given pair.

In contrast to fromDict this will always return a value since there is always at least 1 pair.

dict = withPair 5 "Five" ( Dict.fromList [ ( 4, "Four" ), ( 3, "Three" ) ] )
toDict dict == Dict.fromList [ ( 3, "Three" ), ( 4, "Four" ), ( 5, "Five" ) ]

fromList : NotEmpty.List.List ( comparable, value ) -> Dict comparable value

Same as Dict.fromList except using a NotEmpty.List

fromListWithPair : comparable -> value -> List ( comparable, value ) -> Dict comparable value

Same as withPair but with a list or pairs instead of a dict

Transforming from and to regular dictionaries

fromDict : Dict comparable value -> Maybe (Dict comparable value)

Convert a regular dict into a not empty dict, or Nothing if the dict is empty

fromDict ( Dict.fromList [] ) == Nothing
fromDict ( Dict.fromList [ ( 1, "one" ), ( 2, "two" ), ( 3, "three" ) ] ) == Just ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] )

toDict : Dict comparable value -> Dict comparable value

Convert a not empty dict into a regular dict

toDict ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == Dict.fromList [ ( 1, "one" ), ( 2, "two" ), ( 3, "three" ) ]
toDict ( singleton 1 "one" ) == Dict.fromList [ ( 1, "one" ) ]

New functions

head : Dict key value -> ( key, value )

Extract the first pair from a not empty dictionary.

head ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == ( 1, "one" )
head ( singleton 1 "one" ) == ( 1, "one" )

last : Dict key value -> ( key, value )

Extract the last pair from a not empty dictionary.

last ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == ( 3, "three" )
last ( singleton 1 "one" ) == ( 1, "one" )

tail : Dict key value -> Dict key value

Returns the elements after the first pair as a regular dictionary

tail ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == Dict.fromList [ ( 2, "two" ), ( 3, "three" ) ]
tail ( singleton 1 "one" ) == Dict.fromList []

hasTail : Dict key value -> Basics.Bool

Returns wether the dictionary has a tail with elements

hasTail ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == True
hasTail ( singleton 1 "one" ) == False
hasTail ( fromListWithHead 1 "one" [] ) == False

Modified functions

remove : comparable -> Dict comparable value -> Maybe (Dict comparable value)

Same as Dict.remove but will return Nothing if the last value is removed

remove 1 ( fromListWithPair 1 "one" [ ( 2, "two" ), ( 3, "three" ) ] ) == Just ( fromListWithPair 2 "two" [ ( 3, "three" ) ] )
remove 1 ( singleton 1 "one" ) == Nothing
remove 2 ( singleton 1 "one" ) == Just ( singleton 1 "one" )

filter : (comparable -> value -> Basics.Bool) -> Dict comparable value -> Maybe (Dict comparable value)

Same as Dict.filter but will return Nothing if no pairs satisfy the test

filter isKeyEven ( fromListWithPair 1 1 [ ( 2, 2 ), ( 3, 3 ) ] ) == Just ( fromListWithPair 2 2 [] )
filter isKeyEven ( singleton 1 1 ) == Nothing

diff : Dict comparable value -> Dict comparable a -> Maybe (Dict comparable value)

Same as Dict.diff but will return Nothing if the resulting dictionary is empty

diff ( fromListWithPair 1 1 [ ( 2, 2 ), ( 3, 3 ) ] ) ( singleton 1 "one" ) == Just ( fromListWithPair 2 2 [ ( 3, 3 ) ] )
diff ( singleton 1 1 ) ( singleton 1 "one" ) == Nothing
diff ( singleton 2 2 ) ( singleton 1 "one" ) == Just ( singleton 2 2 )

intersect : Dict comparable value -> Dict comparable a -> Maybe (Dict comparable value)

Same as Dict.intersect but will return Nothing if the resulting dictionary is empty

intersect ( fromListWithPair 1 1 [ ( 2, 2 ), ( 3, 3 ) ] ) ( singleton 1 "one" ) == Just ( fromListWithPair 1 1 [] )
intersect ( singleton 1 1 ) ( singleton 1 "one" ) == Just ( singleton 1 1 )
intersect ( singleton 2 2 ) ( singleton 1 "one" ) == Nothing

Unchanged functions

get : comparable -> Dict comparable value -> Maybe value

Same as Dict.get

member : comparable -> Dict comparable value -> Basics.Bool

Same as Dict.member

insert : comparable -> value -> Dict comparable value -> Dict comparable value

Same as Dict.insert

size : Dict key value -> Basics.Int

Same as Dict.size

toList : Dict key value -> NotEmpty.List.List ( key, value )

Same as Dict.toList

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

Same as Dict.map

foldl : (key -> value -> b -> b) -> b -> Dict key value -> b

Same as Dict.foldl

foldr : (key -> value -> b -> b) -> b -> Dict key value -> b

Same as Dict.foldr

union : Dict comparable value -> Dict comparable value -> Dict comparable value

Same as Dict.union

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

Same as Dict.merge