diff --git a/XMonad/Actions/TagWindows.hs b/XMonad/Actions/TagWindows.hs index 3be10a04..0e11b9a1 100644 --- a/XMonad/Actions/TagWindows.hs +++ b/XMonad/Actions/TagWindows.hs @@ -134,11 +134,6 @@ focusTagged' :: (WindowSet -> [Window]) -> String -> X () focusTagged' wl t = gets windowset >>= findM (hasTag t) . wl >>= maybe (return ()) (windows . focusWindow) -findM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a) -findM _ [] = return Nothing -findM p (x:xs) = do b <- p x - if b then return (Just x) else findM p xs - -- | apply a pure function to windows with a tag withTaggedP, withTaggedGlobalP :: String -> (Window -> WindowSet -> WindowSet) -> X () withTaggedP t f = withTagged' t (winMap f) diff --git a/XMonad/Layout/IM.hs b/XMonad/Layout/IM.hs index 7a907538..aa56ad8e 100644 --- a/XMonad/Layout/IM.hs +++ b/XMonad/Layout/IM.hs @@ -30,10 +30,11 @@ module XMonad.Layout.IM ( ) where import XMonad -import qualified XMonad.StackSet as S import XMonad.Layout.Grid import XMonad.Layout.LayoutModifier +import XMonad.Prelude import XMonad.Util.WindowProperties +import qualified XMonad.StackSet as S import Control.Arrow (first) @@ -110,11 +111,6 @@ applyIM ratio prop wksp rect = do return (first ((w, masterRect) :) wrs) Nothing -> runLayout wksp rect --- | Like find, but works with monadic computation instead of pure function. -findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a) -findM _ [] = return Nothing -findM f (x:xs) = do { b <- f x; if b then return (Just x) else findM f xs } - -- | This is for compatibility with old configs only and will be removed in future versions! data IM a = IM Rational Property deriving (Read, Show) instance LayoutClass IM Window where diff --git a/XMonad/Prelude.hs b/XMonad/Prelude.hs index 721038f4..282c4581 100644 --- a/XMonad/Prelude.hs +++ b/XMonad/Prelude.hs @@ -24,6 +24,7 @@ module XMonad.Prelude ( notEmpty, safeGetWindowAttributes, mkAbsolutePath, + findM, -- * Keys keyToString, @@ -89,6 +90,21 @@ chunksOf i xs = chunk : chunksOf i rest (.:) :: (a -> b) -> (c -> d -> a) -> c -> d -> b (.:) = (.) . (.) +-- | Like 'find', but takes a monadic function instead; retains the +-- short-circuiting behaviour of the non-monadic version. +-- +-- For example, +-- +-- > findM (\a -> putStr (show a <> " ") >> pure False) [1..10] +-- +-- would print "1 2 3 4 5 6 7 8 9 10" and return @Nothing@, while +-- +-- > findM (\a -> putStr (show a <> " ") >> pure True) [1..10] +-- +-- would print @"1"@ and return @Just 1@. +findM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a) +findM p = foldr (\x -> ifM (p x) (pure $ Just x)) (pure Nothing) + -- | 'Data.List.NonEmpty.fromList' with a better error message. Useful to -- silence GHC's Pattern match(es) are non-exhaustive warning in places where -- the programmer knows it's always non-empty, but it's infeasible to express