{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Safe #-}
module Data.Semigroup.Foldable
(
Foldable1(fold1, foldMap1, toNonEmpty)
, intercalate1
, foldrM1
, foldlM1
, intercalateMap1
, traverse1_
, for1_
, sequenceA1_
, foldMapDefault1
, asum1
, gfold1
, gfoldMap1
, gtoNonEmpty
) where
import Data.Foldable
import Data.Foldable1
import Data.Functor.Alt (Alt(..))
import Data.Functor.Apply
import Data.List.NonEmpty (NonEmpty(..))
import Data.Traversable.Instances ()
import Data.Semigroup hiding (Product, Sum)
import GHC.Generics
import Prelude hiding (foldr)
newtype JoinWith a = JoinWith {forall a. JoinWith a -> a -> a
joinee :: (a -> a)}
instance Semigroup a => Semigroup (JoinWith a) where
JoinWith a -> a
a <> :: JoinWith a -> JoinWith a -> JoinWith a
<> JoinWith a -> a
b = forall a. (a -> a) -> JoinWith a
JoinWith forall a b. (a -> b) -> a -> b
$ \a
j -> a -> a
a a
j forall a. Semigroup a => a -> a -> a
<> a
j forall a. Semigroup a => a -> a -> a
<> a -> a
b a
j
intercalateMap1 :: (Foldable1 t, Semigroup m) => m -> (a -> m) -> t a -> m
intercalateMap1 :: forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
m -> (a -> m) -> t a -> m
intercalateMap1 m
j a -> m
f = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. JoinWith a -> a -> a
joinee m
j forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 (forall a. (a -> a) -> JoinWith a
JoinWith forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)
{-# INLINE intercalateMap1 #-}
newtype Act f a = Act { forall (f :: * -> *) a. Act f a -> f a
getAct :: f a }
instance Apply f => Semigroup (Act f a) where
Act f a
a <> :: Act f a -> Act f a -> Act f a
<> Act f a
b = forall (f :: * -> *) a. f a -> Act f a
Act (f a
a forall (f :: * -> *) a b. Apply f => f a -> f b -> f b
.> f a
b)
instance Functor f => Functor (Act f) where
fmap :: forall a b. (a -> b) -> Act f a -> Act f b
fmap a -> b
f (Act f a
a) = forall (f :: * -> *) a. f a -> Act f a
Act (a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a)
a
b <$ :: forall a b. a -> Act f b -> Act f a
<$ Act f b
a = forall (f :: * -> *) a. f a -> Act f a
Act (a
b forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f b
a)
traverse1_ :: (Foldable1 t, Apply f) => (a -> f b) -> t a -> f ()
traverse1_ :: forall (t :: * -> *) (f :: * -> *) a b.
(Foldable1 t, Apply f) =>
(a -> f b) -> t a -> f ()
traverse1_ a -> f b
f t a
t = () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (f :: * -> *) a. Act f a -> f a
getAct (forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 (forall (f :: * -> *) a. f a -> Act f a
Act forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f b
f) t a
t)
{-# INLINE traverse1_ #-}
for1_ :: (Foldable1 t, Apply f) => t a -> (a -> f b) -> f ()
for1_ :: forall (t :: * -> *) (f :: * -> *) a b.
(Foldable1 t, Apply f) =>
t a -> (a -> f b) -> f ()
for1_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) (f :: * -> *) a b.
(Foldable1 t, Apply f) =>
(a -> f b) -> t a -> f ()
traverse1_
{-# INLINE for1_ #-}
sequenceA1_ :: (Foldable1 t, Apply f) => t (f a) -> f ()
sequenceA1_ :: forall (t :: * -> *) (f :: * -> *) a.
(Foldable1 t, Apply f) =>
t (f a) -> f ()
sequenceA1_ t (f a)
t = () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (f :: * -> *) a. Act f a -> f a
getAct (forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 forall (f :: * -> *) a. f a -> Act f a
Act t (f a)
t)
{-# INLINE sequenceA1_ #-}
foldMapDefault1 :: (Foldable1 t, Monoid m) => (a -> m) -> t a -> m
foldMapDefault1 :: forall (t :: * -> *) m a.
(Foldable1 t, Monoid m) =>
(a -> m) -> t a -> m
foldMapDefault1 a -> m
f = forall m. WrappedMonoid m -> m
unwrapMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall m. m -> WrappedMonoid m
WrapMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)
{-# INLINE foldMapDefault1 #-}
newtype Alt_ f a = Alt_ { forall (f :: * -> *) a. Alt_ f a -> f a
getAlt_ :: f a }
instance Alt f => Semigroup (Alt_ f a) where
Alt_ f a
a <> :: Alt_ f a -> Alt_ f a -> Alt_ f a
<> Alt_ f a
b = forall (f :: * -> *) a. f a -> Alt_ f a
Alt_ (f a
a forall (f :: * -> *) a. Alt f => f a -> f a -> f a
<!> f a
b)
asum1 :: (Foldable1 t, Alt m) => t (m a) -> m a
asum1 :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Alt m) =>
t (m a) -> m a
asum1 = forall (f :: * -> *) a. Alt_ f a -> f a
getAlt_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 forall (f :: * -> *) a. f a -> Alt_ f a
Alt_
{-# INLINE asum1 #-}
gfold1 :: (Foldable1 (Rep1 t), Generic1 t, Semigroup m) => t m -> m
gfold1 :: forall (t :: * -> *) m.
(Foldable1 (Rep1 t), Generic1 t, Semigroup m) =>
t m -> m
gfold1 = forall (t :: * -> *) m. (Foldable1 t, Semigroup m) => t m -> m
fold1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1
gfoldMap1 :: (Foldable1 (Rep1 t), Generic1 t, Semigroup m) => (a -> m) -> t a -> m
gfoldMap1 :: forall (t :: * -> *) m a.
(Foldable1 (Rep1 t), Generic1 t, Semigroup m) =>
(a -> m) -> t a -> m
gfoldMap1 a -> m
f = forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1
gtoNonEmpty :: (Foldable1 (Rep1 t), Generic1 t) => t a -> NonEmpty a
gtoNonEmpty :: forall (t :: * -> *) a.
(Foldable1 (Rep1 t), Generic1 t) =>
t a -> NonEmpty a
gtoNonEmpty = forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1