packages feed

papa-0.7.0: src/Papa/Containers/Data/Seq.hs

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

-- | Finite sequences, with total versions of the partial functions of
-- "Data.Sequence". Import qualified:
--
-- > import qualified Papa.Containers.Data.Seq as Seq
--
-- The functions of "Data.Sequence" that throw (@index@ out of range;
-- @replicate@, @replicateA@, @replicateM@, @iterateN@ and @fromFunction@
-- with a negative length; @chunksOf@ with a non-positive size;
-- @cycleTaking@ of an empty sequence; @scanl1@ and @scanr1@ of an empty
-- sequence) are replaced by functions of the same names that return
-- 'Data.Maybe.Nothing' there instead.
module Papa.Containers.Data.Seq (
  module X,
  index,
  replicate,
  replicateA,
  replicateM,
  iterateN,
  fromFunction,
  chunksOf,
  cycleTaking,
  scanl1,
  scanr1,
) where

import Control.Applicative (Applicative)
import Control.Monad (Monad, guard)
import Data.Bool (not, (||))
import Data.Function (flip)
import Data.Functor ((<$))
import Data.Int (Int)
import Data.Maybe (Maybe)
import Data.Ord (Ord ((<=), (>)))
import Data.Sequence as X hiding (
  chunksOf,
  cycleTaking,
  fromFunction,
  index,
  iterateN,
  replicate,
  replicateA,
  replicateM,
  scanl1,
  scanr1,
 )
import qualified Data.Sequence as S

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Containers.Data.Seq as Seq
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linear (-100) 100)
-- >>> let genSeq = Seq.fromList <$> Gen.list (Range.linear 0 20) genInt
-- >>> let genLength = Gen.int (Range.linear (-5) 20)

