I wrote a simple program, that will make a string 'noisy' if the clipboard contains one.
What disappoints me, is that I should manually check what I got from
getClipboardString
— in the Nothing
case, we simply return from program, otherwise we modify the string. Is there a better way to do this kind of check?
import Data.Char (toUpper)
import System.Random (randomIO)
import System.Clipboard (setClipboardString, getClipboardString)
import Control.Monad (join)
main :: IO ()
main = do
join $ fmap (test doNoise) getClipboardString
where
test :: (String -> IO ()) -> (Maybe String) -> IO ()
test _ Nothing = return ()
test f (Just s) = f s
doNoise :: String -> IO ()
doNoise s = do
capsed <- (sequence $ map randCap s)
setClipboardString capsed
randCap :: Char -> IO Char
randCap x = fmap ($ x) $ fmap choice (randomIO :: IO Bool)
choice :: Bool -> (Char -> Char)
choice x = if x then toUpper else id