packages feed

list-t 0.4.7 → 1

raw patch · 3 files changed

+48/−114 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- ListT: class MonadPlus m => MonadCons m
- ListT: class MonadTrans t => MonadTransUncons t
- ListT: data ListT m a
- ListT: data Positive n
- ListT: instance Data.Data.Data n => Data.Data.Data (ListT.Positive n)
- ListT: instance GHC.Base.Monad m => ListT.MonadCons (ListT.ListT m)
- ListT: instance GHC.Classes.Eq n => GHC.Classes.Eq (ListT.Positive n)
- ListT: instance GHC.Classes.Ord n => GHC.Classes.Ord (ListT.Positive n)
- ListT: instance GHC.Generics.Constructor ListT.C1_0Positive
- ListT: instance GHC.Generics.Datatype ListT.D1Positive
- ListT: instance GHC.Generics.Generic (ListT.Positive n)
- ListT: instance GHC.Read.Read n => GHC.Read.Read (ListT.Positive n)
- ListT: instance GHC.Show.Show n => GHC.Show.Show (ListT.Positive n)
- ListT: instance ListT.MonadCons []
- ListT: instance ListT.MonadCons m => ListT.MonadCons (Control.Monad.Trans.Reader.ReaderT e m)
- ListT: instance ListT.MonadTransUncons ListT.ListT
- ListT: positive :: (Ord n, Num n) => n -> Maybe (Positive n)
- ListT: type Transformation m a b = forall t. (Monad m, MonadCons (t m), MonadTransUncons t) => t m a -> t m b
+ ListT: ListT :: (m (Maybe (a, ListT m a))) -> ListT m a
+ ListT: newtype ListT m a
- ListT: cons :: MonadCons m => a -> m a -> m a
+ ListT: cons :: Monad m => a -> ListT m a -> ListT m a
- ListT: drop :: Int -> Transformation m a a
+ ListT: drop :: Monad m => Int -> ListT m a -> ListT m a
- ListT: fold :: (Monad m, MonadTransUncons t) => (r -> a -> m r) -> r -> t m a -> m r
+ ListT: fold :: Monad m => (r -> a -> m r) -> r -> ListT m a -> m r
- ListT: foldMaybe :: (Monad m, MonadTransUncons t) => (r -> a -> m (Maybe r)) -> r -> t m a -> m r
+ ListT: foldMaybe :: Monad m => (r -> a -> m (Maybe r)) -> r -> ListT m a -> m r
- ListT: fromFoldable :: (MonadCons m, Foldable f) => f a -> m a
+ ListT: fromFoldable :: (Monad m, Foldable f) => f a -> ListT m a
- ListT: fromMVar :: (MonadCons m, MonadIO m) => MVar (Maybe a) -> m a
+ ListT: fromMVar :: (MonadIO m) => MVar (Maybe a) -> ListT m a
- ListT: head :: (Monad m, MonadTransUncons t) => t m a -> m (Maybe a)
+ ListT: head :: Monad m => ListT m a -> m (Maybe a)
- ListT: null :: (Monad m, MonadTransUncons t) => t m a -> m Bool
+ ListT: null :: Monad m => ListT m a -> m Bool
- ListT: repeat :: (MonadCons m) => a -> m a
+ ListT: repeat :: Monad m => a -> ListT m a
- ListT: slice :: Positive Int -> Transformation m a [a]
+ ListT: slice :: Monad m => Int -> ListT m a -> ListT m [a]
- ListT: splitAt :: (Monad m, MonadTransUncons t, MonadPlus (t m)) => Int -> t m a -> m ([a], t m a)
+ ListT: splitAt :: Monad m => Int -> ListT m a -> m ([a], ListT m a)
- ListT: tail :: (Monad m, MonadTransUncons t) => t m a -> m (Maybe (t m a))
+ ListT: tail :: Monad m => ListT m a -> m (Maybe (ListT m a))
- ListT: take :: Int -> Transformation m a a
+ ListT: take :: Monad m => Int -> ListT m a -> ListT m a
- ListT: toList :: (Monad m, MonadTransUncons t) => t m a -> m [a]
+ ListT: toList :: Monad m => ListT m a -> m [a]
- ListT: toReverseList :: (Monad m, MonadTransUncons t) => t m a -> m [a]
+ ListT: toReverseList :: Monad m => ListT m a -> m [a]
- ListT: traverse :: (a -> m b) -> Transformation m a b
+ ListT: traverse :: Monad m => (a -> m b) -> ListT m a -> ListT m b
- ListT: traverse_ :: (Monad m, MonadTransUncons t) => (a -> m ()) -> t m a -> m ()
+ ListT: traverse_ :: Monad m => (a -> m ()) -> ListT m a -> m ()
- ListT: uncons :: (MonadTransUncons t, Monad m) => t m a -> m (Maybe (a, t m a))
+ ListT: uncons :: ListT m a -> m (Maybe (a, ListT m a))
- ListT: unfold :: (MonadCons m) => (b -> Maybe (a, b)) -> b -> m a
+ ListT: unfold :: Monad m => (b -> Maybe (a, b)) -> b -> ListT m a
- ListT: unfoldM :: (Monad m) => (b -> m (Maybe (a, b))) -> b -> ListT m a
+ ListT: unfoldM :: Monad m => (b -> m (Maybe (a, b))) -> b -> ListT m a

