Re: your initial post... you could rewrite the code as

case Maybe.map .cell c of
  Just (Letter s) ->
    s
  _ ->
    ""

Don't know if that's any easier to read, though.

Regarding null_square: If you want to make sure that null_square is passed
by reference, you can define it as a top-level value. (That said, the
record is so small, I doubt it would make a noticeable performance
difference, unless you're planning on making a gazillion of them.)


On Sat, Sep 24, 2016 at 1:44 PM, Martin DeMello <[email protected]>
wrote:

> Don't think that will work because c is of type Maybe Square, not Maybe
> Cell.
>
> One further question - if I define
>
> let null_square = { cell = Empty, num = 0 }
>
> let get_square maybe_square =
>   case maybe_cell of
>     Nothing: null_square
>     Just s : s
>
> let get_letter maybe_square =
>   case maybe_square of
>     { Letter s } -> s
>     _ -> ""
>
> would that be introducing an inefficient creation of a null_square every
> time I call get_letter or is it just a cheap reference?
>
> martin
>
> On Fri, Sep 23, 2016 at 12:04 PM, Simon <[email protected]> wrote:
>
>> This should work
>>
>>  case c of
>>     Just (Letter s) ->
>>       s
>>     _ -> ""
>>
>>
>>
>> On Friday, 23 September 2016 03:50:19 UTC+2, Martin DeMello wrote:
>>>
>>> If I have the following code:
>>>
>>> type Cell = Empty | Letter String
>>>
>>> type alias Square =
>>>   { cell : Cell
>>>   , num : Int
>>>   }
>>>
>>> and c : Maybe Square,
>>>
>>> can I do the following in a single match?
>>>
>>>   case c of
>>>     Just { cell } ->
>>>       case cell of
>>>         Letter s -> s
>>>         _ -> ""
>>>     _ -> ""
>>>
>>> that is, i conceptually want
>>>
>>>   case c of
>>>     Just { cell = Letter s } -> s
>>>     _ -> ""
>>>
>>> martin
>>>
>> --
>> 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.
>

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