[elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-31 Thread Ossi Hanhinen
One thing that helps in some of these cases, and not many people seem to 
know, is that you can match "several levels" at once, like so:

type Top
= Some
| Other (Maybe Int)

-- and then match with

case value of
Other (Just n) ->
doStuffwith n

Other Nothing ->
doWithoutNumber

Some ->
doSomething



On Wednesday, March 29, 2017 at 7:40:40 PM UTC+3, 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-30 Thread Matthieu Pizenberg

>
> Instead of calling it 'getVar3', I am using names like 'whenWithVar3'. The 
> 'when' part better reflects that the operation will not always produce a 
> result. 'whenWith' is a bit long, perhaps 'whenVar3' might be better?
>

This sounds well also. I've no idea though about conventions that may 
exist. 
 

> But perhaps the 'get' and 'set' conventions are already better established 
> from lenses or monocles?
>

 You got me curious by mentionning lenses and monocles. I looked around for 
existing packages explaining those concepts and found a few interesting. 
Maybe the more versatile, that could be helpful with my nested pattern 
matching issue is elm-monocle 
(http://package.elm-lang.org/packages/arturopala/elm-monocle/latest) from 
arturopala. In particular, the Optional type seems very interesting. I will 
keep it in mind for next time. There is no rush.

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-30 Thread 'Rupert Smith' via Elm Discuss
On Thursday, March 30, 2017 at 8:31:29 AM UTC+1, Aaron VonderHaar wrote:
>
> In your abstract example, instead of:
>
> andThen2 : (Var2 -> a) -> Type2 -> Maybe a
>
> you should try to write:
>
> getVar3 : Type2 -> Maybe Var3
>

A most informative discussion, thanks for this. I have kind of figured much 
of this out for myself but not quite so completely or neatly as you have 
shown here.

Instead of calling it 'getVar3', I am using names like 'whenWithVar3'. The 
'when' part better reflects that the operation will not always produce a 
result. 'whenWith' is a bit long, perhaps 'whenVar3' might be better?

model.study.status
|> whenProgressingSteps
|> Maybe.map Pivot.whenCurrent
|> Maybe.andThen whenScribbles
|> Maybe.map (Scribbles.update scribblesMsg model)
|> Maybe.withDefault model

But perhaps the 'get' and 'set' conventions are already better established 
from lenses or monocles?

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-30 Thread Matthieu Pizenberg

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

 Thank you Aaron, that makes it definitely more readable! Those getters are 
the key (`Maybe` I should go back to code in Java ^^)

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-29 Thread Matthieu Pizenberg

>
> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Better syntax for nested pattern matching (destructuring)?

2017-03-29 Thread Christian Charukiewicz
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 
 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.