In your abstract example, instead of:

    andThen2 : (Var2 -> a) -> Type2 -> Maybe a

you should try to write:

    getVar3 : Type2 -> Maybe Var3


With that approach, the pattern Christian suggested would look like this
for your scribbles example:

    model.study.status
  |> getProgressingSteps
  |> Maybe.map Pivot.getCurrent
  |> Maybe.andThen getScribbles
  |> Maybe.map (Scribbles.update scribblesMsg model)
  |> Maybe.withDefault model


where you simply have to write

    getProgressingSteps : Study.Status -> Maybe (Pivot Study.Step)
    getScribbles : Study.Step -> Maybe Scribbles


On Wed, Mar 29, 2017 at 9:45 PM, Matthieu Pizenberg <
[email protected]> wrote:

> 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.
>>
>
> Yes Christian, I am wondering if people are aware of some more generic
> continuation pattern matching syntax. The andThen expression is very useful
> for monadic types like Maybe. (sorry for the M term use ^^ but it seemed
> appropiate even if I'm not into category theory). But in my exemples, I'm
> using Union types that are not Maybes. I guess what I could do is create my
> own "andThenX" like this:
>
> f1 : Var1 -> Type2
> f2 : Var2 -> Type3
> ...
>
> Type Type2
>     = DataType2 Var2
>     | ...
>
> Type Type3
>     = DataType3 Var3
>     | ...
>
> andThen2 : (Var2 -> a) -> Type2 -> Maybe a
> andThen2 f2 type2 =
>     case type2 of
>         DataType2 var2 ->
>             Just (f2 var2)
>         _ ->
>             Nothing
>
> andThen3 : (Var3 -> a) -> Type3 -> Maybe a
> andThen3 f3 type3 =
>     case type3 of
>         DataType3 var3 ->
>             Just (f3 var3)
>         _ ->
>             Nothing
> ...
>
> And then (no pun intended ^^) do something like:
>
> f1 var1
>     |> andThen2 f2
>     |> andThen (andThen3 f3)
>     |> ...
>     |> andThen (andThenN fN)
>     |> withDefault otherwise
>
> This has the advantage of having a clear flow, but the inconvenient that I
> need to write the my special andThenX functions.
> What do you think of this approach?
>
> --
> 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