module Data.Foldable.Extra
( module Data.Foldable
, notNull
, sum'
, product'
, sumOn'
, productOn'
, anyM
, allM
, orM
, andM
, findM
, firstJustM
) where
import Data.Foldable
import qualified Control.Monad.Extra as MX
notNull :: Foldable f => f a -> Bool
notNull :: forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null
sum' :: (Foldable f, Num a) => f a -> a
sum' :: forall (f :: * -> *) a. (Foldable f, Num a) => f a -> a
sum' = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Num a => a -> a -> a
(+) a
0
product' :: (Foldable f, Num a) => f a -> a
product' :: forall (f :: * -> *) a. (Foldable f, Num a) => f a -> a
product' = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Num a => a -> a -> a
(*) a
1
sumOn' :: (Foldable f, Num b) => (a -> b) -> f a -> b
sumOn' :: forall (f :: * -> *) b a.
(Foldable f, Num b) =>
(a -> b) -> f a -> b
sumOn' a -> b
f = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\b
acc a
x -> b
acc forall a. Num a => a -> a -> a
+ a -> b
f a
x) b
0
productOn' :: (Foldable f, Num b) => (a -> b) -> f a -> b
productOn' :: forall (f :: * -> *) b a.
(Foldable f, Num b) =>
(a -> b) -> f a -> b
productOn' a -> b
f = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\b
acc a
x -> b
acc forall a. Num a => a -> a -> a
* a -> b
f a
x) b
1
anyM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
anyM :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Monad m) =>
(a -> m Bool) -> f a -> m Bool
anyM a -> m Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
(MX.||^) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m Bool
p) (forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
allM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
allM :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Monad m) =>
(a -> m Bool) -> f a -> m Bool
allM a -> m Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
(MX.&&^) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m Bool
p) (forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)
orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
orM :: forall (f :: * -> *) (m :: * -> *).
(Foldable f, Monad m) =>
f (m Bool) -> m Bool
orM = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Monad m) =>
(a -> m Bool) -> f a -> m Bool
anyM forall a. a -> a
id
andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
andM :: forall (f :: * -> *) (m :: * -> *).
(Foldable f, Monad m) =>
f (m Bool) -> m Bool
andM = forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Monad m) =>
(a -> m Bool) -> f a -> m Bool
allM forall a. a -> a
id
findM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m (Maybe a)
findM :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Monad m) =>
(a -> m Bool) -> f a -> m (Maybe a)
findM a -> m Bool
p = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
x -> forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
MX.ifM (a -> m Bool
p a
x) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just a
x)) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
firstJustM :: (Foldable f, Monad m) => (a -> m (Maybe b)) -> f a -> m (Maybe b)
firstJustM :: forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, Monad m) =>
(a -> m (Maybe b)) -> f a -> m (Maybe b)
firstJustM a -> m (Maybe b)
p = forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> [a] -> m (Maybe b)
MX.firstJustM a -> m (Maybe b)
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList