On Thursday, January 26, 2017 at 3:45:24 AM UTC, Robert Lee wrote:
>
> The following fits in good enough for me.
>
> filterDiv : List (Maybe (H.Attribute msg)) -> List (Maybe (H.Html msg)) ->
> H.Html msg
> filterDiv a n =
> H.div (List.filterMap identity a) (List.filterMap identity n)
>
Yes, or even:
filterNode :
(List (H.Attribute msg) -> List (H.Html msg) -> H.Html msg)
-> List (Maybe (H.Attribute msg))
-> List (Maybe (H.Html msg))
-> H.Html msg
filterNode node a n =
node (List.filterMap identity a) (List.filterMap identity n)
To make it work over all Html nodes not just divs. Then filterDiv =
filterNode div, and so on.
I went for a slightly different approach which is to define:
when : Bool -> a -> Maybe a
when condition value =
if (condition) then
Just value
else
Nothing
required : a -> Maybe a
required value =
Just value
optional : List (Maybe a) -> List a
optional =
Maybe.Extra.values
Then I can make just the attributes or the child nodes optional. For
example:
div []
(optional
[ contentView model content |> required
, slideButton model |> required
, sideNav model |> required
, clickPlane |> when model.menuOpen
]
)
I could use Just directly instead of 'required' but it has a certain visual
appeal in that I can scan down the list and see what is always there and
what is toggled by a condition.
P.S. I like your "List.filterMap identity", I was using Maybe.Extra.values
for the same thing. At first I was scratching my head, why does the
identity function make a filter over a list? Buit then I see the type of
filterMap and how it works. I can drop Maybe.Extra now...
--
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.