X.U.ExtensibleConf: Flip arguments of once(M)

This appears to be more natural. The function will most often be fixed
by the module using `XC.once` and the configuration will often be
supplied by users of those modules, so it's better to partially apply
the function first.
This commit is contained in:
Tomas Janousek 2021-06-03 01:01:12 +01:00
parent 4a6f21604f
commit eab9a3a58e
3 changed files with 11 additions and 13 deletions

View File

@ -90,7 +90,9 @@ instance Monoid RescreenConfig where
-- Note that 'rescreenHook' is safe to use several times, 'rescreen' is still
-- done just once and hooks are invoked in sequence, also just once.
rescreenHook :: RescreenConfig -> XConfig a -> XConfig a
rescreenHook = flip XC.once rescreenHook'
rescreenHook = XC.once $ \c -> c
{ startupHook = startupHook c <> rescreenStartupHook
, handleEventHook = handleEventHook c <> rescreenEventHook }
-- | Shortcut for 'rescreenHook'.
addAfterRescreenHook :: X () -> XConfig a -> XConfig a
@ -100,10 +102,6 @@ addAfterRescreenHook h = rescreenHook def{ afterRescreenHook = h }
addRandrChangeHook :: X () -> XConfig a -> XConfig a
addRandrChangeHook h = rescreenHook def{ randrChangeHook = h }
rescreenHook' :: XConfig a -> XConfig a
rescreenHook' c = c{ startupHook = startupHook c <> rescreenStartupHook
, handleEventHook = handleEventHook c <> rescreenEventHook }
-- | Startup hook to listen for @RRScreenChangeNotify@ events.
rescreenStartupHook :: X ()
rescreenStartupHook = do

View File

@ -113,16 +113,16 @@ add x = alter (<> Just x)
-- This can be used to implement a composable interface for modules that must
-- only hook into xmonad core once.
once :: forall a l. (Semigroup a, Typeable a)
=> a -- ^ configuration to add
-> (XConfig l -> XConfig l) -- ^ 'XConfig' modification done only once
=> (XConfig l -> XConfig l) -- ^ 'XConfig' modification done only once
-> a -- ^ configuration to add
-> XConfig l -> XConfig l
once x f c = add x $ maybe f (const id) (lookup @a c) c
once f x c = add x $ maybe f (const id) (lookup @a c) c
-- | Config-time: Applicative (monadic) variant of 'once', useful if the
-- 'XConfig' modification needs to do some 'IO' (e.g. create an
-- 'Data.IORef.IORef').
onceM :: forall a l m. (Applicative m, Semigroup a, Typeable a)
=> a -- ^ configuration to add
-> (XConfig l -> m (XConfig l)) -- ^ 'XConfig' modification done only once
=> (XConfig l -> m (XConfig l)) -- ^ 'XConfig' modification done only once
-> a -- ^ configuration to add
-> XConfig l -> m (XConfig l)
onceM x f c = add x <$> maybe f (const pure) (lookup @a c) c
onceM f x c = add x <$> maybe f (const pure) (lookup @a c) c

View File

@ -22,9 +22,9 @@ spec = do
XC.lookup (XC.add "a" (XC.add [1 :: Int] def)) `shouldBe` (Nothing :: Maybe ())
specify "once" $
borderWidth (XC.once "a" incBorderWidth def) `shouldBe` succ (borderWidth def)
borderWidth (XC.once incBorderWidth "a" def) `shouldBe` succ (borderWidth def)
specify "once . once" $
borderWidth (XC.once "b" incBorderWidth (XC.once "a" incBorderWidth def))
borderWidth (XC.once incBorderWidth "b" (XC.once incBorderWidth "a" def))
`shouldBe` succ (borderWidth def)
incBorderWidth :: XConfig l -> XConfig l