-----------------------------------------------------------------------------
-- |
-- Module : Data.Traversable
-----------------------------------------------------------------------------
-- |
-- Module : Data.Traversable
import Prelude hiding (mapM, sequence, foldr)
import qualified Prelude (mapM, foldr)
import Prelude hiding (mapM, sequence, foldr)
import qualified Prelude (mapM, foldr)
- -- | Map each element of a structure to an action, evaluate
- -- these actions from left to right, and collect the results.
- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
- traverse f = sequenceA . fmap f
-
- -- | Evaluate each action in the structure from left to right,
- -- and collect the results.
- sequenceA :: Applicative f => t (f a) -> f (t a)
- sequenceA = traverse id
-
- -- | Map each element of a structure to a monadic action, evaluate
- -- these actions from left to right, and collect the results.
- mapM :: Monad m => (a -> m b) -> t a -> m (t b)
- mapM f = unwrapMonad . traverse (WrapMonad . f)
-
- -- | Evaluate each monadic action in the structure from left to right,
- -- and collect the results.
- sequence :: Monad m => t (m a) -> m (t a)
- sequence = mapM id
+ -- | Map each element of a structure to an action, evaluate
+ -- these actions from left to right, and collect the results.
+ traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
+ traverse f = sequenceA . fmap f
+
+ -- | Evaluate each action in the structure from left to right,
+ -- and collect the results.
+ sequenceA :: Applicative f => t (f a) -> f (t a)
+ sequenceA = traverse id
+
+ -- | Map each element of a structure to a monadic action, evaluate
+ -- these actions from left to right, and collect the results.
+ mapM :: Monad m => (a -> m b) -> t a -> m (t b)
+ mapM f = unwrapMonad . traverse (WrapMonad . f)
+
+ -- | Evaluate each monadic action in the structure from left to right,
+ -- and collect the results.
+ sequence :: Monad m => t (m a) -> m (t a)
+ sequence = mapM id
- traverse f = Prelude.foldr cons_f (pure [])
- where cons_f x ys = (:) <$> f x <*> ys
+ {-# INLINE traverse #-} -- so that traverse can fuse
+ traverse f = Prelude.foldr cons_f (pure [])
+ where cons_f x ys = (:) <$> f x <*> ys
newtype StateL s a = StateL { runStateL :: s -> (s, a) }
instance Functor (StateL s) where
newtype StateL s a = StateL { runStateL :: s -> (s, a) }
instance Functor (StateL s) where
- fmap f (StateL k) = StateL $ \ s ->
- let (s', v) = k s in (s', f v)
+ fmap f (StateL k) = StateL $ \ s -> let (s', v) = k s in (s', f v)
- pure x = StateL (\ s -> (s, x))
- StateL kf <*> StateL kv = StateL $ \ s ->
- let (s', f) = kf s
- (s'', v) = kv s'
- in (s'', f v)
+ pure x = StateL (\ s -> (s, x))
+ StateL kf <*> StateL kv = StateL $ \ s ->
+ let (s', f) = kf s
+ (s'', v) = kv s'
+ in (s'', f v)
-- |The 'mapAccumL' function behaves like a combination of 'fmap'
-- and 'foldl'; it applies a function to each element of a structure,
-- |The 'mapAccumL' function behaves like a combination of 'fmap'
-- and 'foldl'; it applies a function to each element of a structure,
newtype StateR s a = StateR { runStateR :: s -> (s, a) }
instance Functor (StateR s) where
newtype StateR s a = StateR { runStateR :: s -> (s, a) }
instance Functor (StateR s) where
- fmap f (StateR k) = StateR $ \ s ->
- let (s', v) = k s in (s', f v)
+ fmap f (StateR k) = StateR $ \ s -> let (s', v) = k s in (s', f v)
- pure x = StateR (\ s -> (s, x))
- StateR kf <*> StateR kv = StateR $ \ s ->
- let (s', v) = kv s
- (s'', f) = kf s'
- in (s'', f v)
+ pure x = StateR (\ s -> (s, x))
+ StateR kf <*> StateR kv = StateR $ \ s ->
+ let (s', v) = kv s
+ (s'', f) = kf s'
+ in (s'', f v)
-- |The 'mapAccumR' function behaves like a combination of 'fmap'
-- and 'foldr'; it applies a function to each element of a structure,
-- |The 'mapAccumR' function behaves like a combination of 'fmap'
-- and 'foldr'; it applies a function to each element of a structure,
-- | This function may be used as a value for `fmap` in a `Functor` instance.
fmapDefault :: Traversable t => (a -> b) -> t a -> t b
-- | This function may be used as a value for `fmap` in a `Functor` instance.
fmapDefault :: Traversable t => (a -> b) -> t a -> t b
fmapDefault f = getId . traverse (Id . f)
-- | This function may be used as a value for `Data.Foldable.foldMap`
fmapDefault f = getId . traverse (Id . f)
-- | This function may be used as a value for `Data.Foldable.foldMap`