[xmonad] Haskell question: avoiding code duplication

Peter Jones mlists at pmade.com
Sat Feb 2 19:08:46 CET 2013


I've been working on a few customizations to xmonad but as I play with
Haskell I keep seeing a pattern of duplication in my code that I don't
know how to resolve.  Here's an example:

-- | Enables 'focusFollowsMouse' for tiled windows only.  For this to
-- work you need to turn off 'focusFollowsMouse' in your configuration
-- and then add this function to your 'handleEventHook'.
focusFollowsTiledOnly :: Event -> X All
focusFollowsTiledOnly e@(CrossingEvent {ev_window = w, ev_event_type = t})
  | isNormalEnter = whenX bothTiled (focus w) >> continueHooks
  where isNormalEnter   = t == enterNotify && ev_mode e == notifyNormal
        bothTiled       = (&&) <$> notFloating w <*> currentIsTiled
        currentIsTiled  = currentWindow >>= maybe (return True) notFloating
        currentWindow   = gets $ W.peek . windowset
        notFloating w'  = gets $ not . M.member w' . W.floating . windowset
        continueHooks   = return . mempty $ True
focusFollowsTiledOnly _ = return . mempty $ True


The last two lines demonstrate the pattern I've been seeing.  The only
way I know how to remove this duplication is to move it out into a
top-level function.  Is that correct?

Thanks!

-- 
Peter Jones --- Love to Develop
303-219-0226 http://devalot.com



More information about the xmonad mailing list