{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeOperators #-}
module Data.Foldable1 (
Foldable1(..),
foldr1, foldr1',
foldl1, foldl1',
intercalate1,
foldrM1,
foldlM1,
foldrMapM1,
foldlMapM1,
maximumBy,
minimumBy,
) where
import Data.Foldable (Foldable, foldlM, foldr)
import Data.List (foldl, foldl')
import Data.List.NonEmpty (NonEmpty (..))
import Data.Semigroup
(Dual (..), First (..), Last (..), Max (..), Min (..), Product (..),
Semigroup (..), Sum (..))
import Prelude
(Maybe (..), Monad (..), Ord, Ordering (..), id, seq, ($!), ($), (.),
(=<<), flip, const, error)
import qualified Data.List.NonEmpty as NE
import Data.Complex (Complex (..))
import GHC.Generics
(M1 (..), Par1 (..), Rec1 (..), V1, (:*:) (..), (:+:) (..), (:.:) (..))
import Data.Ord (Down (..))
import qualified Data.Monoid as Mon
import Data.Functor.Compose (Compose (..))
import Data.Functor.Identity (Identity (..))
import qualified Data.Functor.Product as Functor
import qualified Data.Functor.Sum as Functor
import Data.Coerce (Coercible, coerce)
class Foldable t => Foldable1 t where
{-# MINIMAL foldMap1 | foldrMap1 #-}
fold1 :: Semigroup m => t m -> m
fold1 = (m -> m) -> t m -> m
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 m -> m
forall a. a -> a
id
foldMap1 :: Semigroup m => (a -> m) -> t a -> m
foldMap1 a -> m
f = (a -> m) -> (a -> m -> m) -> t a -> m
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> m
f (\a
a m
m -> a -> m
f a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
m)
foldMap1' :: Semigroup m => (a -> m) -> t a -> m
foldMap1' a -> m
f = (a -> m) -> (m -> a -> m) -> t a -> m
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1' a -> m
f (\m
m a
a -> m
m m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
a)
toNonEmpty :: t a -> NonEmpty a
toNonEmpty = NonEmptyDList a -> NonEmpty a
forall a. NonEmptyDList a -> NonEmpty a
runNonEmptyDList (NonEmptyDList a -> NonEmpty a)
-> (t a -> NonEmptyDList a) -> t a -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> NonEmptyDList a) -> t a -> NonEmptyDList a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> NonEmptyDList a
forall a. a -> NonEmptyDList a
singleton
maximum :: Ord a => t a -> a
maximum = Max a -> a
forall a. Max a -> a
getMax (Max a -> a) -> (t a -> Max a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Max a) -> t a -> Max a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1' a -> Max a
forall a. a -> Max a
Max
minimum :: Ord a => t a -> a
minimum = Min a -> a
forall a. Min a -> a
getMin (Min a -> a) -> (t a -> Min a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Min a) -> t a -> Min a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1' a -> Min a
forall a. a -> Min a
Min
head :: t a -> a
head = First a -> a
forall a. First a -> a
getFirst (First a -> a) -> (t a -> First a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> First a) -> t a -> First a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> First a
forall a. a -> First a
First
last :: t a -> a
last = Last a -> a
forall a. Last a -> a
getLast (Last a -> a) -> (t a -> Last a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Last a) -> t a -> Last a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> Last a
forall a. a -> Last a
Last
foldrMap1 :: (a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
f a -> b -> b
g t a
xs =
FromMaybe b -> Maybe b -> b
forall b. FromMaybe b -> Maybe b -> b
appFromMaybe ((a -> FromMaybe b) -> t a -> FromMaybe b
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe ((Maybe b -> b) -> FromMaybe b)
-> (a -> Maybe b -> b) -> a -> FromMaybe b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Maybe b -> b
h) t a
xs) Maybe b
forall a. Maybe a
Nothing
where
h :: a -> Maybe b -> b
h a
a Maybe b
Nothing = a -> b
f a
a
h a
a (Just b
b) = a -> b -> b
g a
a b
b
foldlMap1' :: (a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1' a -> b
f b -> a -> b
g t a
xs =
(a -> SMaybe b -> b)
-> (a -> (SMaybe b -> b) -> SMaybe b -> b) -> t a -> SMaybe b -> b
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> SMaybe b -> b
f' a -> (SMaybe b -> b) -> SMaybe b -> b
forall {b}. a -> (SMaybe b -> b) -> SMaybe b -> b
g' t a
xs SMaybe b
forall a. SMaybe a
SNothing
where
f' :: a -> SMaybe b -> b
f' a
a SMaybe b
SNothing = a -> b
f a
a
f' a
a (SJust b
b) = b -> a -> b
g b
b a
a
g' :: a -> (SMaybe b -> b) -> SMaybe b -> b
g' a
a SMaybe b -> b
x SMaybe b
SNothing = SMaybe b -> b
x (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b
f a
a)
g' a
a SMaybe b -> b
x (SJust b
b) = SMaybe b -> b
x (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (b -> a -> b
g b
b a
a)
foldlMap1 :: (a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1 a -> b
f b -> a -> b
g t a
xs =
FromMaybe b -> Maybe b -> b
forall b. FromMaybe b -> Maybe b -> b
appFromMaybe (Dual (FromMaybe b) -> FromMaybe b
forall a. Dual a -> a
getDual ((a -> Dual (FromMaybe b)) -> t a -> Dual (FromMaybe b)
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((FromMaybe b -> Dual (FromMaybe b)
forall a. a -> Dual a
Dual (FromMaybe b -> Dual (FromMaybe b))
-> ((Maybe b -> b) -> FromMaybe b)
-> (Maybe b -> b)
-> Dual (FromMaybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe) ((Maybe b -> b) -> Dual (FromMaybe b))
-> (a -> Maybe b -> b) -> a -> Dual (FromMaybe b)
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Maybe b -> b
h) t a
xs)) Maybe b
forall a. Maybe a
Nothing
where
h :: a -> Maybe b -> b
h a
a Maybe b
Nothing = a -> b
f a
a
h a
a (Just b
b) = b -> a -> b
g b
b a
a
foldrMap1' :: (a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1' a -> b
f a -> b -> b
g t a
xs =
(a -> SMaybe b -> b)
-> ((SMaybe b -> b) -> a -> SMaybe b -> b) -> t a -> SMaybe b -> b
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1 a -> SMaybe b -> b
f' (SMaybe b -> b) -> a -> SMaybe b -> b
forall {b}. (SMaybe b -> b) -> a -> SMaybe b -> b
g' t a
xs SMaybe b
forall a. SMaybe a
SNothing
where
f' :: a -> SMaybe b -> b
f' a
a SMaybe b
SNothing = a -> b
f a
a
f' a
a (SJust b
b) = a -> b -> b
g a
a b
b
g' :: (SMaybe b -> b) -> a -> SMaybe b -> b
g' SMaybe b -> b
bb a
a SMaybe b
SNothing = SMaybe b -> b
bb (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b
f a
a)
g' SMaybe b -> b
bb a
a (SJust b
b) = SMaybe b -> b
bb (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b -> b
g a
a b
b)
foldr1 :: Foldable1 t => (a -> a -> a) -> t a -> a
foldr1 :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldr1 = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> a
forall a. a -> a
id
{-# INLINE foldr1 #-}
foldr1' :: Foldable1 t => (a -> a -> a) -> t a -> a
foldr1' :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldr1' = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1' a -> a
forall a. a -> a
id
{-# INLINE foldr1' #-}
foldl1 :: Foldable1 t => (a -> a -> a) -> t a -> a
foldl1 :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1 = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1 a -> a
forall a. a -> a
id
{-# INLINE foldl1 #-}
foldl1' :: Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1' a -> a
forall a. a -> a
id
{-# INLINE foldl1' #-}
intercalate1 :: (Foldable1 t, Semigroup m) => m -> t m -> m
intercalate1 :: forall (t :: * -> *) m. (Foldable1 t, Semigroup m) => m -> t m -> m
intercalate1 = (m -> (m -> m) -> t m -> m) -> (m -> m) -> m -> t m -> m
forall a b c. (a -> b -> c) -> b -> a -> c
flip m -> (m -> m) -> t m -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
m -> (a -> m) -> t a -> m
intercalateMap1 m -> m
forall a. a -> a
id
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 = (JoinWith m -> m -> m) -> m -> JoinWith m -> m
forall a b c. (a -> b -> c) -> b -> a -> c
flip JoinWith m -> m -> m
forall a. JoinWith a -> a -> a
joinee m
j (JoinWith m -> m) -> (t a -> JoinWith m) -> t a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> JoinWith m) -> t a -> JoinWith m
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((m -> m) -> JoinWith m
forall a. (a -> a) -> JoinWith a
JoinWith ((m -> m) -> JoinWith m) -> (a -> m -> m) -> a -> JoinWith m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m -> m -> m
forall a b. a -> b -> a
const (m -> m -> m) -> (a -> m) -> a -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)
foldrM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a
foldrM1 :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Monad m) =>
(a -> a -> m a) -> t a -> m a
foldrM1 = (a -> m a) -> (a -> a -> m a) -> t a -> m a
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
foldrMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 :: forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 a -> m b
g a -> b -> m b
f = NonEmpty a -> m b
go (NonEmpty a -> m b) -> (t a -> NonEmpty a) -> t a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> NonEmpty a
forall a. t a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty
where
go :: NonEmpty a -> m b
go (a
e:|[a]
es) =
case [a]
es of
[] -> a -> m b
g a
e
a
x:[a]
xs -> a -> b -> m b
f a
e (b -> m b) -> m b -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NonEmpty a -> m b
go (a
xa -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
xs)
foldlM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a
foldlM1 :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Monad m) =>
(a -> a -> m a) -> t a -> m a
foldlM1 = (a -> m a) -> (a -> a -> m a) -> t a -> m a
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
foldlMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 :: forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 a -> m b
g b -> a -> m b
f t a
t = a -> m b
g a
x m b -> (b -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
y -> (b -> a -> m b) -> b -> [a] -> m b
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM b -> a -> m b
f b
y [a]
xs
where a
x:|[a]
xs = t a -> NonEmpty a
forall a. t a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty t a
t
maximumBy :: Foldable1 t => (a -> a -> Ordering) -> t a -> a
maximumBy :: forall (t :: * -> *) a.
Foldable1 t =>
(a -> a -> Ordering) -> t a -> a
maximumBy a -> a -> Ordering
cmp = (a -> a -> a) -> t a -> a
forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' a -> a -> a
max'
where max' :: a -> a -> a
max' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
Ordering
GT -> a
x
Ordering
_ -> a
y
minimumBy :: Foldable1 t => (a -> a -> Ordering) -> t a -> a
minimumBy :: forall (t :: * -> *) a.
Foldable1 t =>
(a -> a -> Ordering) -> t a -> a
minimumBy a -> a -> Ordering
cmp = (a -> a -> a) -> t a -> a
forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' a -> a -> a
min'
where min' :: a -> a -> a
min' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
Ordering
GT -> a
y
Ordering
_ -> a
x
newtype NonEmptyDList a = NEDL { forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL :: [a] -> NonEmpty a }
instance Semigroup (NonEmptyDList a) where
NonEmptyDList a
xs <> :: NonEmptyDList a -> NonEmptyDList a -> NonEmptyDList a
<> NonEmptyDList a
ys = ([a] -> NonEmpty a) -> NonEmptyDList a
forall a. ([a] -> NonEmpty a) -> NonEmptyDList a
NEDL (NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL NonEmptyDList a
xs ([a] -> NonEmpty a) -> ([a] -> [a]) -> [a] -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty a -> [a]) -> ([a] -> NonEmpty a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL NonEmptyDList a
ys)
{-# INLINE (<>) #-}
singleton :: a -> NonEmptyDList a
singleton :: forall a. a -> NonEmptyDList a
singleton = ([a] -> NonEmpty a) -> NonEmptyDList a
forall a. ([a] -> NonEmpty a) -> NonEmptyDList a
NEDL (([a] -> NonEmpty a) -> NonEmptyDList a)
-> (a -> [a] -> NonEmpty a) -> a -> NonEmptyDList a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|)
runNonEmptyDList :: NonEmptyDList a -> NonEmpty a
runNonEmptyDList :: forall a. NonEmptyDList a -> NonEmpty a
runNonEmptyDList = (([a] -> NonEmpty a) -> [a] -> NonEmpty a
forall a b. (a -> b) -> a -> b
$ []) (([a] -> NonEmpty a) -> NonEmpty a)
-> (NonEmptyDList a -> [a] -> NonEmpty a)
-> NonEmptyDList a
-> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL
{-# INLINE runNonEmptyDList #-}
newtype FromMaybe b = FromMaybe { forall b. FromMaybe b -> Maybe b -> b
appFromMaybe :: Maybe b -> b }
instance Semigroup (FromMaybe b) where
FromMaybe Maybe b -> b
f <> :: FromMaybe b -> FromMaybe b -> FromMaybe b
<> FromMaybe Maybe b -> b
g = (Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe (Maybe b -> b
f (Maybe b -> b) -> (Maybe b -> Maybe b) -> Maybe b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> (Maybe b -> b) -> Maybe b -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe b -> b
g)
data SMaybe a = SNothing | SJust !a
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 = (a -> a) -> JoinWith a
forall a. (a -> a) -> JoinWith a
JoinWith ((a -> a) -> JoinWith a) -> (a -> a) -> JoinWith a
forall a b. (a -> b) -> a -> b
$ \a
j -> a -> a
a a
j a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
j a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a -> a
b a
j
instance Foldable1 NonEmpty where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> NonEmpty a -> m
foldMap1 a -> m
f (a
x :| [a]
xs) = m -> [a] -> m
go (a -> m
f a
x) [a]
xs where
go :: m -> [a] -> m
go m
y [] = m
y
go m
y (a
z : [a]
zs) = m
y m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m -> [a] -> m
go (a -> m
f a
z) [a]
zs
foldMap1' :: forall m a. Semigroup m => (a -> m) -> NonEmpty a -> m
foldMap1' a -> m
f (a
x :| [a]
xs) = (m -> a -> m) -> m -> [a] -> m
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\m
m a
y -> m
m m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
y) (a -> m
f a
x) [a]
xs
toNonEmpty :: forall a. NonEmpty a -> NonEmpty a
toNonEmpty = NonEmpty a -> NonEmpty a
forall a. a -> a
id
foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> NonEmpty a -> b
foldrMap1 a -> b
g a -> b -> b
f (a
x :| [a]
xs) = a -> [a] -> b
go a
x [a]
xs where
go :: a -> [a] -> b
go a
y [] = a -> b
g a
y
go a
y (a
z : [a]
zs) = a -> b -> b
f a
y (a -> [a] -> b
go a
z [a]
zs)
foldlMap1 :: forall a b. (a -> b) -> (b -> a -> b) -> NonEmpty a -> b
foldlMap1 a -> b
g b -> a -> b
f (a
x :| [a]
xs) = (b -> a -> b) -> b -> [a] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl b -> a -> b
f (a -> b
g a
x) [a]
xs
foldlMap1' :: forall a b. (a -> b) -> (b -> a -> b) -> NonEmpty a -> b
foldlMap1' a -> b
g b -> a -> b
f (a
x :| [a]
xs) = let gx :: b
gx = a -> b
g a
x in b
gx b -> b -> b
forall a b. a -> b -> b
`seq` (b -> a -> b) -> b -> [a] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' b -> a -> b
f b
gx [a]
xs
head :: forall a. NonEmpty a -> a
head = NonEmpty a -> a
forall a. NonEmpty a -> a
NE.head
last :: forall a. NonEmpty a -> a
last = NonEmpty a -> a
forall a. NonEmpty a -> a
NE.last
instance Foldable1 Down where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Down a -> m
foldMap1 = (a -> m) -> Down a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Complex where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Complex a -> m
foldMap1 a -> m
f (a
x :+ a
y) = a -> m
f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
y
toNonEmpty :: forall a. Complex a -> NonEmpty a
toNonEmpty (a
x :+ a
y) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: []
instance Foldable1 ((,) a) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> (a, a) -> m
foldMap1 a -> m
f (a
_, a
y) = a -> m
f a
y
toNonEmpty :: forall a. (a, a) -> NonEmpty a
toNonEmpty (a
_, a
x) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
minimum :: forall a. Ord a => (a, a) -> a
minimum (a
_, a
x) = a
x
maximum :: forall a. Ord a => (a, a) -> a
maximum (a
_, a
x) = a
x
head :: forall a. (a, a) -> a
head (a
_, a
x) = a
x
last :: forall a. (a, a) -> a
last (a
_, a
x) = a
x
instance Foldable1 Dual where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Dual a -> m
foldMap1 = (a -> m) -> Dual a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Sum where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Sum a -> m
foldMap1 = (a -> m) -> Sum a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Product where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Product a -> m
foldMap1 = (a -> m) -> Product a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Min where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Min a -> m
foldMap1 = (a -> m) -> Min a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Max where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Max a -> m
foldMap1 = (a -> m) -> Max a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 First where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> First a -> m
foldMap1 = (a -> m) -> First a -> m
forall a b. Coercible a b => a -> b
coerce
instance Foldable1 Last where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Last a -> m
foldMap1 = (a -> m) -> Last a -> m
forall a b. Coercible a b => a -> b
coerce
deriving instance (Foldable1 f) => Foldable1 (Mon.Alt f)
deriving instance (Foldable1 f) => Foldable1 (Mon.Ap f)
instance Foldable1 V1 where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> V1 a -> m
foldMap1 a -> m
_ V1 a
x = V1 a
x V1 a -> m -> m
forall a b. a -> b -> b
`seq` [Char] -> m
forall a. HasCallStack => [Char] -> a
error [Char]
"foldMap1 @V1"
instance Foldable1 Par1 where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Par1 a -> m
foldMap1 = (a -> m) -> Par1 a -> m
forall a b. Coercible a b => a -> b
coerce
deriving instance Foldable1 f => Foldable1 (Rec1 f)
deriving instance Foldable1 f => Foldable1 (M1 i c f)
instance (Foldable1 f, Foldable1 g) => Foldable1 (f :+: g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:+:) f g a -> m
foldMap1 a -> m
f (L1 f a
x) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x
foldMap1 a -> m
f (R1 g a
y) = (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y
instance (Foldable1 f, Foldable1 g) => Foldable1 (f :*: g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:*:) f g a -> m
foldMap1 a -> m
f (f a
x :*: g a
y) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y
instance (Foldable1 f, Foldable1 g) => Foldable1 (f :.: g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:.:) f g a -> m
foldMap1 a -> m
f = (g a -> m) -> f (g a) -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f) (f (g a) -> m) -> ((:.:) f g a -> f (g a)) -> (:.:) f g a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:.:) f g a -> f (g a)
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (p :: k1).
(:.:) f g p -> f (g p)
unComp1
instance Foldable1 Identity where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Identity a -> m
foldMap1 = (a -> m) -> Identity a -> m
forall a b. Coercible a b => a -> b
coerce
foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Identity a -> b
foldrMap1 a -> b
g a -> b -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
foldrMap1' :: forall a b. (a -> b) -> (a -> b -> b) -> Identity a -> b
foldrMap1' a -> b
g a -> b -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
foldlMap1 :: forall a b. (a -> b) -> (b -> a -> b) -> Identity a -> b
foldlMap1 a -> b
g b -> a -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
foldlMap1' :: forall a b. (a -> b) -> (b -> a -> b) -> Identity a -> b
foldlMap1' a -> b
g b -> a -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
toNonEmpty :: forall a. Identity a -> NonEmpty a
toNonEmpty (Identity a
x) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
last :: forall a. Identity a -> a
last = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
head :: forall a. Identity a -> a
head = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
minimum :: forall a. Ord a => Identity a -> a
minimum = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
maximum :: forall a. Ord a => Identity a -> a
maximum = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
instance (Foldable1 f, Foldable1 g) => Foldable1 (Functor.Product f g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Product f g a -> m
foldMap1 a -> m
f (Functor.Pair f a
x g a
y) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y
foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Product f g a -> b
foldrMap1 a -> b
g a -> b -> b
f (Functor.Pair f a
x g a
y) = (a -> b -> b) -> b -> f a -> b
forall a b. (a -> b -> b) -> b -> f a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f ((a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f g a
y) f a
x
head :: forall a. Product f g a -> a
head (Functor.Pair f a
x g a
_) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head f a
x
last :: forall a. Product f g a -> a
last (Functor.Pair f a
_ g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last g a
y
instance (Foldable1 f, Foldable1 g) => Foldable1 (Functor.Sum f g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Sum f g a -> m
foldMap1 a -> m
f (Functor.InL f a
x) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x
foldMap1 a -> m
f (Functor.InR g a
y) = (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y
foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Sum f g a -> b
foldrMap1 a -> b
g a -> b -> b
f (Functor.InL f a
x) = (a -> b) -> (a -> b -> b) -> f a -> b
forall a b. (a -> b) -> (a -> b -> b) -> f a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f f a
x
foldrMap1 a -> b
g a -> b -> b
f (Functor.InR g a
y) = (a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f g a
y
toNonEmpty :: forall a. Sum f g a -> NonEmpty a
toNonEmpty (Functor.InL f a
x) = f a -> NonEmpty a
forall a. f a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty f a
x
toNonEmpty (Functor.InR g a
y) = g a -> NonEmpty a
forall a. g a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty g a
y
head :: forall a. Sum f g a -> a
head (Functor.InL f a
x) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head f a
x
head (Functor.InR g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head g a
y
last :: forall a. Sum f g a -> a
last (Functor.InL f a
x) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last f a
x
last (Functor.InR g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last g a
y
minimum :: forall a. Ord a => Sum f g a -> a
minimum (Functor.InL f a
x) = f a -> a
forall a. Ord a => f a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
minimum f a
x
minimum (Functor.InR g a
y) = g a -> a
forall a. Ord a => g a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
minimum g a
y
maximum :: forall a. Ord a => Sum f g a -> a
maximum (Functor.InL f a
x) = f a -> a
forall a. Ord a => f a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
maximum f a
x
maximum (Functor.InR g a
y) = g a -> a
forall a. Ord a => g a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
maximum g a
y
instance (Foldable1 f, Foldable1 g) => Foldable1 (Compose f g) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Compose f g a -> m
foldMap1 a -> m
f = (g a -> m) -> f (g a) -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f) (f (g a) -> m) -> (Compose f g a -> f (g a)) -> Compose f g a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Compose f g a -> b
foldrMap1 a -> b
f a -> b -> b
g = (g a -> b) -> (g a -> b -> b) -> f (g a) -> b
forall a b. (a -> b) -> (a -> b -> b) -> f a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 ((a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
f a -> b -> b
g) (\g a
xs b
x -> (a -> b -> b) -> b -> g a -> b
forall a b. (a -> b -> b) -> b -> g a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
g b
x g a
xs) (f (g a) -> b) -> (Compose f g a -> f (g a)) -> Compose f g a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
head :: forall a. Compose f g a -> a
head = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head (g a -> a) -> (Compose f g a -> g a) -> Compose f g a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (g a) -> g a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head (f (g a) -> g a)
-> (Compose f g a -> f (g a)) -> Compose f g a -> g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
last :: forall a. Compose f g a -> a
last = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last (g a -> a) -> (Compose f g a -> g a) -> Compose f g a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (g a) -> g a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last (f (g a) -> g a)
-> (Compose f g a -> f (g a)) -> Compose f g a -> g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
(#.) :: Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. :: forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
(#.) b -> c
_f = (a -> b) -> a -> c
forall a b. Coercible a b => a -> b
coerce