packages feed

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

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

module Papa.Base.Data.Foldable (
  module P,
  mapM_,
  forM_,
  sequence_,
) where

import Data.Foldable as P (
  Foldable (elem, fold, foldMap, foldl, foldl', foldr, foldr', null, product, sum, toList),
  all,
  and,
  any,
  asum,
  find,
  foldlM,
  foldrM,
  for_,
  notElem,
  or,
  sequenceA_,
  traverse_,
 )

import Control.Applicative (Applicative)
import Data.Foldable ()

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

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

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