Re: [elm-discuss] `Case of` branches grouping

2016-12-09 Thread Max Goldstein
In addition to the underscore pattern (meh - makes it easy to forget about a 
case added later) and refactoring types, I can also recommend extracting a 
function. You still have to have n cases but each of them will be short. 

-- 
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] `Case of` branches grouping

2016-12-09 Thread Nick H
>
> I generally don't like using equality test on union types because that
> makes your code more prone to errors - later on you might decide to add /
> remove some types from union type.


I'm on board with that. I try to avoid catch-all underscores in case
statements for the same reason. But if your case statement is really as
redundant as the ones in your examples, I think these are both options
worth considering (as is refactoring -- as Wouter suggested -- which is
often the best decision).

How is this handled in other languages like OCaml or Haskell?


Haskell does not allow this either. (At least it didn't 5 years ago, when this
proposal  was made). The difference is,
the formatting conventions for Haskell are much more compact. But in Elm,
its perfectly valid to compress your case statement into this:

case someTypeValue of
A -> stuff1
B _ -> stuff2
C _ _ -> stuff2
D _ _ _ -> stuff2

On Fri, Dec 9, 2016 at 8:08 AM, Petr Huřťák  wrote:

> I generally don't like using equality test on union types because that
> makes your code more prone to errors - later on you might decide to add /
> remove some types from union type.
>
> On Friday, December 9, 2016 at 4:57:48 PM UTC+1, Nick H wrote:
>>
>> You can also do equality tests on type values, which means in your first
>> two cases you can use an if statement.
>>
>> if someValue == A then
>>   stuff1
>> else
>>   stuff2
>>
>> This only works if your type values aren't storing any data.
>>
>> On Fri, Dec 9, 2016 at 7:25 AM, Petr Huřťák  wrote:
>>
>>> Hi,
>>>
>>> I would like hear some discussion on grouping of branches in `case of`
>>> statement . Currently it is not possible to use one branch for multiple
>>> conditions.
>>>
>>> Something along these lines:
>>>
>>> case someTypeValue of
>>> A ->
>>> -- code
>>>
>>> B ->
>>> C ->
>>> D ->
>>> -- different code
>>>
>>>
>>> Current alternative is this
>>>
>>> case someTypeValue of
>>> let
>>> stuff2 =
>>> -- code
>>> in
>>> A ->
>>> -- different code
>>>
>>> B ->
>>> stuff2
>>>
>>> C ->
>>> stuff2
>>>
>>> D ->
>>> stuff2
>>>
>>>
>>> Which is unnecessarily verbose and harder to read.
>>>
>>> One question is how this would work when there in cases where matched
>>> patterns have some values attached to them
>>>
>>> case someTypeValue of
>>> A ->
>>> -- stuff1
>>>
>>>
>>> B _ ->
>>> C _ _ ->
>>> D _ _ _ ->
>>> -- stuff2
>>>
>>> How is this handled in other languages like OCaml or Haskell?
>>>
>>> NOTE: moved from https://groups.google.com/forum/#!topic/elm-dev/DtUT2ie
>>> YTDo
>>>
>>> --
>>> 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...@googlegroups.com.
>>> 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 elm-discuss+unsubscr...@googlegroups.com.
> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] `Case of` branches grouping

2016-12-09 Thread Petr Huřťák
I generally don't like using equality test on union types because that 
makes your code more prone to errors - later on you might decide to add / 
remove some types from union type.

On Friday, December 9, 2016 at 4:57:48 PM UTC+1, Nick H wrote:
>
> You can also do equality tests on type values, which means in your first 
> two cases you can use an if statement.
>
> if someValue == A then
>   stuff1
> else
>   stuff2
>
> This only works if your type values aren't storing any data.
>
> On Fri, Dec 9, 2016 at 7:25 AM, Petr Huřťák  > wrote:
>
>> Hi,
>>
>> I would like hear some discussion on grouping of branches in `case of` 
>> statement . Currently it is not possible to use one branch for multiple 
>> conditions.
>>
>> Something along these lines:
>>
>> case someTypeValue of
>> A ->
>> -- code
>>
>> B ->
>> C ->
>> D ->
>> -- different code
>>
>>
>> Current alternative is this
>>
>> case someTypeValue of
>> let
>> stuff2 =
>> -- code  
>> in
>> A ->
>> -- different code
>>
>> B ->
>> stuff2
>>
>> C ->
>> stuff2
>> 
>> D ->
>> stuff2
>>
>>
>> Which is unnecessarily verbose and harder to read.
>>
>> One question is how this would work when there in cases where matched 
>> patterns have some values attached to them
>>
>> case someTypeValue of
>> A ->
>> -- stuff1
>>
>>
>> B _ ->
>> C _ _ ->
>> D _ _ _ ->
>> -- stuff2
>>
>> How is this handled in other languages like OCaml or Haskell?
>>
>> NOTE: moved from 
>> https://groups.google.com/forum/#!topic/elm-dev/DtUT2ieYTDo
>>
>> -- 
>> 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...@googlegroups.com .
>> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] `Case of` branches grouping

2016-12-09 Thread Wouter In t Velt
Op vrijdag 9 december 2016 16:57:48 UTC+1 schreef Nick H:
>
> This only works if your type values aren't storing any data.
>

The equality test can be done if you are *not trying to pattern match* on 
the data in the type.
So

-- will work
if someValue == Just 42 then
  doStuffWith 42


is fine, but you cannot do:

-- this will not work
if someValue == Just a then
  doStuffWith a 

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