Very nice but perhaps better named "orElse".

Mark

> On Aug 21, 2016, at 5:49 AM, [email protected] wrote:
> 
> I'm working on a program that attempts to parse a String in several different 
> ways, and uses the first one that matches. Each parse function returns a 
> Result. At first I had nested case statements:
> 
> let result =
>   case parse1 input of
>     Err _ ->
>       case parse2 input of
>         Err _ ->
>           parse3 input
>         res ->
>           res
>     res ->
>       res
> 
> (additional parse functions will be added later, deepening this)
> 
> Then I wrote an orThen function that's like the complement of andThen to 
> simplify it:
> 
> orThen : (a -> Result x b) -> a -> Result x b -> Result x b
> orThen f input res =
>   case res of
>     Ok _ -> res
>     _ -> f input
> 
> let result =
>   parse1 input
>   |> orThen parse2 input
>   |> orThen parse3 input
> 
> I put an example and a couple alternative variations (though I like this one 
> best) on Elm Fiddle:
> http://www.elmfiddle.io/view/cis4ls2gm0003rhtvo7mh49m5
> 
> An analogous function could be defined for Maybe. Maybe's oneOf could be used 
> for this purpose too, though that requires computing all Maybe values first 
> instead of being lazy.
> 
> Perhaps orThen would be a useful addition to Result.Extra / Maybe.Extra?
> 
> -- 
> 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