bundsol / boxed / Boxed.Dictionary

Some well known tools carried over from the Dict module, and others derived from it. Since the members of a dictionary are Boxed, all of them can be encapsulations of values of different types.

Unboxing

filterMap : (String -> Boxed c -> Maybe a) -> Boxed c -> Dict String a

Returns a Dict with only succesful values, resulting from the application of a given function.

boxedDict = Dict.fromList [("one",Integer 1), ("two",Str "b"), ("three",Str "c")] |> Dictionary

filterMap tryString boxedDict == Dict.fromList [("three",Str "b"), ("two",Str "c")]
filterMap tryBool (Dictionary Dict.empty) == Dict.fromList []
filterMap tryBool (Double 3.01) == Dict.fromList []

foldr : (String -> Boxed c -> a -> a) -> a -> Boxed c -> a

foldl : (String -> Boxed c -> a -> a) -> a -> Boxed c -> a

mapGet : (Boxed c -> Maybe a) -> String -> Boxed c -> Maybe a

Get the value associated with a key. Successful only if found, and the given function could be applied to it.

boxedDict = Dictionary (Dict.fromList [("one", Integer 1), ("two", Double 3.03)])

mapGet asBool "two" boxedDict == Nothing
mapGet asInt "one"  boxedDict == Just 1

Return value of the indicated type only if given Boxed is currently holding an encapsulation of said type. Defined mainly as helpers for the Dict's map, filter and partition functions.

tryBool : String -> Boxed c -> Maybe Basics.Bool

tryFloat : String -> Boxed c -> Maybe Basics.Float

Both Integer and Double will be able to pass as Float

tryInt : String -> Boxed c -> Maybe Basics.Int

tryString : String -> Boxed c -> Maybe String

tryValue : String -> Boxed c -> Maybe Json.Encode.Value

Build

empty : Boxed c

Create an empty dictionary.

singleton : String -> Boxed c -> Boxed c

Create a boxed dictionary with one key-value pair.

insert : String -> Boxed c -> Boxed c -> Boxed c

Insert a key-value pair into a Boxed as a dictionary. Succesful even if the Boxed target is not even currently holding a dictionary.

boxedDict = Dictionary (Dict.fromList [("one", Str "b"), ("two", Double 3.03)])

insert "tree" (Integer 1) boxedDict ==  Dictionary (Dict.fromList [("one", Str "b"), ("tree", Integer 1), ("two", Double 3.03)])
insert "tree" (Integer 1) (Str "placeholder")  ==  Dictionary (Dict.fromList [("tree", Integer 1)])

update : String -> (Maybe (Boxed c) -> Maybe (Boxed c)) -> Boxed c -> Boxed c

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

boxedDict = Dictionary (Dict.fromList [("one", Str "b"), ("two", Double 3.03)])

modifier = always (Just (Boolean False))

update "two" modifier boxedDict ==  Dictionary (Dict.fromList [("one", Str "b"), ("two", Boolean False)])

remove : String -> Boxed c -> Boxed c

Remove a key-value pair from a boxed dictionary.

boxedDict = Dictionary (Dict.fromList [("one", Str "b"), ("two", Double 3.03)])

remove "one" boxedDict  ==  Dictionary (Dict.fromList [("two", Double 3.03)])
remove "three" (Integer 1) == Dictionary (Dict.fromList [])

Mapping

apply : (Dict String (Boxed c) -> a) -> Boxed c -> a

Apply a given function to a boxed dictionary.

boxedDict = Dictionary (Dict.fromList [("one", Str "b"), ("two", Double 3.03)])

apply (Dict.get "two") boxedDict == Just (Double 3.03)
apply (Dict.member "one") boxedDict == True
apply Dict.size boxedDict == 2
apply Dict.isEmpty (Str "d") == True
apply (Dict.map tryString) boxedDict == Dict.fromList [("one",Just "b"),("two",Nothing)]