{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Foldable.WithIndex (
FoldableWithIndex (..),
iany,
iall,
inone, none,
itraverse_,
ifor_,
imapM_,
iforM_,
iconcatMap,
ifind,
ifoldrM,
ifoldlM,
itoList,
) where
import Prelude (Bool, Maybe (..), Monad (..), flip, not, (.), curry)
import Control.Applicative (Applicative (..))
import Control.Monad (liftM, void)
import Data.Foldable (Foldable, any)
import Data.Monoid (All (..), Any (..))
import CoerceCompat ((#.), (#..))
import GhcList (build)
import WithIndex
iany :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
iany :: forall i (f :: * -> *) a.
FoldableWithIndex i f =>
(i -> a -> Bool) -> f a -> Bool
iany i -> a -> Bool
f = Any -> Bool
getAny (Any -> Bool) -> (f a -> Any) -> f a -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (i -> a -> Any) -> f a -> Any
forall m a. Monoid m => (i -> a -> m) -> f a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (Bool -> Any
Any (Bool -> Any) -> (i -> a -> Bool) -> i -> a -> Any
forall b c i a.
Coercible b c =>
(b -> c) -> (i -> a -> b) -> i -> a -> c
#.. i -> a -> Bool
f)
{-# INLINE iany #-}
iall :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
iall :: forall i (f :: * -> *) a.
FoldableWithIndex i f =>
(i -> a -> Bool) -> f a -> Bool
iall i -> a -> Bool
f = All -> Bool
getAll (All -> Bool) -> (f a -> All) -> f a -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (i -> a -> All) -> f a -> All
forall m a. Monoid m => (i -> a -> m) -> f a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (Bool -> All
All (Bool -> All) -> (i -> a -> Bool) -> i -> a -> All
forall b c i a.
Coercible b c =>
(b -> c) -> (i -> a -> b) -> i -> a -> c
#.. i -> a -> Bool
f)
{-# INLINE iall #-}
inone :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool
inone :: forall i (f :: * -> *) a.
FoldableWithIndex i f =>
(i -> a -> Bool) -> f a -> Bool
inone i -> a -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (f a -> Bool) -> f a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (i -> a -> Bool) -> f a -> Bool
forall i (f :: * -> *) a.
FoldableWithIndex i f =>
(i -> a -> Bool) -> f a -> Bool
iany i -> a -> Bool
f
{-# INLINE inone #-}
none :: Foldable f => (a -> Bool) -> f a -> Bool
none :: forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Bool
none a -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (f a -> Bool) -> f a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> f a -> Bool
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Bool
any a -> Bool
f
{-# INLINE none #-}
itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f ()
itraverse_ :: forall i (t :: * -> *) (f :: * -> *) a b.
(FoldableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f ()
itraverse_ i -> a -> f b
f = f b -> f ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (f b -> f ()) -> (t a -> f b) -> t a -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Traversed b f -> f b
forall {k} (a :: k) (f :: k -> *). Traversed a f -> f a
getTraversed (Traversed b f -> f b) -> (t a -> Traversed b f) -> t a -> f b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (i -> a -> Traversed b f) -> t a -> Traversed b f
forall m a. Monoid m => (i -> a -> m) -> t a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (f b -> Traversed b f
forall {k} (a :: k) (f :: k -> *). f a -> Traversed a f
Traversed (f b -> Traversed b f)
-> (i -> a -> f b) -> i -> a -> Traversed b f
forall b c i a.
Coercible b c =>
(b -> c) -> (i -> a -> b) -> i -> a -> c
#.. i -> a -> f b
f)
{-# INLINE itraverse_ #-}
ifor_ :: (FoldableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f ()
ifor_ :: forall i (t :: * -> *) (f :: * -> *) a b.
(FoldableWithIndex i t, Applicative f) =>
t a -> (i -> a -> f b) -> f ()
ifor_ = ((i -> a -> f b) -> t a -> f ()) -> t a -> (i -> a -> f b) -> f ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (i -> a -> f b) -> t a -> f ()
forall i (t :: * -> *) (f :: * -> *) a b.
(FoldableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f ()
itraverse_
{-# INLINE ifor_ #-}
imapM_ :: (FoldableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m ()
imapM_ :: forall i (t :: * -> *) (m :: * -> *) a b.
(FoldableWithIndex i t, Monad m) =>
(i -> a -> m b) -> t a -> m ()
imapM_ i -> a -> m b
f = (b -> ()) -> m b -> m ()
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> ()
forall a. a -> ()
skip (m b -> m ()) -> (t a -> m b) -> t a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequenced b m -> m b
forall {k} (a :: k) (m :: k -> *). Sequenced a m -> m a
getSequenced (Sequenced b m -> m b) -> (t a -> Sequenced b m) -> t a -> m b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (i -> a -> Sequenced b m) -> t a -> Sequenced b m
forall m a. Monoid m => (i -> a -> m) -> t a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (m b -> Sequenced b m
forall {k} (a :: k) (m :: k -> *). m a -> Sequenced a m
Sequenced (m b -> Sequenced b m)
-> (i -> a -> m b) -> i -> a -> Sequenced b m
forall b c i a.
Coercible b c =>
(b -> c) -> (i -> a -> b) -> i -> a -> c
#.. i -> a -> m b
f)
{-# INLINE imapM_ #-}
iforM_ :: (FoldableWithIndex i t, Monad m) => t a -> (i -> a -> m b) -> m ()
iforM_ :: forall i (t :: * -> *) (m :: * -> *) a b.
(FoldableWithIndex i t, Monad m) =>
t a -> (i -> a -> m b) -> m ()
iforM_ = ((i -> a -> m b) -> t a -> m ()) -> t a -> (i -> a -> m b) -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (i -> a -> m b) -> t a -> m ()
forall i (t :: * -> *) (m :: * -> *) a b.
(FoldableWithIndex i t, Monad m) =>
(i -> a -> m b) -> t a -> m ()
imapM_
{-# INLINE iforM_ #-}
iconcatMap :: FoldableWithIndex i f => (i -> a -> [b]) -> f a -> [b]
iconcatMap :: forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> [b]) -> f a -> [b]
iconcatMap = (i -> a -> [b]) -> f a -> [b]
forall m a. Monoid m => (i -> a -> m) -> f a -> m
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap
{-# INLINE iconcatMap #-}
ifind :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Maybe (i, a)
ifind :: forall i (f :: * -> *) a.
FoldableWithIndex i f =>
(i -> a -> Bool) -> f a -> Maybe (i, a)
ifind i -> a -> Bool
p = (i -> a -> Maybe (i, a) -> Maybe (i, a))
-> Maybe (i, a) -> f a -> Maybe (i, a)
forall a b. (i -> a -> b -> b) -> b -> f a -> b
forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr (\i
i a
a Maybe (i, a)
y -> if i -> a -> Bool
p i
i a
a then (i, a) -> Maybe (i, a)
forall a. a -> Maybe a
Just (i
i, a
a) else Maybe (i, a)
y) Maybe (i, a)
forall a. Maybe a
Nothing
{-# INLINE ifind #-}
ifoldrM :: (FoldableWithIndex i f, Monad m) => (i -> a -> b -> m b) -> b -> f a -> m b
ifoldrM :: forall i (f :: * -> *) (m :: * -> *) a b.
(FoldableWithIndex i f, Monad m) =>
(i -> a -> b -> m b) -> b -> f a -> m b
ifoldrM i -> a -> b -> m b
f b
z0 f a
xs = (i -> (b -> m b) -> a -> b -> m b) -> (b -> m b) -> f a -> b -> m b
forall b a. (i -> b -> a -> b) -> b -> f a -> b
forall i (f :: * -> *) b a.
FoldableWithIndex i f =>
(i -> b -> a -> b) -> b -> f a -> b
ifoldl i -> (b -> m b) -> a -> b -> m b
forall {b}. i -> (b -> m b) -> a -> b -> m b
f' b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return f a
xs b
z0
where f' :: i -> (b -> m b) -> a -> b -> m b
f' i
i b -> m b
k a
x b
z = i -> a -> b -> m b
f i
i a
x b
z 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 -> m b
k
{-# INLINE ifoldrM #-}
ifoldlM :: (FoldableWithIndex i f, Monad m) => (i -> b -> a -> m b) -> b -> f a -> m b
ifoldlM :: forall i (f :: * -> *) (m :: * -> *) b a.
(FoldableWithIndex i f, Monad m) =>
(i -> b -> a -> m b) -> b -> f a -> m b
ifoldlM i -> b -> a -> m b
f b
z0 f a
xs = (i -> a -> (b -> m b) -> b -> m b) -> (b -> m b) -> f a -> b -> m b
forall a b. (i -> a -> b -> b) -> b -> f a -> b
forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr i -> a -> (b -> m b) -> b -> m b
forall {b}. i -> a -> (b -> m b) -> b -> m b
f' b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return f a
xs b
z0
where f' :: i -> a -> (b -> m b) -> b -> m b
f' i
i a
x b -> m b
k b
z = i -> b -> a -> m b
f i
i b
z 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 -> m b
k
{-# INLINE ifoldlM #-}
itoList :: FoldableWithIndex i f => f a -> [(i,a)]
itoList :: forall i (f :: * -> *) a. FoldableWithIndex i f => f a -> [(i, a)]
itoList f a
xs = (forall b. ((i, a) -> b -> b) -> b -> b) -> [(i, a)]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\(i, a) -> b -> b
c b
n -> (i -> a -> b -> b) -> b -> f a -> b
forall a b. (i -> a -> b -> b) -> b -> f a -> b
forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr (((i, a) -> b -> b) -> i -> a -> b -> b
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (i, a) -> b -> b
c) b
n f a
xs)
{-# INLINE itoList #-}