{-# 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 forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (Bool -> Any
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 forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (Bool -> All
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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (a :: k) (f :: k -> *). Traversed a f -> f a
getTraversed forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (forall {k} (a :: k) (f :: k -> *). f a -> Traversed a f
Traversed 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_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip 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 = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. a -> ()
skip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (a :: k) (m :: k -> *). Sequenced a m -> m a
getSequenced forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (forall {k} (a :: k) (m :: k -> *). m a -> Sequenced a m
Sequenced 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_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip 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 = 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 = 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 forall a. a -> Maybe a
Just (i
i, a
a) else Maybe (i, a)
y) 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 = forall i (f :: * -> *) b a.
FoldableWithIndex i f =>
(i -> b -> a -> b) -> b -> f a -> b
ifoldl forall {b}. i -> (b -> m b) -> a -> b -> m b
f' 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 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 = forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr forall {b}. i -> a -> (b -> m b) -> b -> m b
f' 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 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 a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\(i, a) -> b -> b
c b
n -> forall i (f :: * -> *) a b.
FoldableWithIndex i f =>
(i -> a -> b -> b) -> b -> f a -> b
ifoldr (forall a b c. ((a, b) -> c) -> a -> b -> c
curry (i, a) -> b -> b
c) b
n f a
xs)
{-# INLINE itoList #-}