universum 0.2 → 0.2.1
raw patch · 10 files changed
+234/−82 lines, 10 files
Files
- CHANGES.md +14/−0
- src/Applicative.hs +4/−0
- src/Either.hs +0/−30
- src/List.hs +29/−5
- src/Monad.hs +7/−14
- src/Monad/Either.hs +54/−0
- src/Monad/Maybe.hs +45/−0
- src/Monad/Trans.hs +65/−0
- src/Universum.hs +0/−21
- universum.cabal +16/−12
CHANGES.md view
@@ -1,3 +1,17 @@+0.2.1+=====++* [#24](https://github.com/serokell/universum/issues/26):+ Add `whenNothing`, `whenNothing_`, `whenNothingM`, `whenNothingM_`,+ `whenLeft`, `whenLeftM`, `whenRight`, `whenRightM`,+ `whenNotNull`, `whenNotNullM`.+* [#26](https://github.com/serokell/universum/issues/24):+ Add `usingReader`, `usingReaderT`,+ `usingState`, `usingStateT`,+ `executingState`, `executingStateT`,+ `evaluatingState`, `evaluatingStateT`.+* Remove `maybeToEither`.+ 0.2 ===
src/Applicative.hs view
@@ -6,6 +6,7 @@ , orEmpty , eitherA , liftAA2+ , pass , purer , (<<*>>) ) where@@ -15,6 +16,9 @@ import Data.Either (Either (..)) import Data.Function ((.)) import Data.Monoid (Monoid (..))++pass :: Applicative f => f ()+pass = pure () orAlt :: (Alternative f, Monoid a) => f a -> f a orAlt f = f <|> pure mempty
− src/Either.hs
@@ -1,30 +0,0 @@-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE Safe #-}--module Either- ( maybeToLeft- , maybeToRight- , leftToMaybe- , rightToMaybe- , maybeToEither- ) where--import Data.Either (Either (..), either)-import Data.Function (const)-import Data.Maybe (Maybe (..), maybe)-import Data.Monoid (Monoid, mempty)--leftToMaybe :: Either l r -> Maybe l-leftToMaybe = either Just (const Nothing)--rightToMaybe :: Either l r -> Maybe r-rightToMaybe = either (const Nothing) Just--maybeToRight :: l -> Maybe r -> Either l r-maybeToRight l = maybe (Left l) Right--maybeToLeft :: r -> Maybe l -> Either l r-maybeToLeft r = maybe (Right r) Left--maybeToEither :: Monoid b => (a -> b) -> Maybe a -> b-maybeToEither = maybe mempty
src/List.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} module List@@ -6,16 +7,28 @@ , sortOn , unzip , unzip3+#if ( __GLASGOW_HASKELL__ >= 800 )+ , whenNotNull+ , whenNotNullM+#endif , zip , zip3 ) where -import Data.Function ((.))-import Data.Functor (fmap)-import Data.List (sortBy, unzip, unzip3, zip, zip3)-import Data.Ord (Ord, comparing)-import qualified Data.Set as Set+import Control.Applicative (Applicative)+import Control.Monad (Monad (..))+import Data.Function ((.))+import Data.Functor (fmap)+import Data.List (sortBy, unzip, unzip3, zip, zip3)+import Data.Ord (Ord, comparing)+import qualified Data.Set as Set +#if ( __GLASGOW_HASKELL__ >= 800 )+import Data.List.NonEmpty as X (NonEmpty (..))+#endif++import Applicative (pass)+ sortOn :: (Ord o) => (a -> o) -> [a] -> [a] sortOn = sortBy . comparing @@ -33,3 +46,14 @@ list def f xs = case xs of [] -> def _ -> fmap f xs++#if ( __GLASGOW_HASKELL__ >= 800 )+whenNotNull :: Applicative f => [a] -> (NonEmpty a -> f ()) -> f ()+whenNotNull [] _ = pass+whenNotNull (x:xs) f = f (x :| xs)+{-# INLINE whenNotNull #-}++whenNotNullM :: Monad m => m [a] -> (NonEmpty a -> m ()) -> m ()+whenNotNullM ml f = ml >>= \l -> whenNotNull l f+{-# INLINE whenNotNullM #-}+#endif
src/Monad.hs view
@@ -3,7 +3,9 @@ {-# LANGUAGE Trustworthy #-} module Monad- ( Monad ((>>=), return)+ ( module Export++ , Monad ((>>=), return) , MonadFail (fail) , MonadPlus (..) @@ -29,9 +31,6 @@ , when , unless - , whenJust- , whenJustM- , allM , anyM , andM@@ -49,10 +48,12 @@ , (<$!>) ) where +import Monad.Either as Export+import Monad.Maybe as Export+import Monad.Trans as Export+ import Base (IO, seq)-import Control.Applicative (Applicative, pure) import Data.List (concat)-import Data.Maybe (Maybe (..), maybe) import Prelude (Bool (..)) #if __GLASGOW_HASKELL__ >= 710@@ -92,14 +93,6 @@ z `seq` return z {-# INLINE (<$!>) #-} -whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()-whenJust (Just x) f = f x-whenJust Nothing _ = pure ()-{-# INLINE whenJust #-}--whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()-whenJustM mm f = maybe (return ()) f =<< mm-{-# INLINE whenJustM #-} -- Copied from 'monad-loops' by James Cook (the library is in public domain)
+ src/Monad/Either.hs view
@@ -0,0 +1,54 @@+{-# LANGUAGE Safe #-}++-- | Utilites to work with @Either@ data type.++module Monad.Either+ ( module Data.Either+ , maybeToLeft+ , maybeToRight+ , leftToMaybe+ , rightToMaybe+ , whenLeft+ , whenLeftM+ , whenRight+ , whenRightM+ ) where++import Control.Applicative (Applicative)+import Control.Monad (Monad (..))+import Data.Either (Either (..), either, isLeft, isRight, lefts,+ partitionEithers, rights)+import Data.Function (const)+import Data.Maybe (Maybe (..), maybe)++import Applicative (pass)++leftToMaybe :: Either l r -> Maybe l+leftToMaybe = either Just (const Nothing)++rightToMaybe :: Either l r -> Maybe r+rightToMaybe = either (const Nothing) Just++maybeToRight :: l -> Maybe r -> Either l r+maybeToRight l = maybe (Left l) Right++maybeToLeft :: r -> Maybe l -> Either l r+maybeToLeft r = maybe (Right r) Left++whenLeft :: Applicative f => Either l r -> (l -> f ()) -> f ()+whenLeft (Left l) f = f l+whenLeft (Right _) _ = pass+{-# INLINE whenLeft #-}++whenLeftM :: Monad m => m (Either l r) -> (l -> m ()) -> m ()+whenLeftM me f = me >>= \e -> whenLeft e f+{-# INLINE whenLeftM #-}++whenRight :: Applicative f => Either l r -> (r -> f ()) -> f ()+whenRight (Left _) _ = pass+whenRight (Right r) f = f r+{-# INLINE whenRight #-}++whenRightM :: Monad m => m (Either l r) -> (r -> m ()) -> m ()+whenRightM me f = me >>= \e -> whenRight e f+{-# INLINE whenRightM #-}
+ src/Monad/Maybe.hs view
@@ -0,0 +1,45 @@+{-# LANGUAGE Safe #-}++-- | Utility functions to work with 'Data.Maybe' data type as monad.++module Monad.Maybe+ ( whenJust+ , whenJustM+ , whenNothing+ , whenNothing_+ , whenNothingM+ , whenNothingM_+ ) where++import Control.Applicative (Applicative, pure)+import Control.Monad (Monad (..))+import Data.Maybe (Maybe (..))++import Applicative (pass)++whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()+whenJust (Just x) f = f x+whenJust Nothing _ = pass+{-# INLINE whenJust #-}++whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()+whenJustM mm f = mm >>= \m -> whenJust m f+{-# INLINE whenJustM #-}++whenNothing :: Applicative f => Maybe a -> f a -> f a+whenNothing (Just x) _ = pure x+whenNothing Nothing m = m+{-# INLINE whenNothing #-}++whenNothing_ :: Applicative f => Maybe a -> f () -> f ()+whenNothing_ Nothing m = m+whenNothing_ _ _ = pass+{-# INLINE whenNothing_ #-}++whenNothingM :: Monad m => m (Maybe a) -> m a -> m a+whenNothingM mm action = mm >>= \m -> whenNothing m action+{-# INLINE whenNothingM #-}++whenNothingM_ :: Monad m => m (Maybe a) -> m () -> m ()+whenNothingM_ mm action = mm >>= \m -> whenNothing_ m action+{-# INLINE whenNothingM_ #-}
+ src/Monad/Trans.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE Safe #-}++-- | Monad transformers utilities.++module Monad.Trans+ ( module X++ , usingReader+ , usingReaderT++ , evaluatingState+ , evaluatingStateT+ , executingState+ , executingStateT+ , usingState+ , usingStateT+ ) where++-- Monad transformers+import Control.Monad.Catch as X (MonadCatch (catch), MonadMask (..),+ MonadThrow (throwM), bracket, bracket_,+ catchAll, finally)+import Control.Monad.Except as X (ExceptT (..), runExceptT)+import Control.Monad.State as X (MonadState, State, StateT (..), evalState,+ evalStateT, execState, execStateT, gets,+ modify, runState, state, withState)++import Control.Monad.Reader as X (MonadReader, Reader, ReaderT (..), ask, asks,+ local, reader, runReader)++import Control.Monad.Trans as X (MonadIO, lift, liftIO)++import Prelude (Functor, flip, fst, snd, (<$>))++usingReaderT :: r -> ReaderT r m a -> m a+usingReaderT = flip runReaderT+{-# INLINE usingReaderT #-}++usingReader :: r -> Reader r a -> a+usingReader = flip runReader+{-# INLINE usingReader #-}++usingStateT :: s -> StateT s m a -> m (a, s)+usingStateT = flip runStateT+{-# INLINE usingStateT #-}++usingState :: s -> State s a -> (a, s)+usingState = flip runState+{-# INLINE usingState #-}++evaluatingStateT :: Functor f => s -> StateT s f a -> f a+evaluatingStateT s st = fst <$> usingStateT s st+{-# INLINE evaluatingStateT #-}++evaluatingState :: s -> State s a -> a+evaluatingState s st = fst (usingState s st)+{-# INLINE evaluatingState #-}++executingStateT :: Functor f => s -> StateT s f a -> f s+executingStateT s st = snd <$> usingStateT s st+{-# INLINE executingStateT #-}++executingState :: s -> State s a -> s+executingState s st = snd (usingState s st)+{-# INLINE executingState #-}
src/Universum.hs view
@@ -24,7 +24,6 @@ , prettyL , print , foreach- , pass , guarded , guardedA , show@@ -39,7 +38,6 @@ import Containers as X import Conv as X import Debug as X-import Either as X import Exceptions as X import Functor as X import Lifted as X@@ -121,26 +119,10 @@ import Data.Void as X (Void, absurd, vacuous) #endif --- Monad transformers-import Control.Monad.Catch as X (MonadCatch (catch), MonadMask (..),- MonadThrow (throwM), bracket, bracket_,- catchAll, finally)-import Control.Monad.Except as X (ExceptT (..), runExceptT)-import Control.Monad.State as X (MonadState, State, StateT (..), evalState,- evalStateT, execState, execStateT, gets,- modify, runState, state, withState)--import Control.Monad.Reader as X (MonadReader, Reader, ReaderT (..), ask,- asks, local, reader, runReader)--import Control.Monad.Trans as X (MonadIO, lift, liftIO)- -- Base types import Data.Bits as X hiding (unsafeShiftL, unsafeShiftR) import Data.Bool as X hiding (bool) import Data.Char as X (chr)-import Data.Either as X (Either (..), either, isLeft, isRight,- lefts, partitionEithers, rights) import Data.Int as X (Int, Int16, Int32, Int64, Int8) import Data.Maybe as X hiding (fromJust) import Data.Word as X (Word, Word16, Word32, Word64, Word8,@@ -243,9 +225,6 @@ foreach :: Functor f => f a -> (a -> b) -> f b foreach = flip fmap--pass :: Applicative f => f ()-pass = pure () guarded :: (Alternative f) => (a -> Bool) -> a -> f a guarded p x = X.bool empty (pure x) (p x)
universum.cabal view
@@ -1,5 +1,5 @@ name: universum-version: 0.2+version: 0.2.1 synopsis: Custom prelude used in Serokell description: Custom prelude used in Serokell homepage: https://github.com/serokell/universum@@ -27,22 +27,26 @@ library exposed-modules: Universum- Unsafe- Base+ Applicative- Bool- Debug- List- Monad- Show- Conv- Either- Functor+ Base Bifunctor+ Bool Containers+ Conv+ Debug Exceptions- Panic+ Functor Lifted+ List+ Panic+ Show+ Unsafe++ Monad+ Monad.Either+ Monad.Maybe+ Monad.Trans default-extensions: NoImplicitPrelude