papa-0.7.0: src/Papa/Bifunctors/Data/Bifoldable.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Folds of both sides of a 'Bifoldable', under the names of the list
-- functions @concat@ and @concatMap@.
module Papa.Bifunctors.Data.Bifoldable (
biconcat,
biconcatMap,
) where
import Data.Bifoldable (Bifoldable (bifold, bifoldMap))
import Data.Monoid (Monoid)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genString = Gen.string (Range.linear 0 5) Gen.alpha
-- | Combine both sides of a 'Bifoldable' of a 'Monoid'. This is 'bifold'.
--
-- ==== __Examples__
--
-- >>> biconcat ("ab", "cd")
-- "abcd"
--
-- >>> biconcat (Left "ab" :: Either String String)
-- "ab"
--
-- >>> check $ withTests 100 $ property $ do a <- forAll genString; b <- forAll genString; biconcat (a, b) === a <> b
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
biconcat :: (Bifoldable p, Monoid m) => p m m -> m
biconcat =
bifold
{-# INLINE biconcat #-}
-- | Map both sides of a 'Bifoldable' into a 'Monoid', and combine them.
-- This is 'bifoldMap'.
--
-- ==== __Examples__
--
-- >>> biconcatMap show (: []) (1 :: Int, 'x')
-- "1x"
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genString); g <- forAll (fn genString); a <- forAll (Gen.int (Range.linear 0 9)); b <- forAll Gen.bool; biconcatMap (apply f) (apply g) (a, b) === apply f a <> apply g b
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
biconcatMap :: (Bifoldable p, Monoid m) => (a -> m) -> (b -> m) -> p a b -> m
biconcatMap =
bifoldMap
{-# INLINE biconcatMap #-}