Files

library/ListT.hs view
@@ -1,11 +1,9 @@-{-# LANGUAGE UndecidableInstances, CPP #-}+{-# LANGUAGE UndecidableInstances #-} module ListT (-  ListT,-  -- * Classes-  MonadTransUncons(..),-  MonadCons(..),+  ListT(..),   -- * Execution utilities+  uncons,   head,   tail,   null,@@ -16,6 +14,7 @@   traverse_,   splitAt,   -- * Construction utilities+  cons,   fromFoldable,   fromMVar,   unfold,@@ -25,16 +24,12 @@   -- |    -- These utilities only accumulate the transformations   -- without actually traversing the stream.-  -- They only get applied with a single traversal, -  -- which happens at the execution.-  Transformation,+  -- They only get applied in a single traversal, +  -- which only happens at the execution.   traverse,   take,   drop,   slice,-  -- * Positive numbers-  Positive,-  positive, ) where @@ -57,7 +52,7 @@ -- <http://hackage.haskell.org/package/monadplus "monadplus"> -- with it. newtype ListT m a =-  ListT { unListT :: m (Maybe (a, ListT m a)) }+  ListT (m (Maybe (a, ListT m a)))  instance Monad m => Monoid (ListT m a) where   mempty =@@ -74,7 +69,7 @@  instance Functor m => Functor (ListT m) where   fmap f =-    ListT . (fmap . fmap) (f *** fmap f) . unListT+    ListT . (fmap . fmap) (f *** fmap f) . uncons  instance (Monad m, Functor m) => Applicative (ListT m) where   pure = @@ -116,7 +111,7 @@  instance MFunctor ListT where   hoist f =-    ListT . f . (liftM . fmap) (id *** hoist f) . unListT+    ListT . f . (liftM . fmap) (id *** hoist f) . uncons  instance MMonad ListT where   embed f (ListT m) =@@ -128,7 +123,6 @@   liftBase =     lift . liftBase -#if MIN_VERSION_monad_control(1,0,0) instance MonadBaseControl b m => MonadBaseControl b (ListT m) where   type StM (ListT m) a =     StM m (Maybe (a, ListT m a))@@ -139,93 +133,55 @@     lift (restoreM inner) >>= \case       Nothing -> mzero       Just (h, t) -> cons h t-#else-instance MonadBaseControl b m => MonadBaseControl b (ListT m) where-  newtype StM (ListT m) a =-    StM (StM m (Maybe (a, ListT m a)))-  liftBaseWith runToBase =-    lift $ liftBaseWith $ \runInner -> -      runToBase $ liftM StM . runInner . uncons-  restoreM (StM inner) =-    lift (restoreM inner) >>= \case-      Nothing -> mzero-      Just (h, t) -> cons h t-#endif  instance MonadError e m => MonadError e (ListT m) where   throwError = ListT . throwError-  catchError m handler = ListT $ catchError (unListT m) $ unListT . handler---- * Classes------------------------------ |--- A monad transformer capable of deconstructing like a list.-class MonadTrans t => MonadTransUncons t where-  -- |-  -- Execute in the inner monad,-  -- getting the head and the tail.-  -- Returns nothing if it's empty.-  uncons :: Monad m => t m a -> m (Maybe (a, t m a))--instance MonadTransUncons ListT where-  {-# INLINE uncons #-}-  uncons (ListT m) = m----- |--- A monad capable of constructing like a list.-class MonadPlus m => MonadCons m where-  -- |-  -- Prepend an element.-  cons :: a -> m a -> m a--instance MonadCons [] where-  cons a m = a : m--instance Monad m => MonadCons (ListT m) where-  {-# INLINABLE cons #-}-  cons h t = ListT $ return (Just (h, t))--instance MonadCons m => MonadCons (ReaderT e m) where-  cons a m = ReaderT $ cons a . runReaderT m+  catchError m handler = ListT $ catchError (uncons m) $ uncons . handler   -- * Execution in the inner monad -------------------------  -- |+-- Execute in the inner monad,+-- getting the head and the tail.+-- Returns nothing if it's empty.+uncons :: ListT m a -> m (Maybe (a, ListT m a))+uncons (ListT m) =+  m++-- | -- Execute, getting the head. Returns nothing if it's empty. {-# INLINABLE head #-}-head :: (Monad m, MonadTransUncons t) => t m a -> m (Maybe a)+head :: Monad m => ListT m a -> m (Maybe a) head =   liftM (fmap fst) . uncons  -- | -- Execute, getting the tail. Returns nothing if it's empty. {-# INLINABLE tail #-}-tail :: (Monad m, MonadTransUncons t) => t m a -> m (Maybe (t m a))+tail :: Monad m => ListT m a -> m (Maybe (ListT m a)) tail =   liftM (fmap snd) . uncons  -- | -- Execute, checking whether it's empty. {-# INLINABLE null #-}-null :: (Monad m, MonadTransUncons t) => t m a -> m Bool+null :: Monad m => ListT m a -> m Bool null =   liftM (maybe True (const False)) . uncons  -- | -- Execute, applying a left fold. {-# INLINABLE fold #-}-fold :: (Monad m, MonadTransUncons t) => (r -> a -> m r) -> r -> t m a -> m r+fold :: Monad m => (r -> a -> m r) -> r -> ListT m a -> m r fold s r =    uncons >=> maybe (return r) (\(h, t) -> s r h >>= \r' -> fold s r' t)  -- | -- A version of 'fold', which allows early termination. {-# INLINABLE foldMaybe #-}-foldMaybe :: (Monad m, MonadTransUncons t) => (r -> a -> m (Maybe r)) -> r -> t m a -> m r+foldMaybe :: Monad m => (r -> a -> m (Maybe r)) -> r -> ListT m a -> m r foldMaybe s r l =   liftM (maybe r id) $ runMaybeT $ do     (h, t) <- MaybeT $ uncons l@@ -235,29 +191,29 @@ -- | -- Execute, folding to a list. {-# INLINABLE toList #-}-toList :: (Monad m, MonadTransUncons t) => t m a -> m [a]+toList :: Monad m => ListT m a -> m [a] toList =   liftM ($ []) . fold (\f e -> return $ f . (e :)) id  -- |--- Execute, folding to a list in a reverse order.+-- Execute, folding to a list in the reverse order. -- Performs more efficiently than 'toList'. {-# INLINABLE toReverseList #-}-toReverseList :: (Monad m, MonadTransUncons t) => t m a -> m [a]+toReverseList :: Monad m => ListT m a -> m [a] toReverseList =   ListT.fold (\l -> return . (:l)) []  -- | -- Execute, traversing the stream with a side effect in the inner monad.  {-# INLINABLE traverse_ #-}-traverse_ :: (Monad m, MonadTransUncons t) => (a -> m ()) -> t m a -> m ()+traverse_ :: Monad m => (a -> m ()) -> ListT m a -> m () traverse_ f =   fold (const f) ()  -- | -- Execute, consuming a list of the specified length and returning the remainder stream. {-# INLINABLE splitAt #-}-splitAt :: (Monad m, MonadTransUncons t, MonadPlus (t m)) => Int -> t m a -> m ([a], t m a)+splitAt :: Monad m => Int -> ListT m a -> m ([a], ListT m a) splitAt =   \case     n | n > 0 -> \l ->@@ -274,32 +230,38 @@ -------------------------  -- |+-- Prepend an element.+cons :: Monad m => a -> ListT m a -> ListT m a+cons h t =+  ListT $ return (Just (h, t))++-- | -- Construct from any foldable. {-# INLINABLE fromFoldable #-}-fromFoldable :: (MonadCons m, Foldable f) => f a -> m a+fromFoldable :: (Monad m, Foldable f) => f a -> ListT m a fromFoldable =    foldr cons mzero  -- |--- Construct from an MVar, interpreting a value of Nothing as an end.-fromMVar :: (MonadCons m, MonadIO m) => MVar (Maybe a) -> m a+-- Construct from an MVar, interpreting the value of Nothing as the end.+fromMVar :: (MonadIO m) => MVar (Maybe a) -> ListT m a fromMVar v =   fix $ \loop -> liftIO (takeMVar v) >>= maybe mzero (flip cons loop)  -- | -- Construct by unfolding a pure data structure. {-# INLINABLE unfold #-}-unfold :: (MonadCons m) => (b -> Maybe (a, b)) -> b -> m a+unfold :: Monad m => (b -> Maybe (a, b)) -> b -> ListT m a unfold f s =   maybe mzero (\(h, t) -> cons h (unfold f t)) (f s)  -- | -- Construct by unfolding a monadic data structure ----- This is the most memory-efficient way to construct a ListT where+-- This is the most memory-efficient way to construct ListT where -- the length depends on the inner monad. {-# INLINABLE unfoldM #-}-unfoldM :: (Monad m) => (b -> m (Maybe (a, b))) -> b -> ListT m a+unfoldM :: Monad m => (b -> m (Maybe (a, b))) -> b -> ListT m a unfoldM f = go where   go s = ListT $ f s >>= \case     Nothing -> return Nothing@@ -308,7 +270,7 @@ -- | -- Produce an infinite stream. {-# INLINABLE repeat #-}-repeat :: (MonadCons m) => a -> m a+repeat :: Monad m => a -> ListT m a repeat =    fix . cons @@ -317,19 +279,10 @@ -------------------------  -- |--- A function, which updates the contents of a list transformer.--- --- Since it's merely just a function,--- you can run it by passing a list transformer as an argument.-type Transformation m a b = -  forall t. (Monad m, MonadCons (t m), MonadTransUncons t) =>-  t m a -> t m b---- | -- A transformation, -- which traverses the stream with an action in the inner monad. {-# INLINABLE traverse #-}-traverse :: (a -> m b) -> Transformation m a b+traverse :: Monad m => (a -> m b) -> ListT m a -> ListT m b traverse f s =   lift (uncons s) >>=    mapM (\(h, t) -> lift (f h) >>= \h' -> cons h' (traverse f t)) >>=@@ -339,7 +292,7 @@ -- A transformation, -- reproducing the behaviour of @Data.List.'Data.List.take'@. {-# INLINABLE take #-}-take :: Int -> Transformation m a a+take :: Monad m => Int -> ListT m a -> ListT m a take =   \case     n | n > 0 -> \t ->@@ -354,7 +307,7 @@ -- A transformation, -- reproducing the behaviour of @Data.List.'Data.List.drop'@. {-# INLINABLE drop #-}-drop :: Int -> Transformation m a a+drop :: Monad m => Int -> ListT m a -> ListT m a drop =   \case     n | n > 0 ->@@ -366,30 +319,11 @@ -- A transformation, -- which slices a list into chunks of the specified length. {-# INLINABLE slice #-}-slice :: Positive Int -> Transformation m a [a]+slice :: Monad m => Int -> ListT m a -> ListT m [a] slice n l =    do-    (h, t) <- lift $ splitAt (case n of Positive n -> n) l+    (h, t) <- lift $ splitAt n l     case h of       [] -> mzero       _ -> cons h (slice n t)----- * Positive numbers------------------------------ |--- A newtype wrapper around a number,--- which ensures that it is greater than zero.-newtype Positive n = -  Positive n-  deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)---- |--- A smart constructor for positive numbers.-positive :: (Ord n, Num n) => n -> Maybe (Positive n)-positive =-  \case-    n | n > 0 -> Just $ Positive n-    _ -> Nothing 
list-t.cabal view
@@ -1,7 +1,7 @@ name:   list-t version:-  0.4.7+  1 synopsis:   ListT done right description:
tests/Main.hs view
@@ -145,7 +145,7 @@      test_slice =   assertEqual ["abc", "def", "gh"] =<< do-    toList $ L.slice (fromJust $ L.positive 3) $ L.fromFoldable ("abcdefgh" :: [Char])+    toList $ L.slice 3 $ L.fromFoldable ("abcdefgh" :: [Char])   toList :: Monad m => L.ListT m a -> m [a]