module Foundation.Collection.Mappable
( Mappable(..)
, sequence_
, traverse_
, mapM_
, forM
, forM_
) where
import Basement.Compat.Base
import qualified Data.Traversable
import Basement.BoxedArray (Array)
class Functor collection => Mappable collection where
{-# MINIMAL traverse | sequenceA #-}
traverse :: Applicative f => (a -> f b)
-> collection a
-> f (collection b)
traverse a -> f b
f = collection (f b) -> f (collection b)
forall (collection :: * -> *) (f :: * -> *) a.
(Mappable collection, Applicative f) =>
collection (f a) -> f (collection a)
sequenceA (collection (f b) -> f (collection b))
-> (collection a -> collection (f b))
-> collection a
-> f (collection b)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> f b) -> collection a -> collection (f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> f b
f
sequenceA :: Applicative f => collection (f a)
-> f (collection a)
sequenceA = (f a -> f a) -> collection (f a) -> f (collection a)
forall (collection :: * -> *) (f :: * -> *) a b.
(Mappable collection, Applicative f) =>
(a -> f b) -> collection a -> f (collection b)
traverse f a -> f a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
mapM :: (Applicative m, Monad m) => (a -> m b) -> collection a -> m (collection b)
mapM = (a -> m b) -> collection a -> m (collection b)
forall (collection :: * -> *) (f :: * -> *) a b.
(Mappable collection, Applicative f) =>
(a -> f b) -> collection a -> f (collection b)
traverse
sequence :: (Applicative m, Monad m) => collection (m a) -> m (collection a)
sequence = collection (m a) -> m (collection a)
forall (collection :: * -> *) (f :: * -> *) a.
(Mappable collection, Applicative f) =>
collection (f a) -> f (collection a)
sequenceA
traverse_ :: (Mappable col, Applicative f) => (a -> f b) -> col a -> f ()
traverse_ :: (a -> f b) -> col a -> f ()
traverse_ a -> f b
f col a
col = (a -> f b) -> col a -> f (col b)
forall (collection :: * -> *) (f :: * -> *) a b.
(Mappable collection, Applicative f) =>
(a -> f b) -> collection a -> f (collection b)
traverse a -> f b
f col a
col f (col b) -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
mapM_ :: (Mappable col, Applicative m, Monad m) => (a -> m b) -> col a -> m ()
mapM_ :: (a -> m b) -> col a -> m ()
mapM_ a -> m b
f col a
c = (a -> m b) -> col a -> m (col b)
forall (collection :: * -> *) (m :: * -> *) a b.
(Mappable collection, Applicative m, Monad m) =>
(a -> m b) -> collection a -> m (collection b)
mapM a -> m b
f col a
c m (col b) -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
sequence_ :: (Mappable col, Applicative m, Monad m) => col (m a) -> m ()
sequence_ :: col (m a) -> m ()
sequence_ col (m a)
c = col (m a) -> m (col a)
forall (collection :: * -> *) (m :: * -> *) a.
(Mappable collection, Applicative m, Monad m) =>
collection (m a) -> m (collection a)
sequence col (m a)
c m (col a) -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
forM :: (Mappable col, Applicative m, Monad m) => col a -> (a -> m b) -> m (col b)
forM :: col a -> (a -> m b) -> m (col b)
forM = ((a -> m b) -> col a -> m (col b))
-> col a -> (a -> m b) -> m (col b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> m b) -> col a -> m (col b)
forall (collection :: * -> *) (m :: * -> *) a b.
(Mappable collection, Applicative m, Monad m) =>
(a -> m b) -> collection a -> m (collection b)
mapM
forM_ :: (Mappable col, Applicative m, Monad m) => col a -> (a -> m b) -> m ()
forM_ :: col a -> (a -> m b) -> m ()
forM_ = ((a -> m b) -> col a -> m ()) -> col a -> (a -> m b) -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> m b) -> col a -> m ()
forall (col :: * -> *) (m :: * -> *) a b.
(Mappable col, Applicative m, Monad m) =>
(a -> m b) -> col a -> m ()
mapM_
instance Mappable [] where
{-# INLINE traverse #-}
traverse :: (a -> f b) -> [a] -> f [b]
traverse = (a -> f b) -> [a] -> f [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
Data.Traversable.traverse
instance Mappable Array where
traverse :: (a -> f b) -> Array a -> f (Array b)
traverse a -> f b
f Array a
arr = [b] -> Array b
forall l. IsList l => [Item l] -> l
fromList ([b] -> Array b) -> f [b] -> f (Array b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> [a] -> f [b]
forall (collection :: * -> *) (f :: * -> *) a b.
(Mappable collection, Applicative f) =>
(a -> f b) -> collection a -> f (collection b)
traverse a -> f b
f (Array a -> [Item (Array a)]
forall l. IsList l => l -> [Item l]
toList Array a
arr)