-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A partial binary associative operator -- -- A partial semigroup is like a semigroup, but the operator is partial. -- We represent this in Haskell as a total function appendMaybe :: a -- -> a -> Maybe a. @package partial-semigroup @version 0.0.0.1 module Data.PartialSemigroup -- | A partial semigroup is like a Semigroup, but with an operator -- returning Maybe a rather than a. -- -- For comparison: -- --
-- (<>) :: Semigroup a => a -> a -> a -- appendMaybe :: PartialSemigroup a => a -> a -> Maybe a ---- --
-- >>> appendMaybe (AppendLeft (Left "ab")) (AppendLeft (Left "cd"))
-- Just (AppendLeft {unAppendLeft = Left "abcd"})
--
--
-- Anything else produces Nothing
--
-- -- >>> appendMaybe (AppendLeft (Right "ab")) (AppendLeft (Right "cd")) -- Nothing ---- -- groupAndConcat combines consecutive Left values, leaving -- the Right values unmodified. -- --
-- >>> xs = [Left "a", Left "b", Right "c", Right "d", Left "e", Left "f"] -- -- >>> fmap unAppendLeft . groupAndConcat . fmap AppendLeft $ xs -- [Left "ab",Right "c",Right "d",Left "ef"] --newtype AppendLeft a b AppendLeft :: Either a b -> AppendLeft a b [unAppendLeft] :: AppendLeft a b -> Either a b -- | A wrapper for Either where the PartialSemigroup operator -- is defined only over Right values. -- --
-- >>> appendMaybe (AppendRight (Right "ab")) (AppendRight (Right "cd"))
-- Just (AppendRight {unAppendRight = Right "abcd"})
--
--
-- Anything else produces Nothing
--
-- -- >>> appendMaybe (AppendRight (Left "ab")) (AppendRight (Left "cd")) -- Nothing ---- -- groupAndConcat combines consecutive Right values, -- leaving the Left values unmodified. -- --
-- >>> xs = [Left "a", Left "b", Right "c", Right "d", Left "e", Left "f"] -- -- >>> fmap unAppendRight . groupAndConcat . fmap AppendRight $ xs -- [Left "a",Left "b",Right "cd",Left "e",Left "f"] --newtype AppendRight a b AppendRight :: Either a b -> AppendRight a b [unAppendRight] :: AppendRight a b -> Either a b -- | Apply a semigroup operation to any pairs of consecutive list elements -- where the semigroup operation is defined over them. -- --
-- >>> xs = [Left "a", Right "b", Right "c", Left "d", Left "e", Left "f"] -- -- >>> groupAndConcat xs -- [Left "a",Right "bc",Left "def"] --groupAndConcat :: forall a. PartialSemigroup a => [a] -> [a] -- | If xs is nonempty and the partial semigroup operator is -- defined for all pairs of values in xs, then -- partialConcat xs produces a Just result with -- the combination of all the values. Otherwise, returns Nothing. -- --
-- >>> partialConcat [Left "a", Left "b", Left "c"] -- Just (Left "abc") ---- -- When some values cannot be combined, we get Nothing. -- --
-- >>> partialConcat [Left "a", Left "b", Right "c"] -- Nothing ---- -- When the list is empty, we get Nothing. -- --
-- >>> partialConcat [] -- Nothing --partialConcat :: forall a. PartialSemigroup a => [a] -> Maybe a -- | Like partialConcat, but for non-empty lists. -- --
-- >>> partialConcat1 (Left "a" :| [Left "b", Left "c"]) -- Just (Left "abc") ---- -- When some values cannot be combined, we get Nothing. -- --
-- >>> partialConcat1 (Left "a" :| [Left "b", Right "c"]) -- Nothing --partialConcat1 :: forall a. PartialSemigroup a => NonEmpty a -> Maybe a -- |
-- >>> xs = [Left "a", Left "b", Right "c"] -- -- >>> ys = [Left "1", Left "2", Right "3"] -- -- >>> partialZip xs ys -- Just [Left "a1",Left "b2",Right "c3"] ---- -- If the pairs do not all combine, then we get Nothing. -- --
-- >>> xs = [Left "a", Left "b", Right "c"] -- -- >>> ys = [Left "1", Right "2", Right "3"] -- -- >>> partialZip xs ys -- Nothing ---- -- If the lists have different lengths, then we get Nothing. -- --
-- >>> xs = [Left "a", Left "b", Right "c"] -- -- >>> ys = [Left "1", Left "2"] -- -- >>> partialZip xs ys -- Nothing --partialZip :: forall a. PartialSemigroup a => [a] -> [a] -> Maybe [a] -- | Like partialZip, but for non-empty lists. -- --
-- >>> xs = Left "a" :| [Left "b", Right "c"] -- -- >>> ys = Left "1" :| [Left "2", Right "3"] -- -- >>> partialZip1 xs ys -- Just (Left "a1" :| [Left "b2",Right "c3"]) ---- -- If the pairs do not all combine, then we get Nothing. -- --
-- >>> xs = Left "a" :| [Left "b", Right "c"] -- -- >>> ys = Left "1" :| [Right "2", Right "3"] -- -- >>> partialZip1 xs ys -- Nothing ---- -- If the lists have different lengths, then we get Nothing. -- --
-- >>> xs = Left "a" :| [Left "b", Right "c"] -- -- >>> ys = Left "1" :| [Left "2"] -- -- >>> partialZip1 xs ys -- Nothing --partialZip1 :: forall a. PartialSemigroup a => NonEmpty a -> NonEmpty a -> Maybe (NonEmpty a) -- | A wrapper to turn any value with a Semigroup instance into a -- value with a PartialSemigroup instance whose appendMaybe -- operator always returns Just. -- --
-- >>> appendMaybe (Total "ab") (Total "cd")
-- Just (Total {unTotal = "abcd"})
--
--
-- -- >>> f = getProduct . unTotal -- -- >>> g = Total . Product -- -- >>> fmap f . partialConcat . fmap g $ [1..4] -- Just 24 --newtype Total a Total :: a -> Total a [unTotal] :: Total a -> a -- | A wrapper for Maybe with an error-propagating Semigroup. newtype Partial a Partial :: Maybe a -> Partial a [unPartial] :: Partial a -> Maybe a instance (GHC.Show.Show a, GHC.Show.Show b) => GHC.Show.Show (Data.PartialSemigroup.AppendRight a b) instance (GHC.Read.Read a, GHC.Read.Read b) => GHC.Read.Read (Data.PartialSemigroup.AppendRight a b) instance (GHC.Classes.Ord a, GHC.Classes.Ord b) => GHC.Classes.Ord (Data.PartialSemigroup.AppendRight a b) instance (GHC.Classes.Eq a, GHC.Classes.Eq b) => GHC.Classes.Eq (Data.PartialSemigroup.AppendRight a b) instance (GHC.Show.Show a, GHC.Show.Show b) => GHC.Show.Show (Data.PartialSemigroup.AppendLeft a b) instance (GHC.Read.Read a, GHC.Read.Read b) => GHC.Read.Read (Data.PartialSemigroup.AppendLeft a b) instance (GHC.Classes.Ord a, GHC.Classes.Ord b) => GHC.Classes.Ord (Data.PartialSemigroup.AppendLeft a b) instance (GHC.Classes.Eq a, GHC.Classes.Eq b) => GHC.Classes.Eq (Data.PartialSemigroup.AppendLeft a b) instance GHC.Show.Show a => GHC.Show.Show (Data.PartialSemigroup.Total a) instance GHC.Read.Read a => GHC.Read.Read (Data.PartialSemigroup.Total a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.PartialSemigroup.Total a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.PartialSemigroup.Total a) instance GHC.Show.Show a => GHC.Show.Show (Data.PartialSemigroup.Partial a) instance GHC.Read.Read a => GHC.Read.Read (Data.PartialSemigroup.Partial a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.PartialSemigroup.Partial a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.PartialSemigroup.Partial a) instance Data.PartialSemigroup.PartialSemigroup () instance Data.PartialSemigroup.PartialSemigroup a => Data.PartialSemigroup.PartialSemigroup (Data.Functor.Identity.Identity a) instance Data.PartialSemigroup.PartialSemigroup [a] instance GHC.Num.Num a => Data.PartialSemigroup.PartialSemigroup (Data.Monoid.Sum a) instance GHC.Num.Num a => Data.PartialSemigroup.PartialSemigroup (Data.Monoid.Product a) instance (Data.PartialSemigroup.PartialSemigroup a, Data.PartialSemigroup.PartialSemigroup b) => Data.PartialSemigroup.PartialSemigroup (Data.Either.Either a b) instance (Data.PartialSemigroup.PartialSemigroup a, Data.PartialSemigroup.PartialSemigroup b) => Data.PartialSemigroup.PartialSemigroup (a, b) instance (Data.PartialSemigroup.PartialSemigroup a, Data.PartialSemigroup.PartialSemigroup b, Data.PartialSemigroup.PartialSemigroup c) => Data.PartialSemigroup.PartialSemigroup (a, b, c) instance Data.PartialSemigroup.PartialSemigroup a => Data.PartialSemigroup.PartialSemigroup (Control.Applicative.ZipList a) instance Data.PartialSemigroup.PartialSemigroup a => Data.Semigroup.Semigroup (Data.PartialSemigroup.Partial a) instance GHC.Base.Monoid a => GHC.Base.Monoid (Data.PartialSemigroup.Partial a) instance Data.Semigroup.Semigroup a => Data.PartialSemigroup.PartialSemigroup (Data.PartialSemigroup.Total a) instance Data.PartialSemigroup.PartialSemigroup a => Data.PartialSemigroup.PartialSemigroup (Data.PartialSemigroup.AppendLeft a b) instance Data.PartialSemigroup.PartialSemigroup b => Data.PartialSemigroup.PartialSemigroup (Data.PartialSemigroup.AppendRight a b)