This function is called unpack
<http://package.elm-lang.org/packages/elm-community/result-extra/2.0.1/Result-Extra#unpack>
in elm-community/result-extra

On Sat, Nov 19, 2016 at 12:58 PM, Charlie Koster <[email protected]>
wrote:

> Just this morning I upgraded a 3500 line Elm 0.17 project to Elm 0.18. I
> wrote down my experience that I will share at a later date but I wanted to
> share a suggestion.
>
> My app makes a handful of Http requests. For example I have a user model
> and Msgs for when the user logs in or out.
>
> type alias Model =
>     { username : String
>     , fullName : String
>     , password : String
>     , email : String
>     }
>
> type Msg =
>     | Login
>     | LoginSucceed Model
>     | LoginFail Http.Error
>     | Logout
>     | LogoutSucceed
>     | LogoutFail Http.Error
>
> A typical 0.18 Http requests looks like this
>
> performLogin : Model -> Cmd Msg
> performLogin user =
>     let
>         request =
>             Http.request
>                 { method = "POST"
>                 , headers = []
>                 , url = "http://mydomain/login";
>                 , body = Http.jsonBody (userEncoder user)
>                 , expect = Http.expectJson userDecoder
>                 , timeout = Nothing
>                 , withCredentials = False
>                 }
>     in
>         Http.send
>             (\result ->
>                 case result of
>                     Ok user ->
>                         LoginSucceed user
>
>                     Err err ->
>                         LoginFail err
>             )
>             request
>
> The `case result of` part in the Http.send argument is a lot of
> boilerplate that I don't like seeing copy/pasted in all of my Http Cmds.
>
> Additionally, in most cases my success Msg takes a payload which is the
> model that was decoded from the response and my fail Msg takes the Result's
> Http.Error.
>
> I have since created a helper function that looks like this
>
> resultToMsg : (x -> b) -> (a -> b) -> Result x a -> b
> resultToMsg errMsg okMsg result =
>     case result of
>         Ok a ->
>             okMsg a
>
>         Err err ->
>             errMsg err
>
>
> With that helper function my Http Cmds now look like this
>
> performLogin : Model -> Cmd Msg
> performLogin user =
>     let
>         request =
>             Http.request
>                 { method = "POST"
>                 , headers = []
>                 , url = "http://mydomain/login";
>                 , body = Http.jsonBody (userEncoder user)
>                 , expect = Http.expectJson userDecoder
>                 , timeout = Nothing
>                 , withCredentials = False
>                 }
>     in
>         Http.send
>             (resultToMsg LoginFail LoginSucceed)
>             request
>
> Which results in 4 fewer lines of boilerplate (5 including the whitespace).
>
> I'm terrible at naming things but I propose adding a helper function to
> elm-lang/core Result with basically the implementation above but with a
> more appropriate name than resultToMsg.
>
> --
> 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.
>

-- 
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