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

Reply via email to