-- | Total 'S.index': the element at an index. 'Data.Maybe.Nothing' for an index out of
-- range. This is 'S.lookup', with the arguments of 'S.index'.
--
-- ==== __Examples__
--
-- >>> Seq.index (Seq.fromList "abc") 1
-- Just 'b'
--
-- >>> Seq.index (Seq.fromList "abc") 3
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSeq; i <- forAll genLength; Seq.index s i === (toList s P.!? i)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
index :: Seq a -> Int -> Maybe a
index =
  flip S.lookup
{-# INLINE index #-}

-- | Total 'S.replicate': a sequence of a value repeated. 'Data.Maybe.Nothing' for a
-- negative length.
--
-- ==== __Examples__
--
-- >>> Seq.replicate 3 'a'
-- Just (fromList "aaa")
--
-- >>> Seq.replicate (-1) 'a'
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; a <- forAll genInt; fmap toList (Seq.replicate n a) === bool Nothing (Just (P.replicate n a)) (n >= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
replicate :: Int -> a -> Maybe (Seq a)
replicate n a =
  S.replicate n a <$ guard (0 <= n)
{-# INLINE replicate #-}

-- | Total 'S.replicateA': an action repeated, collecting the results in a
-- sequence. 'Data.Maybe.Nothing' for a negative length.
--
-- ==== __Examples__
--
-- >>> Seq.replicateA 2 (Just 'a')
-- Just (Just (fromList "aa"))
--
-- >>> Seq.replicateA (-1) (Just 'a')
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; a <- forAll genInt; Seq.replicateA n (Just a) === fmap Just (Seq.replicate n a)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
replicateA :: (Applicative f) => Int -> f a -> Maybe (f (Seq a))
replicateA n fa =
  S.replicateA n fa <$ guard (0 <= n)
{-# INLINE replicateA #-}

-- | Total 'S.replicateM': 'replicateA' at a 'Monad'. 'Data.Maybe.Nothing' for a
-- negative length.
--
-- ==== __Examples__
--
-- >>> Seq.replicateM 2 [0, 1 :: Int]
-- Just [fromList [0,0],fromList [0,1],fromList [1,0],fromList [1,1]]
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int (Range.linear (-3) 4)); Seq.replicateM n "ab" === Seq.replicateA n "ab"
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
replicateM :: (Monad m) => Int -> m a -> Maybe (m (Seq a))
replicateM n ma =
  S.replicateM n ma <$ guard (0 <= n)
{-# INLINE replicateM #-}

-- | Total 'S.iterateN': the first @n@ iterations of a function. 'Data.Maybe.Nothing'
-- for a negative length.
--
-- ==== __Examples__
--
-- >>> Seq.iterateN 4 (* 2) (1 :: Int)
-- Just (fromList [1,2,4,8])
--
-- >>> Seq.iterateN (-1) (* 2) (1 :: Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; f <- forAll (fn genInt); a <- forAll genInt; fmap toList (Seq.iterateN n (apply f) a) === bool Nothing (Just (P.take n (toList (iterate (apply f) a)))) (n >= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
iterateN :: Int -> (a -> a) -> a -> Maybe (Seq a)
iterateN n f a =
  S.iterateN n f a <$ guard (0 <= n)
{-# INLINE iterateN #-}

-- | Total 'S.fromFunction': the sequence of a function at @0@ to @n - 1@.
-- 'Data.Maybe.Nothing' for a negative length.
--
-- ==== __Examples__
--
-- >>> Seq.fromFunction 3 (* 10)
-- Just (fromList [0,10,20])
--
-- >>> Seq.fromFunction (-1) (* 10)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; f <- forAll (fn genInt); fmap toList (Seq.fromFunction n (apply f)) === bool Nothing (Just (fmap (apply f) (P.take n (toList (iterate (+ 1) 0))))) (n >= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
fromFunction :: Int -> (Int -> a) -> Maybe (Seq a)
fromFunction n f =
  S.fromFunction n f <$ guard (0 <= n)
{-# INLINE fromFunction #-}

-- | Total 'S.chunksOf': a sequence split into chunks of a size (the last
-- may be smaller). 'Data.Maybe.Nothing' for a size that is not positive, unless the
-- sequence is empty.
--
-- ==== __Examples__
--
-- >>> Seq.chunksOf 2 (Seq.fromList "abcde")
-- Just (fromList [fromList "ab",fromList "cd",fromList "e"])
--
-- >>> Seq.chunksOf 0 (Seq.fromList "abcde")
-- Nothing
--
-- >>> Seq.chunksOf 0 (Seq.empty :: Seq Char)
-- Just (fromList [])
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; s <- forAll genSeq; fmap (foldMap id) (Seq.chunksOf n s) === bool Nothing (Just s) (n > 0 || S.null s)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
chunksOf :: Int -> Seq a -> Maybe (Seq (Seq a))
chunksOf n s =
  S.chunksOf n s <$ guard (n > 0 || S.null s)
{-# INLINE chunksOf #-}

-- | Total 'S.cycleTaking': the first @n@ elements of a sequence repeated
-- forever. 'Data.Maybe.Nothing' for a positive length and an empty sequence.
--
-- ==== __Examples__
--
-- >>> Seq.cycleTaking 5 (Seq.fromList "ab")
-- Just (fromList "ababa")
--
-- >>> Seq.cycleTaking 5 (Seq.empty :: Seq Char)
-- Nothing
--
-- >>> Seq.cycleTaking 0 (Seq.empty :: Seq Char)
-- Just (fromList "")
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genLength; s <- forAll genSeq; fmap toList (Seq.cycleTaking n s) === bool (fmap (P.take n) (cycle (toList s))) (Just []) (n <= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
cycleTaking :: Int -> Seq a -> Maybe (Seq a)
cycleTaking n s =
  S.cycleTaking n s <$ guard (n <= 0 || not (S.null s))
{-# INLINE cycleTaking #-}

-- | Total 'S.scanl1': the left scan of a non-empty sequence. 'Data.Maybe.Nothing' for
-- the empty sequence.
--
-- ==== __Examples__
--
-- >>> Seq.scanl1 (+) (Seq.fromList [1, 2, 3 :: Int])
-- Just (fromList [1,3,6])
--
-- >>> Seq.scanl1 (+) (Seq.empty :: Seq Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSeq; fmap toList (Seq.scanl1 (+) s) === fmap (\(h :| t) -> toList (P.scanl (+) h t)) (nonEmpty (toList s))
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
scanl1 :: (a -> a -> a) -> Seq a -> Maybe (Seq a)
scanl1 f s =
  S.scanl1 f s <$ guard (not (S.null s))
{-# INLINE scanl1 #-}

-- | Total 'S.scanr1': the right scan of a non-empty sequence. 'Data.Maybe.Nothing' for
-- the empty sequence.
--
-- ==== __Examples__
--
-- >>> Seq.scanr1 (+) (Seq.fromList [1, 2, 3 :: Int])
-- Just (fromList [6,5,3])
--
-- >>> Seq.scanr1 (+) (Seq.empty :: Seq Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genSeq; fmap (toList . Seq.reverse) (Seq.scanr1 (+) s) === fmap (\(h :| t) -> toList (P.scanl (+) h t)) (nonEmpty (toList (Seq.reverse s)))
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
scanr1 :: (a -> a -> a) -> Seq a -> Maybe (Seq a)
scanr1 f s =
  S.scanr1 f s <$ guard (not (S.null s))
{-# INLINE scanr1 #-}