1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
-- |
-- Module : Basement.Mappable
-- License : BSD-style
-- Maintainer : Nicolas Di Prima <nicolas@primetype.co.uk>
-- Stability : experimental
-- Portability : portable
--
-- Class of collection that can be traversed from left to right,
-- performing an action on each element.
--
module Foundation.Collection.Mappable
( Mappable(..)
, sequence_
, traverse_
, mapM_
, forM
, forM_
) where
import Basement.Compat.Base
import qualified Data.Traversable
import Basement.BoxedArray (Array)
-- | Functors representing data structures that can be traversed from
-- left to right.
--
-- Mostly like base's `Traversable` but applied to collections only.
--
class Functor collection => Mappable collection where
{-# MINIMAL traverse | sequenceA #-}
-- | Map each element of a structure to an action, evaluate these actions
-- from left to right, and collect the results. For a version that ignores
-- the results see 'Foundation.Collection.traverse_'.
traverse :: Applicative f => (a -> f b)
-> collection a
-> f (collection b)
traverse f = sequenceA . fmap f
-- | Evaluate each actions of the given collections, from left to right,
-- and collect the results. For a version that ignores the results, see
-- `Foundation.Collection.sequenceA_`
sequenceA :: Applicative f => collection (f a)
-> f (collection a)
sequenceA = traverse id
-- | Map each element of the collection to an action, evaluate these actions
-- from left to right, and collect the results. For a version that ignores
-- the results see 'Foundation.Collection.mapM_'.
mapM :: (Applicative m, Monad m) => (a -> m b) -> collection a -> m (collection b)
mapM = traverse
-- | Evaluate each actions of the given collections, from left to right,
-- and collect the results. For a version that ignores the results, see
-- `Foundation.Collection.sequence_`
sequence :: (Applicative m, Monad m) => collection (m a) -> m (collection a)
sequence = sequenceA
-- | Map each element of a collection to an action, evaluate these
-- actions from left to right, and ignore the results. For a version
-- that doesn't ignore the results see 'Foundation.Collection.traverse`
traverse_ :: (Mappable col, Applicative f) => (a -> f b) -> col a -> f ()
traverse_ f col = traverse f col *> pure ()
-- | Evaluate each action in the collection from left to right, and
-- ignore the results. For a version that doesn't ignore the results
-- see 'Foundation.Collection.sequenceA'.
--sequenceA_ :: (Mappable col, Applicative f) => col (f a) -> f ()
--sequenceA_ col = sequenceA col *> pure ()
-- | Map each element of a collection to a monadic action, evaluate
-- these actions from left to right, and ignore the results. For a
-- version that doesn't ignore the results see
-- 'Foundation.Collection.mapM'.
mapM_ :: (Mappable col, Applicative m, Monad m) => (a -> m b) -> col a -> m ()
mapM_ f c = mapM f c *> return ()
-- | Evaluate each monadic action in the collection from left to right,
-- and ignore the results. For a version that doesn't ignore the
-- results see 'Foundation.Collection.sequence'.
sequence_ :: (Mappable col, Applicative m, Monad m) => col (m a) -> m ()
sequence_ c = sequence c *> return ()
-- | 'forM' is 'mapM' with its arguments flipped. For a version that
-- ignores the results see 'Foundation.Collection.forM_'.
forM :: (Mappable col, Applicative m, Monad m) => col a -> (a -> m b) -> m (col b)
forM = flip mapM
-- | 'forM_' is 'mapM_' with its arguments flipped. For a version that
-- doesn't ignore the results see 'Foundation.Collection.forM'.
forM_ :: (Mappable col, Applicative m, Monad m) => col a -> (a -> m b) -> m ()
forM_ = flip mapM_
----------------------------
-- Foldable instances
----------------------------
instance Mappable [] where
{-# INLINE traverse #-}
traverse = Data.Traversable.traverse
instance Mappable Array where
-- | TODO: to optimise
traverse f arr = fromList <$> traverse f (toList arr)
|