packages feed

papa-0.4.0: src/Papa/Base/Data/Traversable.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

module Papa.Base.Data.Traversable (
  module P,
  mapM,
  forM,
  sequence,
) where

import Data.Traversable as P (
  Traversable (sequenceA, traverse),
  fmapDefault,
  foldMapDefault,
  for,
  mapAccumL,
 )

import Control.Applicative (Applicative)

{-# INLINE mapM #-}
-- | Map each element to an action, evaluate these actions left to right, and collect the results.
--
-- >>> import Data.Maybe (Maybe(..))
-- >>> mapM (\x -> Just x) [1,2,3]
-- Just [1,2,3]
-- >>> mapM (\_ -> Nothing) [1,2,3]
-- Nothing
mapM ::
  (Traversable t, Applicative f) =>
  (a -> f b) ->
  t a ->
  f (t b)
mapM =
  traverse

{-# INLINE forM #-}
-- | 'forM' is 'mapM' with its arguments flipped.
--
-- >>> import Data.Maybe (Maybe(..))
-- >>> forM [1,2,3] (\x -> Just x)
-- Just [1,2,3]
forM ::
  (Traversable t, Applicative f) =>
  t a ->
  (a -> f b) ->
  f (t b)
forM a f =
  traverse f a

{-# INLINE sequence #-}
-- | Evaluate each action in the structure left to right, and collect the results.
--
-- >>> import Data.Maybe (Maybe(..))
-- >>> sequence [Just 1, Just 2, Just 3]
-- Just [1,2,3]
-- >>> sequence [Just 1, Nothing, Just 3]
-- Nothing
sequence ::
  (Traversable t, Applicative f) =>
  t (f a) ->
  f (t a)
sequence =
  sequenceA