Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for maybeCons function #58

Open
YetAnotherMinion opened this issue Jun 11, 2017 · 2 comments
Open

Proposal for maybeCons function #58

YetAnotherMinion opened this issue Jun 11, 2017 · 2 comments

Comments

@YetAnotherMinion
Copy link

YetAnotherMinion commented Jun 11, 2017

I have found it useful when building views to be able to conditionally include an Attribute or child element. It seem natural to use Maybe to represent this conditional nature. However I usually only have one conditional attribute and several unconditional attributes so it feels a little awkward and overkill to wrap many elements in Just and then call filterMap identity. I would prefer to write the list literal and then conditionally add items.

maybeCons : Maybe a -> List a -> List a
maybeCons maybeItem list =
     case maybeItem of
          Just item -> item :: list
          Nothing -> list

For example when a tabbed container, I want to only attach click handlers to the non selected tabs to simplify my update logic.

let                                                                             
    (editOnClick, previewOnClick) =                                             
    case model.editorTab of                                                     
        Edit draft ->                                                           
            ( Nothing                                                           
            , Just <| onWithOptions                                                     
                "click"                                                         
                (Options True True)                                             
                (Json.Decode.succeed <| SetEditorTab <| Preview draft model.user.theme )
            )                                                                   
                                                                                
        Preview draft theme ->                                                  
            ( Just <| onWithOptions                                                     
                "click"                                                         
                (Options True True)                                             
                (Json.Decode.succeed <| SetEditorTab <| Preview draft model.user.theme )
            , Nothing                                                           
            )                                                                   
in                                                                              
    div [ id Editor ]                                                           
        [ div [ class EditorHeader ]                                            
            [ nav [ class HorizontalTabNav ]                                    
                [ button                                                        
                    ( maybeCons                                                 
                        editOnClick                                             
                        [ class HorizontalTab                                   
                        , action "/comments/edit" -- fallback for no JS, yeah yeah yeah XSS we handle it.
                        , method "post"                            
                        ]                                                       
                    )                                                           
                    [ text "Edit" ]                                       
                , button                                                        
                    ( maybeCons                                                 
                        previewOnClick                                          
                        [ class HorizontalTab                                   
                        , action "/comments/preview" -- fallback for no JS
                        , method "post"
                        ]                                     
                    )                                                           
                    [ text "Preview" ]                                             
                ]                                                               
            ]                                                                   
        , viewEditorTab                                                         
        ]

Cons

Haskell does not feel the need to implement this. It is really simple function anyone can write if they really need it.

@Chadtech
Copy link
Collaborator

Hey that looks pretty cool. Ive been in your situation, but I usually had something a bit clunkier, like a function called buttonAttributes : Bool -> List Attribute.

@Chadtech
Copy link
Collaborator

I use maybeCons all the time. Just yesterday that I made a PR over in Maybe.Extra for this function. elm-community/maybe-extra#38

Saying that here for recording keeping in case anyone makes a maybeCons PR on List-Extra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants