On Thursday, February 23, 2017 at 8:57:32 PM UTC, Brian Marick wrote:
>
> The first animal is in the Compact state, the second in Expanded, and the 
> last in Editable. Would you be suggesting a `Mode` like the following?
>
> type Mode
>   = Compact Animal
>   | Expanded Animal
>   | Editable Animal FormData
>

Yes, that is the pattern I am working with at the moment.
 

> I had problems with that. I too often had view code that had to know both 
> that it was given `FormData` and also had to take the whole `Mode` as an 
> argument - or had to assemble a Mode from the pieces they were given. 
> (Unfortunately the details are fuzzy in my memory.)
>

I forgot to include my help functions in the Gist. Adding them now. 
https://gist.github.com/rupertlssmith/c948f304ce0a99cd7ba9d6b6a061251a

I also have situation where I need to write a function the uses >1 of the 
little products that make up a state. I wrote a mapWhenCompose function for 
that. I can also tell you that my first 7 attempt to write this function 
each occupied about 20 lines of code and none of them compiled! Then almost 
by chance, or was it a good nights sleep, the answer came to my no longer 
young and brilliant mind:
 
mapWhenCompose : (c -> d -> Maybe a) -> (a -> d -> Maybe b) -> c -> d -> 
Maybe b
mapWhenCompose mapWhen1 mapWhen2 func state =
    mapWhen1 func state
        |> andThen ((flip mapWhen2) state)

so if you follow my pattern and write the mapWhenWithAnimal and 
mapWhenWithFormData functions, then you can do:

editableView animal formData = ...

and then call it with:

mapWhenCompose mapWhenWithAnimal mapWhenWithFormData editableView mode

Note also that mapWhenCompose also returns a mapWhen function so can be 
further composed.

====

What I really like about this pattern is that I have eliminated all Maybes 
from my model. But now I am using Maybes to linearize the logic and to be 
able to compose pieces of logic together, using Nothing to indicate a 
failure of a logic flow to complete. This feels like a much better way to 
use Maybes.

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

Reply via email to