packages feed

papa-0.7.0: src/Papa/Base/Data/Semigroup.hs

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

-- | Semigroups, with total repetition.
--
-- The repetition functions of "Data.Semigroup" ('Data.Semigroup.stimes'
-- and the functions for defining it) throw for a count that is out of
-- range. These return 'Nothing' there instead. To define the 'stimes'
-- method of an instance, import "Data.Semigroup".
module Papa.Base.Data.Semigroup (
  module P,
  stimes,
  stimesMonoid,
  stimesIdempotent,
  stimesIdempotentMonoid,
  mtimesDefault,
) where

import Data.Bool (bool)
import Data.Maybe (Maybe (Just, Nothing))
import Data.Monoid (Monoid)
import Data.Ord (Ord ((<), (<=)))
import Data.Semigroup as P (
  All (All, getAll),
  Any (Any, getAny),
  Arg (Arg),
  ArgMax,
  ArgMin,
  Dual (Dual, getDual),
  Endo (Endo, appEndo),
  First (First, getFirst),
  Last (Last, getLast),
  Max (Max, getMax),
  Min (Min, getMin),
  Product (Product, getProduct),
  Semigroup (sconcat, (<>)),
  Sum (Sum, getSum),
  WrappedMonoid (WrapMonoid, unwrapMonoid),
  cycle1,
  diff,
 )
import qualified Data.Semigroup as S
import Prelude (Integral)

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genString = Gen.string (Range.linear 0 10) Gen.alpha

-- | Total 'S.stimes': repeat a value a positive number of times. 'Nothing'
-- for a count that is not positive.
--
-- ==== __Examples__
--
-- >>> stimes (3 :: Int) "ab"
-- Just "ababab"
--
-- >>> stimes (0 :: Int) "ab"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; n <- forAll (Gen.int (Range.linear 1 10)); stimes (n + 1) s === fmap (s <>) (stimes n s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
stimes :: (Integral b, Semigroup a) => b -> a -> Maybe a
stimes n a =
  bool (Just (S.stimes n a)) Nothing (n <= 0)
{-# INLINE stimes #-}

-- | Total 'S.stimesMonoid': repeat a value a non-negative number of times.
-- 'Nothing' for a negative count.
--
-- ==== __Examples__
--
-- >>> stimesMonoid (0 :: Int) "ab"
-- Just ""
--
-- >>> stimesMonoid (-1 :: Int) "ab"
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesMonoid n s === if n < 0 then Nothing else Just (concat (replicate n s))
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
stimesMonoid :: (Integral b, Monoid a) => b -> a -> Maybe a
stimesMonoid n a =
  bool (Just (S.stimesMonoid n a)) Nothing (n < 0)
{-# INLINE stimesMonoid #-}

-- | Total 'S.stimesIdempotent': repetition of an idempotent value
-- (@a '<>' a = a@), which is the value itself. 'Nothing' for a count that is
-- not positive.
--
-- ==== __Examples__
--
-- >>> stimesIdempotent (5 :: Int) (Max 'x')
-- Just (Max {getMax = 'x'})
--
-- >>> stimesIdempotent (0 :: Int) (Max 'x')
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll Gen.alpha; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesIdempotent n (Max c) === if n <= 0 then Nothing else Just (Max c)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
stimesIdempotent :: (Integral b) => b -> a -> Maybe a
stimesIdempotent n a =
  bool (Just a) Nothing (n <= 0)
{-# INLINE stimesIdempotent #-}

-- | Total 'S.stimesIdempotentMonoid': repetition of an idempotent value in a
-- monoid, which is 'Data.Monoid.mempty' for a count of zero. 'Nothing' for a
-- negative count.
--
-- ==== __Examples__
--
-- >>> stimesIdempotentMonoid (0 :: Int) (Any True)
-- Just (Any {getAny = False})
--
-- >>> stimesIdempotentMonoid (-1 :: Int) (Any True)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do b <- forAll Gen.bool; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesIdempotentMonoid n (Any b) === if n < 0 then Nothing else Just (Any (b && n > 0))
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
stimesIdempotentMonoid :: (Integral b, Monoid a) => b -> a -> Maybe a
stimesIdempotentMonoid n a =
  bool (Just (S.stimesIdempotentMonoid n a)) Nothing (n < 0)
{-# INLINE stimesIdempotentMonoid #-}

-- | Total 'S.mtimesDefault': repeat a value in a monoid a non-negative number
-- of times. 'Nothing' for a negative count.
--
-- ==== __Examples__
--
-- >>> mtimesDefault (2 :: Int) [1, 2 :: Int]
-- Just [1,2,1,2]
--
-- >>> mtimesDefault (-2 :: Int) [1, 2 :: Int]
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genString; n <- forAll (Gen.int (Range.linear 0 10)); mtimesDefault n s === stimesMonoid n s
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
mtimesDefault :: (Integral b, Monoid a) => b -> a -> Maybe a
mtimesDefault n a =
  bool (Just (S.mtimesDefault n a)) Nothing (n < 0)
{-# INLINE mtimesDefault #-}