hask-irc/Network/IRC/Util.hs

45 lines
1.0 KiB
Haskell
Raw Normal View History

2014-05-25 14:51:33 +05:30
{-# LANGUAGE FlexibleContexts #-}
module Network.IRC.Util where
import ClassyPrelude
2014-05-25 14:51:33 +05:30
import Control.Arrow (Arrow)
import Control.Concurrent.Lifted (Chan)
2014-05-25 14:51:33 +05:30
import Control.Monad.Base (MonadBase)
2014-05-23 02:45:45 +05:30
import Data.Text (strip)
oneSec :: Int
oneSec = 1000000
type Latch = MVar ()
latchIt :: Latch -> IO ()
latchIt latch = putMVar latch ()
awaitLatch :: Latch -> IO ()
awaitLatch latch = void $ takeMVar latch
type Channel a = (Chan a, Latch)
2014-05-22 20:59:02 +05:30
mapKeys :: IsMap map => map -> [ContainerKey map]
mapKeys = map fst . mapToList
mapValues :: IsMap map => map -> [MapValue map]
mapValues = map snd . mapToList
2014-05-23 02:45:45 +05:30
whenJust :: Monad m => Maybe t -> (t -> m ()) -> m ()
whenJust m f = maybe (return ()) f m
clean :: Text -> Text
clean = toLower . strip
2014-05-23 12:21:38 +05:30
io :: MonadIO m => IO a -> m a
io = liftIO
2014-05-25 14:51:33 +05:30
both :: Arrow cat => cat b d -> cat (b, b) (d, d)
both f = first f . second f
atomicModIORef :: MonadBase IO f => IORef t -> (t -> t) -> f ()
atomicModIORef ref f = void . atomicModifyIORef' ref $ \v -> (f v, v)