mathiajusth / nonempty-dict / Dict.Nonempty

A dict that is guaranteed to have an entry in it.

Type


type NonemptyDict comparable v

A dict with at least one entry. The inner dict always contains all of the information. The inner tuple is a redundancy to ensure the nonemptiness and doesn't affect equality of NonemptyDicts

Construction

singleton : comparable -> v -> NonemptyDict comparable v

Initiate a NonemptyDict out of k v pair

fromNonemptyList : List.Nonempty.Nonempty ( comparable, v ) -> NonemptyDict comparable v

Create a NonemptyDict out of a NonemptyList. Prefers last value if keys clash.

fromList : ( comparable, v ) -> List ( comparable, v ) -> NonemptyDict comparable v

Create a NonemptyDict out of k v pair and a List of k v pairs. Prefers value provided in the first argument if keys clash

Member control

insert : comparable -> v -> NonemptyDict comparable v -> NonemptyDict comparable v

Same as Dict.insert

remove : comparable -> NonemptyDict comparable v -> Maybe (NonemptyDict comparable v)

Same as Dict.remove but fails with Nothing if you remove the only entry that was left

Transformation

toDict : NonemptyDict comparable v -> Dict comparable v

Transform NonemptyDict into regular Dict

toList : NonemptyDict comparable v -> List ( comparable, v )

Same as Dict.toList

toNonemptyList : NonemptyDict comparable v -> List.Nonempty.Nonempty ( comparable, v )

Transform NonemptyDict into List.Nonempty.Nonempty

Destruction

get : comparable -> NonemptyDict comparable v -> Maybe v

Same as Dict.get

head : NonemptyDict comparable v -> ( comparable, v )

Get the key value pair corresponding to the lowest key

equal : NonemptyDict comparable v -> NonemptyDict comparable v -> Basics.Bool

Equality on nonempty Dicts. We cannot use build in == as the guaranteed key-value pair doesn't matter for equality.