{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
module RIO.Prelude.Extra
( mapLeft
, fromFirst
, mapMaybeA
, mapMaybeM
, forMaybeA
, forMaybeM
, foldMapM
, nubOrd
, whenM
, unlessM
, (<&>)
, asIO
) where
import Prelude
import qualified Data.Set as Set
import Data.Monoid (First (..))
import Data.Foldable (foldlM)
import Data.Functor
import Data.Maybe
import Control.Monad
mapLeft :: (a1 -> a2) -> Either a1 b -> Either a2 b
mapLeft f (Left a1) = Left (f a1)
mapLeft _ (Right b) = Right b
fromFirst :: a -> First a -> a
fromFirst x = fromMaybe x . getFirst
mapMaybeA :: Applicative f => (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeA f = fmap catMaybes . traverse f
forMaybeA :: Applicative f => [a] -> (a -> f (Maybe b)) -> f [b]
forMaybeA = flip mapMaybeA
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM f = liftM catMaybes . mapM f
forMaybeM :: Monad m => [a] -> (a -> m (Maybe b)) -> m [b]
forMaybeM = flip mapMaybeM
foldMapM
:: (Monad m, Monoid w, Foldable t)
=> (a -> m w)
-> t a
-> m w
foldMapM f = foldlM
(\acc a -> do
w <- f a
return $! mappend acc w)
mempty
nubOrd :: Ord a => [a] -> [a]
nubOrd =
loop mempty
where
loop _ [] = []
loop !s (a:as)
| a `Set.member` s = loop s as
| otherwise = a : loop (Set.insert a s) as
whenM :: Monad m => m Bool -> m () -> m ()
whenM boolM action = boolM >>= (`when` action)
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM boolM action = boolM >>= (`unless` action)
#if !MIN_VERSION_base(4, 11, 0)
(<&>) :: Functor f => f a -> (a -> b) -> f b
as <&> f = f <$> as
infixl 1 <&>
#endif
asIO :: IO a -> IO a
asIO = id