module BishBosh.Property.Switchable(
Switchable(..),
flick
) where
import qualified Data.Foldable
class Switchable a where
on :: IO a
toggle :: a -> IO a
switchOff :: a -> IO a
switchOff a
a
| a -> Bool
forall a. Switchable a => a -> Bool
isOn a
a = a -> IO a
forall a. Switchable a => a -> IO a
toggle a
a
| Bool
otherwise = a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
isOn :: a -> Bool
isOff :: a -> Bool
isOff = Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall a. Switchable a => a -> Bool
isOn
flick :: Switchable switchable => Int -> switchable -> IO switchable
flick :: Int -> switchable -> IO switchable
flick Int
n switchable
switchable = ((switchable -> IO switchable) -> switchable -> IO switchable)
-> switchable -> [switchable -> IO switchable] -> IO switchable
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
Data.Foldable.foldrM (switchable -> IO switchable) -> switchable -> IO switchable
forall a b. (a -> b) -> a -> b
($) switchable
switchable ([switchable -> IO switchable] -> IO switchable)
-> [switchable -> IO switchable] -> IO switchable
forall a b. (a -> b) -> a -> b
$ Int
-> (switchable -> IO switchable) -> [switchable -> IO switchable]
forall a. Int -> a -> [a]
replicate Int
n switchable -> IO switchable
forall a. Switchable a => a -> IO a
toggle