papa-0.7.0: src/Papa/Base/Data/Foldable.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Folds, with the monadic folds of "Prelude" generalised to 'Applicative'.
module Papa.Base.Data.Foldable (
module P,
mapM_,
forM_,
sequence_,
) where
import Control.Applicative (Applicative)
import Data.Foldable as P (
Foldable (elem, fold, foldMap, foldMap', foldl, foldl', foldr, foldr', length, null, product, sum, toList),
all,
and,
any,
asum,
concat,
concatMap,
find,
foldlM,
foldrM,
for_,
notElem,
or,
sequenceA_,
traverse_,
)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> import Control.Monad.Trans.Writer.CPS (runWriter, tell)
-- >>> let genInts = Gen.list (Range.linear 0 20) (Gen.int (Range.linear (-100) 100))
-- | 'traverse_', under the name of "Prelude"'s @mapM_@, which needs only
-- 'Applicative'.
--
-- ==== __Examples__
--
-- >>> mapM_ (\x -> Just x) [1, 2, 3 :: Int]
-- Just ()
--
-- >>> mapM_ (\x -> if x > 2 then Nothing else Just x) [1, 2, 3 :: Int]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; f <- forAll (fn (Gen.list (Range.linear 0 3) (Gen.int (Range.linear 0 9)))); runWriter (mapM_ (tell . apply f) xs) === runWriter (traverse_ (tell . apply f) xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
mapM_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
mapM_ =
traverse_
{-# INLINE mapM_ #-}
-- | 'for_', under the name of "Prelude"'s @forM_@, which needs only
-- 'Applicative'.
--
-- ==== __Examples__
--
-- >>> forM_ [1, 2, 3 :: Int] (\x -> Just x)
-- Just ()
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; f <- forAll (fn (Gen.list (Range.linear 0 3) (Gen.int (Range.linear 0 9)))); runWriter (forM_ xs (tell . apply f)) === runWriter (traverse_ (tell . apply f) xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
forM_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
forM_ =
for_
{-# INLINE forM_ #-}
-- | 'sequenceA_', under the name of "Prelude"'s @sequence_@, which needs
-- only 'Applicative'.
--
-- ==== __Examples__
--
-- >>> sequence_ [Just 1, Just (2 :: Int)]
-- Just ()
--
-- >>> sequence_ [Just 1, Nothing :: Maybe Int]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do xss <- forAll (Gen.list (Range.linear 0 5) genInts); runWriter (sequence_ (fmap tell xss)) === runWriter (sequenceA_ (fmap tell xss))
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
sequence_ :: (Foldable t, Applicative f) => t (f a) -> f ()
sequence_ =
sequenceA_
{-# INLINE sequence_ #-}