(original) (raw)

{-# LANGUAGE DeriveFoldable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE Trustworthy #-} {-# LANGUAGE TypeOperators #-}

module Data.Foldable ( Foldable(..),

[foldrM](Data.Foldable.html#foldrM),
[foldlM](Data.Foldable.html#foldlM),


[traverse_](Data.Foldable.html#traverse%5F),
[for_](Data.Foldable.html#for%5F),
[sequenceA_](Data.Foldable.html#sequenceA%5F),
[asum](Data.Foldable.html#asum),

[mapM_](Data.Foldable.html#mapM%5F),
[forM_](Data.Foldable.html#forM%5F),
[sequence_](Data.Foldable.html#sequence%5F),
[msum](Data.Foldable.html#msum),

[concat](Data.Foldable.html#concat),
[concatMap](Data.Foldable.html#concatMap),
[and](Data.Foldable.html#and),
[or](Data.Foldable.html#or),
[any](Data.Foldable.html#any),
[all](Data.Foldable.html#all),
[maximumBy](Data.Foldable.html#maximumBy),
[minimumBy](Data.Foldable.html#minimumBy),

[notElem](Data.Foldable.html#notElem),
[find](Data.Foldable.html#find)
) where

import Data.Bool import Data.Either import Data.Eq import Data.Functor.Utils (Max(..), Min(..), (#.)) import qualified GHC.List as List import Data.Maybe import Data.Monoid import Data.Ord import Data.Proxy

import GHC.Arr ( Array(..), elems, numElements, foldlElems, foldrElems, foldlElems', foldrElems', foldl1Elems, foldr1Elems) import GHC.Base hiding ( foldr ) import GHC.Generics import GHC.Num ( Num(..) )

infix 4 [elem](Data.Foldable.html#elem), [notElem](Data.Foldable.html#notElem)

class Foldable t where {-# MINIMAL foldMap | foldr #-}

[fold](Data.Foldable.html#fold) :: [Monoid](GHC.Base.html#Monoid) [m](#local-6989586621679231984) => [t](#local-6989586621679231983) [m](#local-6989586621679231984) -> [m](#local-6989586621679231984)
[fold](Data.Foldable.html#fold) = [foldMap](Data.Foldable.html#foldMap) [id](GHC.Base.html#id)


[foldMap](Data.Foldable.html#foldMap) :: [Monoid](GHC.Base.html#Monoid) [m](#local-6989586621679231985) => ([a](#local-6989586621679231986) -> [m](#local-6989586621679231985)) -> [t](#local-6989586621679231983) [a](#local-6989586621679231986) -> [m](#local-6989586621679231985)
{-# INLINE foldMap #-}

[foldMap](Data.Foldable.html#foldMap) [f](#local-6989586621679232005) = [foldr](Data.Foldable.html#foldr) ([mappend](GHC.Base.html#mappend) [.](GHC.Base.html#.) [f](#local-6989586621679232005)) [mempty](GHC.Base.html#mempty)


[foldr](Data.Foldable.html#foldr) :: ([a](#local-6989586621679231987) -> [b](#local-6989586621679231988) -> [b](#local-6989586621679231988)) -> [b](#local-6989586621679231988) -> [t](#local-6989586621679231983) [a](#local-6989586621679231987) -> [b](#local-6989586621679231988)
[foldr](Data.Foldable.html#foldr) [f](#local-6989586621679232006) [z](#local-6989586621679232007) [t](#local-6989586621679232008) = appEndo ([foldMap](Data.Foldable.html#foldMap) ([Endo](Data.Semigroup.Internal.html#Endo) [#.](Data.Functor.Utils.html#%23.) [f](#local-6989586621679232006)) [t](#local-6989586621679232008)) [z](#local-6989586621679232007)


[foldr'](Data.Foldable.html#foldr%27) :: ([a](#local-6989586621679231989) -> [b](#local-6989586621679231990) -> [b](#local-6989586621679231990)) -> [b](#local-6989586621679231990) -> [t](#local-6989586621679231983) [a](#local-6989586621679231989) -> [b](#local-6989586621679231990)
[foldr'](Data.Foldable.html#foldr%27) [f](#local-6989586621679232009) [z0](#local-6989586621679232010) [xs](#local-6989586621679232011) = [foldl](Data.Foldable.html#foldl) [f'](#local-6989586621679232012) [id](GHC.Base.html#id) [xs](#local-6989586621679232011) [z0](#local-6989586621679232010)
  where [f'](#local-6989586621679232012) [k](#local-6989586621679232013) [x](#local-6989586621679232014) [z](#local-6989586621679232015) = [k](#local-6989586621679232013) [$!](GHC.Base.html#%24%21) [f](#local-6989586621679232009) [x](#local-6989586621679232014) [z](#local-6989586621679232015)


[foldl](Data.Foldable.html#foldl) :: ([b](#local-6989586621679231991) -> [a](#local-6989586621679231992) -> [b](#local-6989586621679231991)) -> [b](#local-6989586621679231991) -> [t](#local-6989586621679231983) [a](#local-6989586621679231992) -> [b](#local-6989586621679231991)
[foldl](Data.Foldable.html#foldl) [f](#local-6989586621679232016) [z](#local-6989586621679232017) [t](#local-6989586621679232018) = appEndo (getDual ([foldMap](Data.Foldable.html#foldMap) ([Dual](Data.Semigroup.Internal.html#Dual) [.](GHC.Base.html#.) [Endo](Data.Semigroup.Internal.html#Endo) [.](GHC.Base.html#.) [flip](GHC.Base.html#flip) [f](#local-6989586621679232016)) [t](#local-6989586621679232018))) [z](#local-6989586621679232017)


[foldl'](Data.Foldable.html#foldl%27) :: ([b](#local-6989586621679231993) -> [a](#local-6989586621679231994) -> [b](#local-6989586621679231993)) -> [b](#local-6989586621679231993) -> [t](#local-6989586621679231983) [a](#local-6989586621679231994) -> [b](#local-6989586621679231993)
[foldl'](Data.Foldable.html#foldl%27) [f](#local-6989586621679232019) [z0](#local-6989586621679232020) [xs](#local-6989586621679232021) = [foldr](Data.Foldable.html#foldr) [f'](#local-6989586621679232022) [id](GHC.Base.html#id) [xs](#local-6989586621679232021) [z0](#local-6989586621679232020)
  where [f'](#local-6989586621679232022) [x](#local-6989586621679232023) [k](#local-6989586621679232024) [z](#local-6989586621679232025) = [k](#local-6989586621679232024) [$!](GHC.Base.html#%24%21) [f](#local-6989586621679232019) [z](#local-6989586621679232025) [x](#local-6989586621679232023)


[foldr1](Data.Foldable.html#foldr1) :: ([a](#local-6989586621679231995) -> [a](#local-6989586621679231995) -> [a](#local-6989586621679231995)) -> [t](#local-6989586621679231983) [a](#local-6989586621679231995) -> [a](#local-6989586621679231995)
[foldr1](Data.Foldable.html#foldr1) [f](#local-6989586621679232026) [xs](#local-6989586621679232027) = [fromMaybe](Data.Maybe.html#fromMaybe) ([errorWithoutStackTrace](GHC.Err.html#errorWithoutStackTrace) "foldr1: empty structure")
                ([foldr](Data.Foldable.html#foldr) [mf](#local-6989586621679232028) [Nothing](GHC.Maybe.html#Nothing) [xs](#local-6989586621679232027))
  where
    [mf](#local-6989586621679232028) [x](#local-6989586621679232029) [m](#local-6989586621679232030) = [Just](GHC.Maybe.html#Just) (case [m](#local-6989586621679232030) of
                     [Nothing](GHC.Maybe.html#Nothing) -> [x](#local-6989586621679232029)
                     [Just](GHC.Maybe.html#Just) [y](#local-6989586621679232031)  -> [f](#local-6989586621679232026) [x](#local-6989586621679232029) [y](#local-6989586621679232031))


[foldl1](Data.Foldable.html#foldl1) :: ([a](#local-6989586621679231996) -> [a](#local-6989586621679231996) -> [a](#local-6989586621679231996)) -> [t](#local-6989586621679231983) [a](#local-6989586621679231996) -> [a](#local-6989586621679231996)
[foldl1](Data.Foldable.html#foldl1) [f](#local-6989586621679232032) [xs](#local-6989586621679232033) = [fromMaybe](Data.Maybe.html#fromMaybe) ([errorWithoutStackTrace](GHC.Err.html#errorWithoutStackTrace) "foldl1: empty structure")
                ([foldl](Data.Foldable.html#foldl) [mf](#local-6989586621679232034) [Nothing](GHC.Maybe.html#Nothing) [xs](#local-6989586621679232033))
  where
    [mf](#local-6989586621679232034) [m](#local-6989586621679232035) [y](#local-6989586621679232036) = [Just](GHC.Maybe.html#Just) (case [m](#local-6989586621679232035) of
                     [Nothing](GHC.Maybe.html#Nothing) -> [y](#local-6989586621679232036)
                     [Just](GHC.Maybe.html#Just) [x](#local-6989586621679232037)  -> [f](#local-6989586621679232032) [x](#local-6989586621679232037) [y](#local-6989586621679232036))


[toList](Data.Foldable.html#toList) :: [t](#local-6989586621679231983) [a](#local-6989586621679231997) -> [[a](#local-6989586621679231997)]
{-# INLINE toList #-}
[toList](Data.Foldable.html#toList) [t](#local-6989586621679232038) = [build](GHC.Base.html#build) (\ [c](#local-6989586621679232039) [n](#local-6989586621679232040) -> [foldr](Data.Foldable.html#foldr) [c](#local-6989586621679232039) [n](#local-6989586621679232040) [t](#local-6989586621679232038))


[null](Data.Foldable.html#null) :: [t](#local-6989586621679231983) [a](#local-6989586621679231998) -> Bool
[null](Data.Foldable.html#null) = [foldr](Data.Foldable.html#foldr) (\_ _ -> False) True


[length](Data.Foldable.html#length) :: [t](#local-6989586621679231983) [a](#local-6989586621679231999) -> Int
[length](Data.Foldable.html#length) = [foldl'](Data.Foldable.html#foldl%27) (\[c](#local-6989586621679232041) _ -> [c](#local-6989586621679232041)[+](GHC.Num.html#%2B)1) 0


[elem](Data.Foldable.html#elem) :: Eq [a](#local-6989586621679232000) => [a](#local-6989586621679232000) -> [t](#local-6989586621679231983) [a](#local-6989586621679232000) -> Bool
[elem](Data.Foldable.html#elem) = [any](Data.Foldable.html#any) [.](GHC.Base.html#.) (==)


[maximum](Data.Foldable.html#maximum) :: forall [a](#local-6989586621679232001) . Ord [a](#local-6989586621679232001) => [t](#local-6989586621679231983) [a](#local-6989586621679232001) -> [a](#local-6989586621679232001)
[maximum](Data.Foldable.html#maximum) = [fromMaybe](Data.Maybe.html#fromMaybe) ([errorWithoutStackTrace](GHC.Err.html#errorWithoutStackTrace) "maximum: empty structure") [.](GHC.Base.html#.)
   getMax [.](GHC.Base.html#.) [foldMap](Data.Foldable.html#foldMap) ([Max](Data.Functor.Utils.html#Max) [#.](Data.Functor.Utils.html#%23.) ([Just](GHC.Maybe.html#Just) :: [a](#local-6989586621679232001) -> [Maybe](GHC.Maybe.html#Maybe) [a](#local-6989586621679232001)))


[minimum](Data.Foldable.html#minimum) :: forall [a](#local-6989586621679232002) . Ord [a](#local-6989586621679232002) => [t](#local-6989586621679231983) [a](#local-6989586621679232002) -> [a](#local-6989586621679232002)
[minimum](Data.Foldable.html#minimum) = [fromMaybe](Data.Maybe.html#fromMaybe) ([errorWithoutStackTrace](GHC.Err.html#errorWithoutStackTrace) "minimum: empty structure") [.](GHC.Base.html#.)
   getMin [.](GHC.Base.html#.) [foldMap](Data.Foldable.html#foldMap) ([Min](Data.Functor.Utils.html#Min) [#.](Data.Functor.Utils.html#%23.) ([Just](GHC.Maybe.html#Just) :: [a](#local-6989586621679232002) -> [Maybe](GHC.Maybe.html#Maybe) [a](#local-6989586621679232002)))


[sum](Data.Foldable.html#sum) :: [Num](GHC.Num.html#Num) [a](#local-6989586621679232003) => [t](#local-6989586621679231983) [a](#local-6989586621679232003) -> [a](#local-6989586621679232003)
[sum](Data.Foldable.html#sum) = getSum [#.](Data.Functor.Utils.html#%23.) [foldMap](Data.Foldable.html#foldMap) [Sum](Data.Semigroup.Internal.html#Sum)


[product](Data.Foldable.html#product) :: [Num](GHC.Num.html#Num) [a](#local-6989586621679232004) => [t](#local-6989586621679231983) [a](#local-6989586621679232004) -> [a](#local-6989586621679232004)
[product](Data.Foldable.html#product) = getProduct [#.](Data.Functor.Utils.html#%23.) [foldMap](Data.Foldable.html#foldMap) [Product](Data.Semigroup.Internal.html#Product)

instance Foldable Maybe where foldMap = maybe mempty

[foldr](Data.Foldable.html#foldr) _ [z](#local-6989586621679232104) [Nothing](GHC.Maybe.html#Nothing) = [z](#local-6989586621679232104)
foldr [f](#local-6989586621679232105) [z](#local-6989586621679232106) ([Just](GHC.Maybe.html#Just) [x](#local-6989586621679232107)) = [f](#local-6989586621679232105) [x](#local-6989586621679232107) [z](#local-6989586621679232106)

[foldl](Data.Foldable.html#foldl) _ [z](#local-6989586621679232108) [Nothing](GHC.Maybe.html#Nothing) = [z](#local-6989586621679232108)
foldl [f](#local-6989586621679232109) [z](#local-6989586621679232110) ([Just](GHC.Maybe.html#Just) [x](#local-6989586621679232111)) = [f](#local-6989586621679232109) [z](#local-6989586621679232110) [x](#local-6989586621679232111)

instance Foldable [] where elem = List.elem foldl = List.foldl foldl' = List.foldl' foldl1 = List.foldl1 foldr = List.foldr foldr1 = List.foldr1 length = List.length maximum = List.maximum minimum = List.minimum null = List.null product = List.product sum = List.sum toList = id

instance Foldable NonEmpty where foldr f z ~(a :| as) = f a (List.foldr f z as) foldl f z (a :| as) = List.foldl f (f z a) as foldl1 f (a :| as) = List.foldl f a as

foldr1 f (p :| ps) = foldr go id ps p where go x r prev = f prev (r x)

foldMap f ~(a :| as) = f a [mappend](GHC.Base.html#mappend) foldMap f as fold ~(m :| ms) = m [mappend](GHC.Base.html#mappend) fold ms toList ~(a :| as) = a : as

instance Foldable (Either a) where foldMap _ (Left _) = mempty foldMap f (Right y) = f y

[foldr](Data.Foldable.html#foldr) _ [z](#local-6989586621679232075) ([Left](Data.Either.html#Left) _) = [z](#local-6989586621679232075)
foldr [f](#local-6989586621679232076) [z](#local-6989586621679232077) ([Right](Data.Either.html#Right) [y](#local-6989586621679232078)) = [f](#local-6989586621679232076) [y](#local-6989586621679232078) [z](#local-6989586621679232077)

[length](Data.Foldable.html#length) ([Left](Data.Either.html#Left) _)  = 0
length ([Right](Data.Either.html#Right) _) = 1

[null](Data.Foldable.html#null)             = [isLeft](Data.Either.html#isLeft)

instance Foldable ((,) a) where foldMap f (_, y) = f y

[foldr](Data.Foldable.html#foldr) [f](#local-6989586621679232069) [z](#local-6989586621679232070) (_, [y](#local-6989586621679232071)) = [f](#local-6989586621679232069) [y](#local-6989586621679232071) [z](#local-6989586621679232070)

instance Foldable (Array i) where foldr = foldrElems foldl = foldlElems foldl' = foldlElems' foldr' = foldrElems' foldl1 = foldl1Elems foldr1 = foldr1Elems toList = elems length = numElements null a = numElements a == 0

instance Foldable Proxy where foldMap _ _ = mempty {-# INLINE foldMap #-} fold _ = mempty {-# INLINE fold #-} foldr _ z _ = z {-# INLINE foldr #-} foldl _ z _ = z {-# INLINE foldl #-} foldl1 _ _ = errorWithoutStackTrace "foldl1: Proxy" foldr1 _ _ = errorWithoutStackTrace "foldr1: Proxy" length _ = 0 null _ = True elem _ _ = False sum _ = 0 product _ = 1

instance Foldable Dual where foldMap = coerce

[elem](Data.Foldable.html#elem)               = ([.](GHC.Base.html#.) getDual) [#.](Data.Functor.Utils.html#%23.) (==)
[foldl](Data.Foldable.html#foldl)              = coerce
[foldl'](Data.Foldable.html#foldl%27)             = coerce
[foldl1](Data.Foldable.html#foldl1) _           = getDual
[foldr](Data.Foldable.html#foldr) [f](#local-6989586621679232058) [z](#local-6989586621679232059) ([Dual](Data.Semigroup.Internal.html#Dual) [x](#local-6989586621679232060)) = [f](#local-6989586621679232058) [x](#local-6989586621679232060) [z](#local-6989586621679232059)
[foldr'](Data.Foldable.html#foldr%27)             = [foldr](Data.Foldable.html#foldr)
[foldr1](Data.Foldable.html#foldr1) _           = getDual
[length](Data.Foldable.html#length) _           = 1
[maximum](Data.Foldable.html#maximum)            = getDual
[minimum](Data.Foldable.html#minimum)            = getDual
[null](Data.Foldable.html#null) _             = False
[product](Data.Foldable.html#product)            = getDual
[sum](Data.Foldable.html#sum)                = getDual
[toList](Data.Foldable.html#toList) ([Dual](Data.Semigroup.Internal.html#Dual) [x](#local-6989586621679232061))    = [[x](#local-6989586621679232061)]

instance Foldable Sum where foldMap = coerce

[elem](Data.Foldable.html#elem)               = ([.](GHC.Base.html#.) getSum) [#.](Data.Functor.Utils.html#%23.) (==)
[foldl](Data.Foldable.html#foldl)              = coerce
[foldl'](Data.Foldable.html#foldl%27)             = coerce
[foldl1](Data.Foldable.html#foldl1) _           = getSum
[foldr](Data.Foldable.html#foldr) [f](#local-6989586621679232054) [z](#local-6989586621679232055) ([Sum](Data.Semigroup.Internal.html#Sum) [x](#local-6989586621679232056))  = [f](#local-6989586621679232054) [x](#local-6989586621679232056) [z](#local-6989586621679232055)
[foldr'](Data.Foldable.html#foldr%27)             = [foldr](Data.Foldable.html#foldr)
[foldr1](Data.Foldable.html#foldr1) _           = getSum
[length](Data.Foldable.html#length) _           = 1
[maximum](Data.Foldable.html#maximum)            = getSum
[minimum](Data.Foldable.html#minimum)            = getSum
[null](Data.Foldable.html#null) _             = False
[product](Data.Foldable.html#product)            = getSum
[sum](Data.Foldable.html#sum)                = getSum
[toList](Data.Foldable.html#toList) ([Sum](Data.Semigroup.Internal.html#Sum) [x](#local-6989586621679232057))     = [[x](#local-6989586621679232057)]

instance Foldable Product where foldMap = coerce

[elem](Data.Foldable.html#elem)                  = ([.](GHC.Base.html#.) getProduct) [#.](Data.Functor.Utils.html#%23.) (==)
[foldl](Data.Foldable.html#foldl)                 = coerce
[foldl'](Data.Foldable.html#foldl%27)                = coerce
[foldl1](Data.Foldable.html#foldl1) _              = getProduct
[foldr](Data.Foldable.html#foldr) [f](#local-6989586621679232050) [z](#local-6989586621679232051) ([Product](Data.Semigroup.Internal.html#Product) [x](#local-6989586621679232052)) = [f](#local-6989586621679232050) [x](#local-6989586621679232052) [z](#local-6989586621679232051)
[foldr'](Data.Foldable.html#foldr%27)                = [foldr](Data.Foldable.html#foldr)
[foldr1](Data.Foldable.html#foldr1) _              = getProduct
[length](Data.Foldable.html#length) _              = 1
[maximum](Data.Foldable.html#maximum)               = getProduct
[minimum](Data.Foldable.html#minimum)               = getProduct
[null](Data.Foldable.html#null) _                = False
[product](Data.Foldable.html#product)               = getProduct
[sum](Data.Foldable.html#sum)                   = getProduct
[toList](Data.Foldable.html#toList) ([Product](Data.Semigroup.Internal.html#Product) [x](#local-6989586621679232053))    = [[x](#local-6989586621679232053)]

instance Foldable First where foldMap f = foldMap f . getFirst

instance Foldable Last where foldMap f = foldMap f . getLast

instance (Foldable f) => Foldable (Alt f) where foldMap f = foldMap f . getAlt

instance (Foldable f) => Foldable (Ap f) where foldMap f = foldMap f . getAp

instance Foldable U1 where foldMap _ _ = mempty {-# INLINE foldMap #-} fold _ = mempty {-# INLINE fold #-} foldr _ z _ = z {-# INLINE foldr #-} foldl _ z _ = z {-# INLINE foldl #-} foldl1 _ _ = errorWithoutStackTrace "foldl1: U1" foldr1 _ _ = errorWithoutStackTrace "foldr1: U1" length _ = 0 null _ = True elem _ _ = False sum _ = 0 product _ = 1

deriving instance Foldable V1

deriving instance Foldable Par1

deriving instance Foldable f => Foldable (Rec1 f)

deriving instance Foldable (K1 i c)

deriving instance Foldable f => Foldable (M1 i c f)

deriving instance (Foldable f, Foldable g) => Foldable (f :+: g)

deriving instance (Foldable f, Foldable g) => Foldable (f :*: g)

deriving instance (Foldable f, Foldable g) => Foldable (f :.: g)

deriving instance Foldable UAddr

deriving instance Foldable UChar

deriving instance Foldable UDouble

deriving instance Foldable UFloat

deriving instance Foldable UInt

deriving instance Foldable UWord

deriving instance Foldable Down

foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b foldrM f z0 xs = foldl f' return xs z0 where f' k x z = f x z >>= k

foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b foldlM f z0 xs = foldr f' return xs z0 where f' x k z = f z x >>= k

traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f () traverse_ f = foldr ((*>) . f) (pure ())

for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () {-# INLINE for_ #-} for_ = flip traverse_

mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () mapM_ f= foldr ((>>) . f) (return ())

forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m () {-# INLINE forM_ #-} forM_ = flip mapM_

sequenceA_ :: (Foldable t, Applicative f) => t (f a) -> f () sequenceA_ = foldr (*>) (pure ())

sequence_ :: (Foldable t, Monad m) => t (m a) -> m () sequence_ = foldr (>>) (return ())

asum :: (Foldable t, Alternative f) => t (f a) -> f a {-# INLINE asum #-} asum = foldr (<|>) empty

msum :: (Foldable t, MonadPlus m) => t (m a) -> m a {-# INLINE msum #-} msum = asum

concat :: Foldable t => t [a] -> [a] concat xs = build ([c](#local-6989586621679232184) n -> foldr ([x](#local-6989586621679232186) y -> foldr c y x) n xs) {-# INLINE concat #-}

concatMap :: Foldable t => (a -> [b]) -> t a -> [b] concatMap f xs = build ([c](#local-6989586621679232190) n -> foldr ([x](#local-6989586621679232192) b -> foldr c b (f x)) n xs) {-# INLINE concatMap #-}

and :: Foldable t => t Bool -> Bool and = getAll #. foldMap All

or :: Foldable t => t Bool -> Bool or = getAny #. foldMap Any

any :: Foldable t => (a -> Bool) -> t a -> Bool any p = getAny #. foldMap (Any #. p)

all :: Foldable t => (a -> Bool) -> t a -> Bool all p = getAll #. foldMap (All #. p)

maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a maximumBy cmp = foldl1 max' where max' x y = case cmp x y of GT -> x _ -> y

minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a minimumBy cmp = foldl1 min' where min' x y = case cmp x y of GT -> y _ -> x

notElem :: (Foldable t, Eq a) => a -> t a -> Bool notElem x = not . elem x

find :: Foldable t => (a -> Bool) -> t a -> Maybe a find p = getFirst . foldMap (\ x -> First (if p x then Just x else Nothing))