When you do something that can fail in some way, Elm gives you back a type
that has the failure covered so that you can treat it properly.
The two most frequently used types are
- *Result* where you either have Ok someType  OR  Err someFailureType
- *Maybe* where you either have Just someType OR Nothing

In both cases, if you are 100% sure that the operation cannot fail or you
don't want to cover the fail case, you can provide a default value and
unpack the type

Result.withDefault defaultValue someResult

or

Maybe.withDefault defaultValue someMaybe


You can read more in the Error handling section of the guide:
https://guide.elm-lang.org/error_handling/




On Tue, Sep 27, 2016 at 8:40 AM, J. E. Marca <[email protected]>
wrote:

> Hi all,
>
> I have a moderately complicated JSON object.  A sample looks like:
>
> ```
> {
>   "118_192": {
>     "Cloverdale P": {
>         "sum_lane_miles": 0,
>         "sum_single_unit_mt": 0,
>         "sum_vmt": 57,
>         "sum_combination_mt": 0
>       }
>     }
> }
> ```
>
> Thousands of such top level entries, with a variable number of secondary
> entries, and two different types of tertiary entries depending on the
> "type" of the second entry.  I'm debating between a simple nested dict
> approach, and a more complicated nested dict of two alternative object
> types, but I can't get past Go with the repl in order to test out my ideas.
>
> First, the most simple case, I can decode this with nested Dictionaries
>
> What I did in the repl is:
>
> ```
>
> import Json.Decode as Json exposing (..)
> import Dict exposing (..)
>
> inputString = """{  "118_192": {    "Cloverdale P": {
> "sum_lane_miles": 0,      "sum_single_unit_mt": 0,      "sum_vmt": 57,
> "sum_combination_mt": 0    },    "CO P": {      "sum_lane_miles": 0,
> "sum_single_unit_mt": 0,      "sum_vmt": 43,      "sum_combination_mt":
> 0    }      }}"""
>
> gridVolumes = dict ( dict (dict float))
> dataDict = decodeString gridVolumes inputString
> ```
>
> But that last line is a wild guess.  What I want to do is get "something"
> I can use as a dictionary, but Elm is giving me two things...a Result
> object and the dictionary.
>
> ```
> > dataDict
> Ok (Dict.fromList [("118_192",Dict.fromList [("CO P",Dict.fromList
> [("sum_combination_mt",0),("sum_lane_miles",0),("sum_
> single_unit_mt",0),("sum_vmt",43)]),("Cloverdale P",Dict.fromList
> [("sum_combination_mt",0),("sum_lane_miles",0),("sum_
> single_unit_mt",0),("sum_vmt",57)])])])
>     : Result.Result
>         String
>         (Dict.Dict String (Dict.Dict String (Dict.Dict String Float)))
> >
> ```
>
> How do I strip off the "Result" in the repl so I can touch the dictionary
> and play with various types of Dict.get commands?
>
> Obvious to me that I'm just not getting a fundamental concept of the
> language, but I can't find the right point of view so far in the docs I've
> read...for example, the JSON help page (https://guide.elm-lang.org/
> interop/json.html) never actually seems to *assign* the result of running
> a JSON parser to a value, and the JSON library documentation has fragements
> of code, but no complete worked examples one can reproduce in the repl.
>
> Any advice or pointers to docs I should read or reread would be
> appreciated.  Or just a simple "you're doing X wrong do Y instead to get
> access to the Dict String (Dict String (Dict String Float))) thing" would
> be welcome too!
>
> Regards,
> James
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>



-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to