We have found that this pattern is typically a good case for chaining a 
bunch of andThen statements with a withDefault statement at the end 
together.  The one thing is you would have to be able to adjust your values 
to all be Maybes.

For example, if you have the following:

case someMaybeVal of
    Just someVal ->
        case someVal.record of
            Just someInnerVal ->
                Just finalVal

            Nothing ->
                someDefaultVal
    Nothing ->
        someDefaultVal

You can replace it with:

someMaybeVal
    |> andThen (\someVal -> Just someVal.record)
    |> andThen (\someInnerVal -> Just finalVal)
    |> withDefault someDefaultVal

Looking at the documentation for andThen 
<http://package.elm-lang.org/packages/elm-lang/core/latest/Maybe#andThen> 
may help.


On Wednesday, March 29, 2017 at 11:40:40 AM UTC-5, Matthieu Pizenberg wrote:
>
> Multiple times in my projects, I happen to need nested pattern matching.
> Below is a raw extract of code (in an update) showing this need.
>
> Study.ScribblesMsg scribblesMsg ->
>     case model.study.status of
>         Study.Progressing steps ->
>             case Pivot.getC steps of
>                 Study.ScribblesStep ( imageUrl, scribbles ) ->
>                     ( Scribbles.update scribblesMsg model scribbles
>                     , Cmd.none
>                     )
>
>                 _ ->
>                     ( model, Cmd.none )
>
>         _ ->
>             ( model, Cmd.none )
>
> Basically, this is a specific case of a generic following code:
>
> case f1 var1 of
>     DataType2 var2 ->
>         ...
>             case fn varn of
>                 finalVar ->
>                     doSomethingOf finalVar
>                 _ ->
>                     otherwise
>         ...
>      _ ->
>         otherwise
>
> Do you think there could exist a syntax enabling better handling of such a 
> case?
> Have you encountered one in another langage maybe?
>

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