On Thursday, January 26, 2017 at 3:45:24 AM UTC, Robert Lee wrote:
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...